Membuat gradient warna java

Baik kawan kawan kali ini saya akan menjelaskan cara membuat warna gradient dengan netbeans
yang pertama buat dulu sebuah java package dengan nama terserah dan kemudian di dalamnya buat 3 buah class dengan nama terserah,kalo masih kurang jelas silahkan liat di gambar di bawah ini






















Class Panel berikut ini codinggnya
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 black = Color.black;
    private Color BLUE = Color.BLUE;

    /**
     * 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, black, 0, getHeight(),BLUE);

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


Class PanelDao

package komponen;


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

Class PanelImpl

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


Inilah tampilannya




semoga bermamfaat


0 comments:

Post a Comment