Example usage for java.awt Graphics create

List of usage examples for java.awt Graphics create

Introduction

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

Prototype

public abstract Graphics create();

Source Link

Document

Creates a new Graphics object that is a copy of this Graphics object.

Usage

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.RED);/*from w  w w. ja  v  a  2 s . c o  m*/
    g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
    g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    render(g);
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.RED);/*ww w  .j a  va2  s.  co  m*/
    g2d.fill(poly);

    g2d.setColor(Color.GREEN);
    g2d.translate(50, 100);
    g2d.fill(triangleShape);
    g2d.dispose();
}

From source file:SimpleAttributes.java

public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setBackground(Color.GRAY);
    g2d.clearRect(0, 0, getWidth(), getHeight());

    // String and line with default attributes
    g2d.drawString("Default Font", 10, 20);
    g2d.drawLine(10, 22, 80, 22);/*  ww w  . jav a2 s  . com*/

    // Change the font, foreground color, and Stroke
    g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC, 24f));
    g2d.setColor(Color.WHITE);
    g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));

    // String and line with new attributes
    g2d.drawString("New Font", 10, 50);
    g2d.drawLine(10, 57, 120, 57);
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setColor(Color.RED);//from   ww w.j av a2  s . c  o m
    g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

    Font font = new Font("Arial", Font.BOLD, 48);
    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    int x = ((getWidth() - fm.stringWidth(text)) / 2);
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();

    g2d.setColor(Color.BLACK);
    g2d.drawString(text, x, y);

    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (myImage != null) {
        int x = (getWidth() - myImage.getWidth()) / 2;
        int y = (getHeight() - myImage.getHeight()) / 2;
        g2d.drawImage(myImage, x, y, this);

        g2d.setColor(Color.RED);/*www . j  a  v  a  2  s  . c o m*/
        g2d.translate(x, y);
        g2d.draw(myOffice);
    }
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    int xPos = (getWidth() - bg.getWidth()) / 2;
    int yPos = yOffset;

    yPos = yOffset;/*from w w  w.j  a va 2  s. com*/
    while (yPos < getHeight()) {
        g2d.drawImage(bg, xPos, yPos, this);
        yPos += bg.getHeight();
    }

    g2d.dispose();
}

From source file:RotationAboutCenter.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d;/*from w w  w  . java 2  s  .c om*/
    g2d = (Graphics2D) g.create();

    // Erase background to white
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, getWidth(), getHeight());

    // base rectangle
    g2d.setColor(Color.GRAY.brighter());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees around origin
    g2d.rotate(Math.toRadians(45));
    g2d.setColor(Color.GRAY.darker());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees about center of rect
    g2d = (Graphics2D) g.create();
    g2d.rotate(Math.toRadians(45), 75, 75);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(50, 50, 50, 50);

    // done with g2d, dispose it
    g2d.dispose();
}

From source file:Wallpaper.java

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);

    Graphics2D g2 = (Graphics2D) g.create();

    int w = c.getWidth();
    int h = c.getHeight();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
    g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red));
    g2.fillRect(0, 0, w, h);/*from   w w w.jav  a  2  s .  com*/

    g2.dispose();
}

From source file:SplashScreen.java

public SplashScreen(final BufferedImage img) {
    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            g2d.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), SplashScreen.this);
        }// w w w  .  ja v  a2  s .com
    };
    panel.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));

    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(panel, BorderLayout.NORTH);
    content.add(label = new JLabel(), BorderLayout.CENTER);
    content.add(bar = new JProgressBar(), BorderLayout.SOUTH);
    pack();
    setLocationRelativeTo(null);
}

From source file:Main.java

@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Color color = null;//from ww w . ja  v a2  s.c o m
    JScrollBar sb = (JScrollBar) c;
    if (!sb.isEnabled() || r.width > r.height) {
        return;
    } else if (isDragging) {
        color = Color.DARK_GRAY;
    } else if (isThumbRollover()) {
        color = Color.LIGHT_GRAY;
    } else {
        color = Color.GRAY;
    }
    g2.setPaint(color);
    g2.fillRoundRect(r.x, r.y, r.width, r.height, 10, 10);
    g2.setPaint(Color.WHITE);
    g2.drawRoundRect(r.x, r.y, r.width, r.height, 10, 10);
    g2.dispose();
}