Example usage for java.awt Graphics translate

List of usage examples for java.awt Graphics translate

Introduction

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

Prototype

public abstract void translate(int x, int y);

Source Link

Document

Translates the origin of the graphics context to the point (xy) in the current coordinate system.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object[][] data = { { "A", new Integer(3), new Double(7.23), new Boolean(true) },
            { "J", new Integer(2), new Double(4.64), new Boolean(false) },
            { "S", new Integer(1), new Double(8.81), new Boolean(true) } };

    String[] columns = { "Col", "Col", "Col", "Col" };

    JTable table = new JTable(data, columns);
    JScrollPane scroll = new JScrollPane(table);

    JFrame f = new JFrame();
    f.setContentPane(scroll);/*from www . ja  va2 s  . co  m*/
    f.pack();

    int x = (int) table.getTableHeader().getSize().getWidth();
    int y = (int) table.getTableHeader().getSize().getHeight() + (int) table.getSize().getHeight();

    BufferedImage bi = new BufferedImage((int) x, (int) y, BufferedImage.TYPE_INT_RGB);

    Graphics g = bi.createGraphics();
    table.getTableHeader().paint(g);
    g.translate(0, table.getTableHeader().getHeight());
    table.paint(g);
    g.dispose();

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
    ImageIO.write(bi, "png", new File("c:/Java_Dev/table.png"));

    System.exit(0);
}

From source file:Main.java

public static void drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight) {
    Color oldColor = g.getColor(); // Make no net change to g
    g.translate(x, y);

    g.setColor(shadow);/*from  www  .ja  v  a  2s . co m*/
    g.drawRect(0, 0, w - 2, h - 2);

    g.setColor(highlight);
    g.drawLine(1, h - 3, 1, 1);
    g.drawLine(1, 1, w - 3, 1);

    g.drawLine(0, h - 1, w - 1, h - 1);
    g.drawLine(w - 1, h - 1, w - 1, 0);

    g.translate(-x, -y);
    g.setColor(oldColor);
}

From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java

private static String drowString(Graphics g, String randomString, int i) {
    g.setFont(getFont());//w w  w. j  av  a2  s .  c o  m
    g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
    String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
    randomString += rand;
    g.translate(random.nextInt(3), random.nextInt(3));
    g.drawString(rand, 13 * i, 16);
    return randomString;
}

From source file:Center.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);
    FontMetrics fm = g.getFontMetrics();
    for (int i = 0; i < text.length; i++) {
        int x, y;
        x = (getWidth() - fm.stringWidth(text[i])) / 2;
        y = (i + 1) * fm.getHeight() - 1;
        g.drawString(text[i], x, y);//  www .  j a  va2 s  .  c o  m
    }

}

From source file:Main.java

public void paint(Graphics g) {
    g.translate(100, 100);
    FontMetrics fm = null;//  w w w . ja  v a 2 s  .  c  o m
    int ascent, descent, leading, width1, width2, height;
    String string1 = "www.java2s.com";
    String string2 = "java2s.com";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:metrics.java

License:asdf

public void paint(Graphics g) {
    g.translate(100, 100);
    FontMetrics fm = null;//w w  w  .ja va 2  s .  com
    int ascent, descent, leading, width1, width2, height;
    String string1 = "dsdas";
    String string2 = "asdf";
    int xPos = 25, yPos = 50;
    fm = g.getFontMetrics();
    ascent = fm.getAscent();
    descent = fm.getDescent();
    leading = fm.getLeading();
    height = fm.getHeight();
    width1 = fm.stringWidth(string1);
    width2 = fm.stringWidth(string2);
    g.drawString(string1, xPos, yPos);
    g.drawLine(xPos, yPos - ascent - leading, xPos + width1, yPos - ascent - leading);
    g.drawLine(xPos, yPos - ascent, xPos + width1, yPos - ascent);
    g.drawLine(xPos, yPos, xPos + width1, yPos);
    g.drawLine(xPos, yPos + descent, xPos + width1, yPos + descent);
    g.drawString(string2, xPos, yPos + height);
    g.drawLine(xPos, yPos - ascent - leading + height, xPos + width2, yPos - ascent - leading + height);
    g.drawLine(xPos, yPos - ascent + height, xPos + width2, yPos - ascent + height);
    g.drawLine(xPos, yPos + height, xPos + width2, yPos + height);
    g.drawLine(xPos, yPos + descent + height, xPos + width2, yPos + descent + height);

}

From source file:ImagePrint.java

public int print(Graphics g, PageFormat pf, int pageIndex) {
    Graphics2D g2d = (Graphics2D) g;
    g.translate((int) (pf.getImageableX()), (int) (pf.getImageableY()));
    if (pageIndex == 0) {
        double pageWidth = pf.getImageableWidth();
        double pageHeight = pf.getImageableHeight();
        double imageWidth = printImage.getIconWidth();
        double imageHeight = printImage.getIconHeight();
        double scaleX = pageWidth / imageWidth;
        double scaleY = pageHeight / imageHeight;
        double scaleFactor = Math.min(scaleX, scaleY);
        g2d.scale(scaleFactor, scaleFactor);
        g.drawImage(printImage.getImage(), 0, 0, null);
        return Printable.PAGE_EXISTS;
    }//from   w  w  w . j a  va  2 s  . c  om
    return Printable.NO_SUCH_PAGE;
}

From source file:Main.java

private void myPaint(Component comp, Graphics g) {
    int x = comp.getX();
    int y = comp.getY();
    g.translate(x, y);
    cursor.translate(-x, -y);/* www . j  ava2 s  . c om*/
    if (comp.contains(cursor)) {
        String cls_name = comp.getClass().getName();
        g.setColor(Color.black);
        g.drawString(cls_name, 0, 10);
    }
    if (comp instanceof Container) {
        Container cont = (Container) comp;
        for (int i = 0; i < cont.getComponentCount(); i++) {
            Component child = cont.getComponent(i);
            myPaint(child, g);
        }
    }

    cursor.translate(x, y);
    g.translate(-x, -y);
}

From source file:Ventanas.VentanaVerGrafico.java

public void paint(Graphics g) {
    super.paint(g);
    g.translate(getInsets().left, getInsets().top);
    g.drawImage(grafica, 0, 0, this);
}

From source file:CheckBoxSample.java

public void paintIcon(Component component, Graphics g, int x, int y) {
    boolean selected = false;
    g.setColor(color);//  w w w .j a va 2  s.  c o m
    g.translate(x, y);
    if (component instanceof AbstractButton) {
        AbstractButton abstractButton = (AbstractButton) component;
        selected = abstractButton.isSelected();
    }
    if (selected) {
        g.fillPolygon(polygon);
    } else {
        g.drawPolygon(polygon);
    }
    g.translate(-x, -y);
}