Search in sources :

Example 46 with GradientPaint

use of java.awt.GradientPaint in project jdk8u_jdk by JetBrains.

the class TextRenderingTest method main.

public static void main(final String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
    VolatileImage vi = gc.createCompatibleVolatileImage(width, height);
    while (true) {
        vi.validate(gc);
        Graphics2D g2d = vi.createGraphics();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, width, height);
        g2d.setPaint(new GradientPaint(new Point2D.Float(0, height / 2), Color.white, new Point2D.Float(width, height / 2), Color.black));
        g2d.fillRect(0, 0, width, height);
        String fnt = g2d.getFont().getFamily();
        g2d.setFont(new Font(fnt, Font.PLAIN, 100));
        // draw text with offset
        g2d.drawString("IIIIIIIIII", 100, 100);
        g2d.dispose();
        if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            continue;
        }
        if (vi.contentsLost()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            continue;
        }
        break;
    }
    BufferedImage bi = vi.getSnapshot();
    // the text shifted shouldn't be visible onto a painted rectangle!
    // so the check: color component (blue) must decrease monotonously
    int prev = Integer.MAX_VALUE;
    for (int x = 0; x < width; ++x) {
        int color = bi.getRGB(x, height / 2);
        int b = color & 0xFF;
        if (b > prev) {
            throw new RuntimeException("test failed: can see the text rendered!");
        }
        prev = b;
    }
}
Also used : VolatileImage(java.awt.image.VolatileImage) GradientPaint(java.awt.GradientPaint) GraphicsEnvironment(java.awt.GraphicsEnvironment) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) GradientPaint(java.awt.GradientPaint) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D)

Example 47 with GradientPaint

use of java.awt.GradientPaint in project jdk8u_jdk by JetBrains.

the class TSFrame method render.

private static void render(Graphics g, int w, int h, boolean useNonOpaque) {
    if (useNonOpaque) {
        Graphics2D g2d = (Graphics2D) g;
        GradientPaint p = new GradientPaint(0.0f, 0.0f, new Color(rnd.nextInt(0xffffff)), w, h, new Color(rnd.nextInt(0xff), rnd.nextInt(0xff), rnd.nextInt(0xff), 0), true);
        g2d.setPaint(p);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(0, 0, w, h);
    } else {
        g.setColor(new Color(rnd.nextInt(0xffffff)));
        g.fillRect(0, 0, w, h);
    }
}
Also used : Color(java.awt.Color) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D)

Example 48 with GradientPaint

use of java.awt.GradientPaint in project processdash by dtuma.

the class HierarchyNoteIcon method paintIcon.

@Override
protected void paintIcon(Graphics2D g2, int width, int height, float scale) {
    // fill the background area and trace its outline
    Shape shape = new RoundRectangle2D.Float(0, 0, width - 1, height - 1, 5 * scale, 5 * scale);
    g2.setPaint(new GradientPaint(0, 0, fill, width, height, Color.white));
    g2.fill(shape);
    g2.setStroke(new BasicStroke(1));
    g2.setColor(edge);
    g2.draw(shape);
    // draw text in the center
    int x = (int) (2 * Math.min(scale, 2));
    int y = (int) (3 * scale);
    int lineSpacing = (int) (2.3 * scale);
    g2.setClip(new RoundRectangle2D.Float(x, x, width - 2 * x, height - 2 * x, 2 * scale, 2 * scale));
    g2.setFont(new Font("Dialog", Font.BOLD, 10).deriveFont(1.9f * scale));
    g2.setColor(text);
    String s = TEXT;
    while (y <= height - 2) {
        g2.drawString(s, x, y);
        s = s.substring(10);
        y += lineSpacing;
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) RoundRectangle2D(java.awt.geom.RoundRectangle2D) GradientPaint(java.awt.GradientPaint) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font)

Example 49 with GradientPaint

use of java.awt.GradientPaint in project processdash by dtuma.

the class RangeXYItemRenderer method drawItemRangeGradient.

private void drawItemRangeGradient(Graphics2D g2, Line2D line, Paint paint, Stroke stroke, double x2, double y2, double x3, double y3) {
    Line2D edge1, edge2, mainLine;
    Polygon fillArea;
    Stroke mainLineStroke, edgeLineStroke;
    Paint mainLinePaint, edgeLinePaint, fillPaint;
    double x0 = line.getX1();
    double y0 = line.getY1();
    double x1 = line.getX2();
    double y1 = line.getY2();
    mainLine = new Line2D.Double(x0, y0, x1, y1);
    edge1 = new Line2D.Double(x0, y0, x2, y2);
    edge2 = new Line2D.Double(x0, y0, x3, y3);
    fillArea = new Polygon();
    fillArea.addPoint((int) Math.round(x0), (int) Math.round(y0));
    fillArea.addPoint((int) Math.round(x2), (int) Math.round(y2));
    fillArea.addPoint((int) Math.round(x3), (int) Math.round(y3));
    mainLinePaint = paint;
    if (mainLinePaint instanceof Color) {
        Color c = (Color) mainLinePaint;
        Color dark = transp(c, calcAlpha(c));
        Color light = transp(c, 0.01);
        edgeLinePaint = fillPaint = c;
        try {
            fillPaint = new GradientPaint(gradientStart(x0, y0, x1, y1, x2, y2, x3, y3), light, new Point2D.Double(x1, y1), dark, true);
        } catch (Exception e) {
        }
    } else {
        edgeLinePaint = fillPaint = mainLinePaint;
    }
    if (stroke instanceof BasicStroke) {
        float lineWidth = ((BasicStroke) stroke).getLineWidth();
        edgeLineStroke = new BasicStroke(lineWidth / 4);
        mainLineStroke = new BasicStroke(lineWidth * 2);
    } else {
        mainLineStroke = edgeLineStroke = stroke;
    }
    g2.setPaint(fillPaint);
    g2.fill(fillArea);
    g2.fill(fillArea);
    g2.setStroke(edgeLineStroke);
    g2.setPaint(edgeLinePaint);
    g2.draw(edge1);
    g2.draw(edge2);
    g2.setStroke(mainLineStroke);
    g2.setPaint(mainLinePaint);
    g2.draw(mainLine);
}
Also used : BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Polygon(java.awt.Polygon) Line2D(java.awt.geom.Line2D)

Example 50 with GradientPaint

use of java.awt.GradientPaint in project vcell by virtualcell.

the class ReactionToolShapeIcon method main.

public static void main(String[] args) {
    try {
        Frame frame = new Frame();
        Icon iconNormal = new ReactionToolShapeIcon(State.normal, Mode.plain);
        int diameter = 20;
        int x = 50;
        int y = 80;
        JPanel panel = new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                Color colorOld = g2.getColor();
                Paint paintOld = g2.getPaint();
                Stroke strokeOld = g2.getStroke();
                // --------------------------------------------------------------
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setStroke(new BasicStroke(5f));
                g2.setColor(Color.black);
                Color c1, c2;
                // -16
                int xx = x - 16;
                // -15
                int yy = y - 15;
                // 8
                int w = diameter * 8;
                int h = diameter * 8;
                c1 = Color.black;
                c2 = Color.lightGray;
                Arc2D arc = new // Line2D
                Arc2D.Double(// Line2D
                xx, // Line2D
                yy, // Line2D
                w, // Line2D
                h, 0, 300, Arc2D.OPEN);
                GradientPaint gp = new GradientPaint(50, 50 + 200, c1, 50 + 5, 50 + 200 - 5, c2, true);
                g2.setPaint(gp);
                g2.draw(arc);
                // -------------------------------------------------------------
                g2.setPaint(paintOld);
                int a = 0;
                float[] dash = { 10.0f };
                Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dash, 0);
                g2.setStroke(dashed);
                g2.drawLine(10, 10 + a, 150, 10 + a);
                a = 10;
                float[] dash1 = { 5.0f };
                Stroke dashed1 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dash1, 0);
                g2.setStroke(dashed1);
                g2.drawLine(10, 10 + a, 150, 10 + a);
                a = 20;
                float[] dash2 = { 12.0f, 10.0f, 8.0f };
                Stroke dashed2 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dash2, 0);
                g2.setStroke(dashed2);
                g2.drawLine(10, 10 + a, 150, 10 + a);
                a = 30;
                float[] dash3 = { 12.0f, 10.0f, 8.0f };
                Stroke dashed3 = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, dash3, 50);
                g2.setStroke(dashed3);
                g2.drawLine(10, 10 + a, 150, 10 + a);
                // ----------------------------------------------------------------------------------
                g2.setStroke(strokeOld);
                g2.setColor(colorOld);
                g2.setPaint(paintOld);
            }
        };
        panel.setSize(500, 400);
        frame.add("Center", panel);
        frame.setSize(panel.getSize());
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent event) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    } catch (Throwable exception) {
        System.err.println("Exception occurred in main() of java.awt.Panel");
        exception.printStackTrace(System.out);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) JPanel(javax.swing.JPanel) Frame(java.awt.Frame) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) GradientPaint(java.awt.GradientPaint) WindowAdapter(java.awt.event.WindowAdapter) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Arc2D(java.awt.geom.Arc2D) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) WindowEvent(java.awt.event.WindowEvent) Icon(javax.swing.Icon)

Aggregations

GradientPaint (java.awt.GradientPaint)55 Graphics2D (java.awt.Graphics2D)37 Color (java.awt.Color)33 BasicStroke (java.awt.BasicStroke)14 Font (java.awt.Font)13 Paint (java.awt.Paint)12 Rectangle2D (java.awt.geom.Rectangle2D)11 FontMetrics (java.awt.FontMetrics)9 BufferedImage (java.awt.image.BufferedImage)9 Rectangle (java.awt.Rectangle)8 RadialGradientPaint (java.awt.RadialGradientPaint)6 Stroke (java.awt.Stroke)6 RoundRectangle2D (java.awt.geom.RoundRectangle2D)6 Point2D (java.awt.geom.Point2D)5 LinearGradientPaint (java.awt.LinearGradientPaint)4 Shape (java.awt.Shape)4 Ellipse2D (java.awt.geom.Ellipse2D)4 RectangularShape (java.awt.geom.RectangularShape)4 Graphics (java.awt.Graphics)3 MultipleGradientPaint (java.awt.MultipleGradientPaint)3