Example usage for java.awt GradientPaint GradientPaint

List of usage examples for java.awt GradientPaint GradientPaint

Introduction

In this page you can find the example usage for java.awt GradientPaint GradientPaint.

Prototype

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

Source Link

Document

Constructs a simple acyclic GradientPaint object.

Usage

From source file:Main.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
            Graphics2D imageGraphics = image.createGraphics();
            GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange);
            imageGraphics.setPaint(gp);// w  w  w  . j  a v a2  s  . c o  m
            imageGraphics.fillRect(0, 0, 400, 300);

            JLabel textLabel = new JLabel("java2s.com");
            textLabel.setSize(textLabel.getPreferredSize());

            Dimension d = textLabel.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.setColor(new Color(255, 200, 255, 128));
            g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10);
            g.setColor(Color.black);
            textLabel.paint(g);
            Graphics g2 = image.getGraphics();
            g2.drawImage(bi, 20, 20, f);

            ImageIcon ii = new ImageIcon(image);
            JLabel imageLabel = new JLabel(ii);

            f.getContentPane().add(imageLabel);
            f.pack();

            f.setVisible(true);
        }
    });
}

From source file:MakeFades.java

public static void main(String[] args) throws IOException, NumberFormatException {

    // Create from and to colors based on those arguments
    Color from = Color.RED; // transparent
    Color to = Color.BLACK; // opaque

    // Loop through the sizes and directions, and create an image for each
    for (int s = 0; s < sizes.length; s++) {
        for (int d = 0; d < directions.length; d++) {
            // This is the size of the image
            int size = sizes[s];

            // Create a GradientPaint for this direction and size
            Paint paint = new GradientPaint(directions[d][0] * size, directions[d][1] * size, from,
                    directions[d][2] * size, directions[d][3] * size, to);

            // Start with a blank image that supports transparency
            BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

            // Now use fill the image with our color gradient
            Graphics2D g = image.createGraphics();
            g.setPaint(paint);/*from  ww  w. j av a2s. c o  m*/
            g.fillRect(0, 0, size, size);

            // This is the name of the file we'll write the image to
            File file = new File("fade-to-" + sizeNames[s] + "-" + directionNames[d] + ".png");

            // Save the image in PNG format using the javax.imageio API
            javax.imageio.ImageIO.write(image, "png", file);

            // Show the user our progress by printing the filename
            System.out.println(file);
        }
    }
}

From source file:Draw2DTest.java

public static void main(String[] args) {
    final Graphics2DRenderer renderer = new Graphics2DRenderer();
    Shell shell = new Shell();
    shell.setSize(350, 350);/*from  w  w w. j av a2s.c om*/

    shell.open();
    shell.setText("Draw2d Hello World");
    LightweightSystem lws = new LightweightSystem(shell);
    IFigure figure = new Figure() {
        protected void paintClientArea(org.eclipse.draw2d.Graphics graphics) {
            Dimension controlSize = getSize();

            renderer.prepareRendering(graphics);
            // prepares the Graphics2D renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.width,
                    (float) controlSize.width, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.width, controlSize.width);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.width / 2, controlSize.width / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(graphics);

            // now we can continue with pure SWT paint operations
            graphics.drawOval(0, 0, controlSize.width, controlSize.width);
        }
    };
    lws.setContents(figure);
    Display display = Display.getDefault();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    renderer.dispose();
}

From source file:SWTTest.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(350, 350);/*w  ww .j a va2  s  .  co m*/
    shell.setLayout(new GridLayout());
    Canvas canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    GridData data = new GridData(GridData.FILL_BOTH);
    canvas.setLayoutData(data);

    final Graphics2DRenderer renderer = new Graphics2DRenderer();

    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            Point controlSize = ((Control) e.getSource()).getSize();

            GC gc = e.gc; // gets the SWT graphics context from the event

            renderer.prepareRendering(gc); // prepares the Graphics2D
            // renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.x,
                    (float) controlSize.y, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.x, controlSize.y);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.x / 2, controlSize.y / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(gc);

            // now we can continue with pure SWT paint operations
            gc.drawOval(0, 0, controlSize.x, controlSize.y);

        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
    renderer.dispose();
    System.exit(0);
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawGradientPaintTest(Graphics2D g2) {
    g2.setPaint(new GradientPaint(10f, 10f, Color.RED, 10f, 60f, Color.YELLOW));
    g2.fillRect(10, 10, 50, 50);/*from  w  ww  . ja va 2 s.  c  o  m*/
    g2.setPaint(Color.BLUE);
    g2.fillRect(60, 10, 50, 50);
}

From source file:Main.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

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

    g2d.setPaint(gp1);//from w w w  .j av a2s  . c  o m
    g2d.fillRect(20, 20, 300, 40);

}

From source file:lu.lippmann.cdb.graph.renderer.CadralVertexColorRenderer.java

/**
 * {@inheritDoc}//from w ww .  j  a v  a2s . com
 */
@Override
public Paint transform(CNode node) {
    return new GradientPaint(0, 0, node.getColor(), 40, 40, node.getColor());
}

From source file:Main.java

/**
 * Fills a gradient using the start color and end color specified.
 *
 * @param g// ww w.  ja v a2 s.co m
 *          the gradient
 * @param s
 *          the shape to fill
 * @param start
 *          the start color
 * @param end
 *          the end color
 * @param vertical
 *          true for a vertical gradient; false for horizontal
 */
public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) {
    Rectangle r = s.getBounds();
    Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end);
    Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setPaint(gp);
    g.fill(s);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o);
}

From source file:Main.java

public static void smoothFillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) steps;
    int y1 = y;/*from   ww  w.java2  s . c  om*/
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        g.setColor(colors[colors.length - i - 1]);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[colors.length - i - 1]);
            g.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[colors.length - i - 1], 0, y2,
                    colors[colors.length - i - 2]));
            g.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    Color startColor = Color.red;
    Color endColor = Color.blue;

    int startX = 10, startY = 20, endX = 30, endY = 40;

    GradientPaint gradient = new GradientPaint(startX, startY, startColor, endX, endY, endColor);
    g2d.setPaint(gradient);//ww w. jav  a2 s. co m

    g2d.draw(new Rectangle(20, 20, 200, 200));

}