Example usage for java.awt Graphics setColor

List of usage examples for java.awt Graphics setColor

Introduction

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

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:MessagePopup.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);//from ww w . j  a  v  a  2  s  .  c  o  m
    if (selected) {
        g.fillPolygon(poly);
    } else {
        g.drawPolygon(poly);
    }
    g.translate(-x, -y);
}

From source file:OvalIcon.java

public void paintIcon(Component c, Graphics g, int x, int y) {
    if (color != null) {
        int width = getIconWidth();
        int height = getIconHeight();
        g.setColor(color);
        g.fillOval(x, y, width, height);
    }/*from  ww  w.  j a v  a2 s . c  om*/
}

From source file:painting.IconDisplayer.java

protected void paintComponent(Graphics g) {
    if (isOpaque()) { //paint background
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
    }//from   w  ww. j a  va2  s  .c  om

    if (icon != null) {
        //Draw the icon over and over, right aligned.
        Insets insets = getInsets();
        int iconWidth = icon.getIconWidth();
        int iconX = getWidth() - insets.right - iconWidth;
        int iconY = insets.top;
        boolean faded = false;

        //Copy the Graphics object, which is actually
        //a Graphics2D object.  Cast it so we can
        //set alpha composite.
        Graphics2D g2d = (Graphics2D) g.create();

        //Draw the icons, starting from the right.
        //After the first one, the rest are faded.
        //We won't bother painting icons that are clipped.
        g.getClipBounds(clipRect);
        while (iconX >= insets.left) {
            iconRect.setBounds(iconX, iconY, iconWidth, icon.getIconHeight());
            if (iconRect.intersects(clipRect)) {
                icon.paintIcon(this, g2d, iconX, iconY);
            }
            iconX -= (iconWidth + pad);
            if (!faded) {
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
                faded = true;
            }
        }

        g2d.dispose(); //clean up
    }
}

From source file:Clock.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    calendar.setTime(new Date()); // get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());//from  w w  w  . ja v a 2 s  . c  o m
    if (isDigital) {
        String time = "" + hrs + ":" + min;
        g.setFont(getFont());
        FontMetrics fm = g.getFontMetrics();
        int y = (getHeight() + fm.getAscent()) / 2;
        int x = (getWidth() - fm.stringWidth(time)) / 2;
        g.drawString(time, x, y);
    } else {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        int rh = getHeight() / 4;
        int rm = getHeight() / 3;

        double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
        double am = min / 30.0 * Math.PI;

        g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
        g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
    }

    g.setColor(colorRetainer);
}

From source file:GlassPaneDemo.java

protected void paintComponent(Graphics g) {
    if (point != null) {
        g.setColor(Color.red);
        g.fillOval(point.x - 10, point.y - 10, 20, 20);
    }//from w  w w .j  av a  2 s. c o  m
}

From source file:raspihomeapp.ParamForm.java

private void drawClock(final Graphics g) {

    g.setColor(Color.white);
    g.drawString("Leistung in W", 1, 8);
    g.drawString("Uhrzeit", 750, 320);
    g.drawLine(20, 320, 20, 15); // Y -Achse
    g.drawLine(10, 310, 790, 310); // X-Achse

}

From source file:Main.java

@Override
public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
    g.setColor(color == null ? c.getSelectionColor() : color);
    Rectangle rect = null;/* www  .  ja  v a2s.c  om*/
    if (offs0 == view.getStartOffset() && offs1 == view.getEndOffset()) {
        if (bounds instanceof Rectangle) {
            rect = (Rectangle) bounds;
        } else {
            rect = bounds.getBounds();
        }
    } else {
        try {
            Shape shape = view.modelToView(offs0, Position.Bias.Forward, offs1, Position.Bias.Backward, bounds);
            rect = (shape instanceof Rectangle) ? (Rectangle) shape : shape.getBounds();
        } catch (BadLocationException e) {
            return null;
        }
    }
    FontMetrics fm = c.getFontMetrics(c.getFont());
    int baseline = rect.y + rect.height - fm.getDescent() + 1;
    g.drawLine(rect.x, baseline, rect.x + rect.width, baseline);
    g.drawLine(rect.x, baseline + 1, rect.x + rect.width, baseline + 1);
    return rect;
}

From source file:org.accada.reader.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna/*  w  w w  .  j ava 2 s.  c  o m*/
 * 
 * @param the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getProperty("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:MultiBufferTest.java

public MultiBufferTest(int numBuffers, GraphicsDevice device) {
    try {/*from   www. j  a  va  2 s  . co  m*/
        GraphicsConfiguration gc = device.getDefaultConfiguration();
        mainFrame = new Frame(gc);
        mainFrame.setUndecorated(true);
        mainFrame.setIgnoreRepaint(true);
        device.setFullScreenWindow(mainFrame);
        if (device.isDisplayChangeSupported()) {
            chooseBestDisplayMode(device);
        }
        Rectangle bounds = mainFrame.getBounds();
        mainFrame.createBufferStrategy(numBuffers);
        BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
        for (float lag = 2000.0f; lag > 0.00000006f; lag = lag / 1.33f) {
            for (int i = 0; i < numBuffers; i++) {
                Graphics g = bufferStrategy.getDrawGraphics();
                if (!bufferStrategy.contentsLost()) {
                    g.setColor(COLORS[i]);
                    g.fillRect(0, 0, bounds.width, bounds.height);
                    bufferStrategy.show();
                    g.dispose();
                }
                try {
                    Thread.sleep((int) lag);
                } catch (InterruptedException e) {
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        device.setFullScreenWindow(null);
    }
}

From source file:OptimalPrimitives.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    long startTime, endTime, totalTime;

    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);/*from   www .j  ava 2  s .c  o m*/

    g.drawString("Bad vs. Good Primitive Rendering", 50, 20);
    g.drawString("(" + ITERATIONS + " iterations)", 100, 35);
    g.drawString("Bad: ", 10, BAD_Y + 30);
    g.drawString("Good: ", 10, GOOD_Y + 30);

    // Bad line
    Shape line = new Line2D.Double(LINE_X, BAD_Y, LINE_X + 50, BAD_Y + 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.draw(line);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, BAD_Y + 70);

    // Good line
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.drawLine(LINE_X, GOOD_Y, LINE_X + 50, GOOD_Y + 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, GOOD_Y + 70);

    // Bad rect
    Shape rect = new Rectangle(RECT_X, BAD_Y, 50, 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.fill(rect);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, BAD_Y + 70);

    // Good rect
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.fillRect(RECT_X, GOOD_Y, 50, 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, GOOD_Y + 70);
}