PaintComponent Membuat Form Berwarna Gradient

Awal belajar Java di Netbeans hal pertama yang saya tanyakan adalah gimana caranya supaya membuat Panel yang standar NetBeans cuman bisanya satu warna doang menjadi lebih berwarna (yaaa.... seperti ada efek gradasi warna dari 2 warna yag berbeda).

Pengen tau, cari tau, sudah tau, akhirnya berbagi tau.... hehe

Kira-kira seperti ini...














>> Kodingannya kira-kira seperti ini... (Saya buat dalam Package "komponen")
// Panel.java

package komponen;

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 java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Jie
 */
public class Panel extends JPanel {

    private static final long serialVersionUID = -1;
    private BufferedImage gradientImage;
    private Color white = Color.WHITE;
    private Color cyan = Color.CYAN;

    /**
     * membuat panel white cyan
     */
    public Panel() {
        super();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (isOpaque()) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            setUpGradient();
            g2.drawImage(gradientImage, 0, 0, getWidth(), getHeight(), null);

            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);

            GradientPaint 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);
            g2.dispose();
        }
    }

    /**
     * membuat gambar background gradient
     */
    private void setUpGradient() {
        gradientImage = new BufferedImage(1, getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) gradientImage.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        GradientPaint paint = new GradientPaint(0, 0, white, 0, getHeight(), cyan);

        g2.setPaint(paint);
        g2.fillRect(0, 0, 1, getHeight());
        g2.dispose();
    }
}





// PanelDao.java

package komponen;

/**
 *
 * @author Jie
 */
public interface PanelDao {
    public static final int BACKGROUND_IMAGE_CENTER_BOTTOM = 0;
    public static final int BACKGROUND_IMAGE_CENTER_MIDDLE = 1;
    public static final int BACKGROUND_IMAGE_CENTER_TOP = 2;
    public static final int BACKGROUND_IMAGE_LEFT_BOTTOM = 3;
    public static final int BACKGROUND_IMAGE_LEFT_MIDDLE = 4;
    public static final int BACKGROUND_IMAGE_LEFT_TOP = 5;
    public static final int BACKGROUND_IMAGE_RIGHT_BOTTOM = 6;
    public static final int BACKGROUND_IMAGE_RIGHT_MIDDLE = 7;
    public static final int BACKGROUND_IMAGE_RIGHT_TOP = 8;
    public static final int BACKGROUND_IMAGE_STRECT = 9;
    public static final int BACKGROUND_IMAGE_TILED = 10;
}



// PanelImpl.java

package komponen;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Paint;
import javax.swing.Icon;
import javax.swing.JPanel;

/**
 *
 * @author Jie
 */
public class PanelImpl extends JPanel implements PanelDao{

    private static final long serialVersionUID = 1L;
    private Icon backgroundImage;
    private int backgroundImageType;
    private Paint backgroundPaint;
    private boolean opaqueGradient;
    private boolean opaqueImage;
    private Image gambar;

    public PanelImpl() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public Icon getBackgroundImage() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public int getBackgroundImageType() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public Paint getBackgroundPaint() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public boolean isOpaqueGradient() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public boolean isOpaqueImage() {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    protected void paintComponent(Graphics g) {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public void setBackgroundImage(Icon backgroundImage) throws IllegalArgumentException {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public void setBackgroundImageType(int backgroundImageType) throws IllegalArgumentException {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public void setBackgroundPaint(Paint backgroundPaint) {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public void setOpaqueGradient(boolean opaqueGradient) {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }

    public void setOpaqueImage(boolean opaqueImage) {
        //compiled code
        throw new RuntimeException("Compiled Code");
    }
}





>> cara menggunakannya tinggal drag n drop Panel.java ke Frame yang akan kita buat..


Penampakannya...

Artikel Lainnya