Java – Membuat Swing Panel Gradient

Gradient Panel

GradientPanel.java

package swing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;

import javax.swing.JPanel;

public class GradientPanel extends JPanel {

	/**
	 * Create the panel.
	 */
	public GradientPanel() {
		
	}
	
    @Override
    protected void paintComponent(Graphics g) {   
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        GradientPaint paint = new GradientPaint(0, 0, Color.BLACK, 0, getHeight(), Color.BLUE);
        
        g2.setPaint(paint);
        g2.fillRect(0, 0, getWidth(), getHeight());

        int width = getWidth();
        int height = getHeight() * 5 / 100;

        Color light = new Color(1F, 1F, 1F, 0.5F);
        Color dark = new Color(1F, 1F, 1F, 0.0F);

        paint = new GradientPaint(0, 0, light, 0, height, dark);
        GeneralPath path = new GeneralPath();
        path.moveTo(0, 0);
        path.lineTo(0, height);
        path.curveTo(0, height, width / 2, height / 2, width, height);
        path.lineTo(width, 0);
        path.closePath();

        g2.setPaint(paint);
        g2.fill(path);

        paint = new GradientPaint(0, getHeight(), light, 0, getHeight() - height, dark);
        path = new GeneralPath();
        path.moveTo(0, getHeight());
        path.lineTo(0, getHeight() - height);
        path.curveTo(0, getHeight() - height, width / 2, getHeight() - height / 2, width, getHeight() - height);
        path.lineTo(width, getHeight());
        path.closePath();

        g2.setPaint(paint);
        g2.fill(path);
    

    }

}

Contoh penggunaan :
MainWindow.java

package swing;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;


public class MainWindow extends JFrame {

	private GradientPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new GradientPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

}

Semoga bermanfaat,

mazipan-signature

One thought on “Java – Membuat Swing Panel Gradient

Be a good reader, leave your comment please.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s