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

static void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    // x1 and y1 are coordinates of circle or rectangle
    // x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
    Graphics2D g = (Graphics2D) g1.create();
    double dx = x2 - x1;
    double dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform t = AffineTransform.getTranslateInstance(x1, y1);
    t.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(t);//www .ja  v a  2  s . c  o m
    g.drawLine(0, 0, len, 0);
    int basePosition = len - ARROW_HEAD_SIZE.width;
    int height = ARROW_HEAD_SIZE.height;
    g.fillPolygon(new int[] { len, basePosition, basePosition, len }, new int[] { 0, -height, height, 0 }, 4);
}

From source file:Main.java

/**
 * Draws a double headed arrow with arrow heads of a given width and height.
 * /* www.j  a va2s.c  o m*/
 * @param aG
 *          the canvas to draw on;
 * @param aX1
 *          the starting X position of the arrow;
 * @param aY1
 *          the starting Y position of the arrow;
 * @param aX2
 *          the ending X position of the arrow;
 * @param aY2
 *          the ending Y position of the arrow;
 * @param aArrowWidth
 *          the total width of the arrow head;
 * @param aArrowHeight
 *          the total height of the arrow head.
 */
public static final void drawDoubleHeadedArrow(final Graphics aG, final int aX1, final int aY1, final int aX2,
        final int aY2, final int aArrowWidth, final int aArrowHeight) {
    final Graphics2D g2d = (Graphics2D) aG.create();

    final int lineWidth = Math.abs(aX2 - aX1);
    final int threshold = (2 * aArrowWidth) + 2;
    try {
        int x1 = aX1;
        int x2 = aX2;

        if (lineWidth > threshold) {
            drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight);
            // why x2 needs to be shifted by one pixel is beyond me...
            drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight);

            x1 += aArrowWidth - 1;
            x2 -= aArrowWidth + 1;
        }

        g2d.drawLine(x1, aY1, x2, aY2);
    } finally {
        g2d.dispose();
    }
}

From source file:Main.java

public void paint(Graphics g) {
    g.setColor(Color.red);/*w  ww.j  a  v  a 2  s  . com*/
    Graphics clippedGraphics = g.create();
    clippedGraphics.drawRect(0, 0, 100, 100);
    clippedGraphics.clipRect(25, 25, 50, 50);
    clippedGraphics.drawLine(0, 0, 100, 100);
    clippedGraphics.dispose();
    clippedGraphics = null;
    g.drawLine(0, 100, 100, 0);
}

From source file:Main.java

public void paint(Graphics g) {
    g.setColor(Color.red);//  ww  w. java2  s  .  c  om
    Graphics clippedGraphics = g.create();
    clippedGraphics.drawRect(0, 0, 100, 100);
    clippedGraphics.clipRect(25, 25, 50, 50);
    clippedGraphics.drawLine(0, 0, 100, 100);
    clippedGraphics.finalize();
    clippedGraphics = null;
    g.drawLine(0, 100, 100, 0);
}

From source file:Main.java

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    if (!on) {// ww  w .java 2s  .co m
        g2d.setComposite(AlphaComposite.SrcOver.derive(0f));
    } else {
        g2d.setComposite(AlphaComposite.SrcOver.derive(1f));
    }
    super.paint(g2d);
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(getBackground());/*ww  w. j av a2  s  . c  o m*/
    g2d.fillRect(0, 0, getWidth(), getHeight());
    if (image != null) {
        int x = getWidth() - image.getWidth();
        int y = getHeight() - image.getHeight();
        g2d.drawImage(image, x, y, this);
    }
    super.paintComponent(g2d);
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = getWidth() + "x" + getHeight();
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
    g2d.drawString(text, x, y);//ww  w  .j  a  va2 s  .c  o m
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = "I don't see the problem";
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent();
    g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2));
    g2d.drawString(text, x, y);//from  www.  j  av  a 2s  .co m
    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);/*w ww  .j a  v a 2s  . co m*/
    g2d.drawLine(0, 0, getWidth(), getHeight());
    g2d.drawLine(getWidth(), 0, 0, getHeight());
    g2d.dispose();
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
    g2d.fillRect(10, 10, getWidth(), getHeight());
    g2d.dispose();//from  w  w w  .  ja v a2  s  . c o  m
}