Title: Shape Fills
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Shape Fills

So you are bored with solid colors for Shape fill properties.

Our first option is a Gradient fill. To make a Gradient fill we use the following constructor:

GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)

GradientFills.java



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

import javax.swing.JFrame;

public class GradientFills extends JFrame{
    private final double points[][] = {
            { 0, 85 }, { 75, 75 }, { 100, 10 }, { 125, 75 },
            { 200, 85 }, { 150, 125 }, { 160, 190 }, { 100, 150 },
            { 40, 190 }, { 50, 125 }, { 0, 85 }
    };
    GradientFills()
    {
        setSize(600, 500);
        setTitle("GradientFills");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

        GeneralPath star = new GeneralPath();

        star.moveTo(points[0][0], points[0][1]);  // Add the origin of the Shape

        for (int k = 1; k < points.length; k++)
            star.lineTo(points[k][0], points[k][1]); // Adding Line Segments  to the Shape

        //  star.closePath(); // doesn't seem to be needed

        g2d.setColor(Color.green);
        g2d.setStroke( new BasicStroke(4));
        g2d.draw(star);


        GradientPaint gp1 = new GradientPaint(5, 5,
            Color.red, 20, 20, Color.black, true);

        g2d.setPaint(gp1);
        g2d.fill(star);
        g2d.fillRect(200, 20, 300, 40);

        GradientPaint gp2 = new GradientPaint(5, 25,
            Color.yellow, 20, 2, Color.black, true);

        g2d.setPaint(gp2);
        g2d.fillRect(200, 80, 300, 40);

        GradientPaint gp3 = new GradientPaint(5, 25,
            Color.green, 2, 2, Color.black, true);

        g2d.setPaint(gp3);
        g2d.fillRect(200, 140, 300, 40);

        GradientPaint gp4 = new GradientPaint(350, 00,
            Color.blue, 250, 00, Color.black, false);

        g2d.setPaint(gp4);
        g2d.fillRect(200, 200, 300, 40);

        GradientPaint gp5 = new GradientPaint(0, 0,
             Color.orange, 0, 20, Color.black, true);

        g2d.setPaint(gp5);
        g2d.fillRect(200, 260, 300, 140);

        g2d.dispose();
    }

    public static void main(String[] args)
    {
        GradientFills sgp = new GradientFills();
    }

}



Texture Fills

Another option is to fill your Shape with your favorite image:

squirrel.png

java.png

TextureFill.java


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;



public class TextureFill extends JFrame{

    BufferedImage squirrelImage;
    BufferedImage javaImage;

    private final double points[][] = {
            { 40, 185 },  { 125, 75 },
            { 400, 85 }, { 450, 525 },
            { 50, 525 }, { 40, 185 }
    };
    TextureFill()
    {
        loadImages();
        setSize(650, 820);
        setTitle("TextureFill");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    private void loadImages() {

        try {

            squirrelImage = ImageIO.read(new File("squirrel.png"));
            javaImage = ImageIO.read(new File("java.png"));

        } catch (IOException ex) {

            System.out.println("Error reading in images: +ex");
        }
    }
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

        GeneralPath shape1 = new GeneralPath();

        shape1.moveTo(points[0][0], points[0][1]);  // Add the origin of the Shape

        for (int k = 1; k < points.length; k++)
            shape1.lineTo(points[k][0], points[k][1]); // Adding Line Segments  to the Shape

        //  star.closePath(); // doesn't seem to be needed
        TexturePaint squirrelTexture = new TexturePaint(squirrelImage, new Rectangle(0, 0, 120, 120));
        g2d.setPaint(squirrelTexture);
        g2d.fill(shape1);

        Ellipse2D.Double ellipse = new Ellipse2D.Double(50, 550, 500, 200);
        TexturePaint javaTexture = new TexturePaint(javaImage, new Rectangle(0, 0, 120, 120));
        g2d.setPaint(javaTexture);
        g2d.fill(ellipse);
        g2d.dispose();
    }

    public static void main(String[] args)
    {
        TextureFill tf = new TextureFill();
    }

}






Last Updated: June 17, 2015 4:22 PM