Java by API/java.awt/Graphics2D

Материал из Java эксперт
Перейти к: навигация, поиск

Graphics2D: drawGlyphVector(GlyphVector g, float x, float y)

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    
    String s = "www.jexp.ru";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);
  }
}





Graphics2D: draw(Shape s)

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  Shape shape;
  public MyCanvas() {
    shape = create();
  }
  protected Shape create() {
    GeneralPath path = new GeneralPath();
    path.moveTo(50, 30);
    path.lineTo(30, 200);
    return path;
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.black);
    g2.draw(shape);
  }
}





Graphics2D: drawString(String str, int x, int y)

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);
    g2.drawString("jade", 40, 120); 
  }
}





Graphics2D: drawString(String str, int x, int y) (Unicode)

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);
    g2.drawString("Please \u062e\u0644\u0639 slowly.", 40, 80);
  }
}





Graphics2D: fill(Shape s)

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Shape shape = new Rectangle2D.Float(100, 50, 80, 80);
    
    Graphics2D g2 = (Graphics2D) g;
    
    g2.fill(shape);
  }
}





Graphics2D: getFontRenderContext()

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MainClass {
  public static void main(String[] a) throws Exception {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    TextFormat tl = new TextFormat();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class TextFormat extends JComponent {
   List layouts;
   Font font = new Font("SansSerif", Font.BOLD, 42);
   String text = "The quick brown fox jumped over the lazy cow";
  public void paint(Graphics g) {
    if (layouts == null)
      getLayouts(g);
    Point pen = new Point(0, 0);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(java.awt.Color.black); // or a property
    g2d.setFont(font);
    Iterator it = layouts.iterator();
    while (it.hasNext()) {
      TextLayout layout = (TextLayout) it.next();
      pen.y += (layout.getAscent());
      g2d.setFont(font);
      layout.draw(g2d, pen.x, pen.y);
      pen.y += layout.getDescent();
    }
  }
  private void getLayouts(Graphics g) {
    layouts = new ArrayList();
    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext frc = g2d.getFontRenderContext();
    AttributedString attrStr = new AttributedString(text);
    attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length());   
    LineBreakMeasurer measurer = new LineBreakMeasurer(
      attrStr.getIterator(), frc);
    float wrappingWidth;
    wrappingWidth = getSize().width - 15;
    while (measurer.getPosition() < text.length()) {
      TextLayout layout = measurer.nextLayout(wrappingWidth);
      layouts.add(layout);
    }
  }
  
}





Graphics2D: rotate(double theta)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    g.fillRect(0, 0, 20, 20);
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0);
    g2.scale(2.0, 2.0);
    g.setColor(Color.red);
    g.fillRect(0, 0, 20, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}





Graphics2D: scale(double sx, double sy)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    g.fillRect(0, 0, 20, 20);
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0);
    g2.scale(2.0, 2.0);
    g.setColor(Color.red);
    g.fillRect(0, 0, 20, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}





Graphics2D: setComposite(Composite comp)

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
    g2.setPaint(Color.red);
    g2.fill(r);
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
       .4f);
    g2.setComposite(c);

    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("www.jexp.ru", 25, 130);
  }
}





Graphics2D: setPaint(Paint paint)

import java.awt.Color;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Ellipse2D e = new Ellipse2D.Float(40, 40, 120, 120);
    GradientPaint gp = new GradientPaint(75, 75, Color.white,
        95, 95, Color.gray, true);
    g2.setPaint(gp);
    g2.fill(e);
  }
}





Graphics2D: setRenderingHint(Key hintKey, Object hintValue)

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);
    g2.drawString("jade", 40, 120); 
  }
}





Graphics2D: setRenderingHints(Map hints)

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Font f = new Font ("Arial", Font.PLAIN, 48);
    g2.setFont (f);
    g2.drawString ("www.jexp.ru", 10, 60);
    RenderingHints rh = g2.getRenderingHints ();
    rh.put (RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints (rh);
    g2.drawString ("Antialiased text", 10, 120);
  
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}





Graphics2D: setStroke(Stroke s)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  Shape shape;
  public MyCanvas() {
    shape = create();
  }
  protected Shape create() {
    float cm = 72 / 2.54f;
    return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,
        new float[] { 3, 1 }, 0);
    g2.setStroke(stroke);
    
    g2.setPaint(Color.black);
    g2.draw(shape);
    
  }
}





Graphics2D: transform(AffineTransform Tx)

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  Shape shape;
  public MyCanvas() {
    shape = create();
  }
  protected Shape create() {
    float cm = 72 / 2.54f;
    return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    g2.setPaint(Color.black);
    g2.draw(shape);
    
    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);
    g2.setPaint(Color.red);
    g2.draw(shape);
    
  }
}





Graphics2D: translate(int x, int y)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    g.fillRect(0, 0, 20, 20);
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0);
    g2.scale(2.0, 2.0);
    g.setColor(Color.red);
    g.fillRect(0, 0, 20, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}