Java Tutorial/2D Graphics/Graphics

Материал из Java эксперт
Версия от 15:25, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Demonstrate XOR mode.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class XORModePaintWithMouse extends JFrame {
  int chsX = 100, chsY = 100;
  public static void main(String[] a) {
    XORModePaintWithMouse f = new XORModePaintWithMouse();
    f.setSize(300, 300);
    f.setVisible(true);
  }
  XORModePaintWithMouse() {
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent me) {
        int x = me.getX();
        int y = me.getY();
        chsX = x - 10;
        chsY = y - 10;
        repaint();
      }
    });
  }
  public void paint(Graphics g) {
    g.drawLine(0, 0, 100, 100);
    g.drawLine(0, 100, 100, 0);
    g.setColor(Color.blue);
    g.drawLine(40, 25, 250, 180);
    g.drawLine(75, 90, 400, 400);
    g.setColor(Color.green);

    // xor cross hairs
    g.setXORMode(Color.black);
    g.drawLine(chsX - 10, chsY, chsX + 10, chsY);
    g.drawLine(chsX, chsY - 10, chsX, chsY + 10);
    g.setPaintMode();
  }
}





Draw 2D shape

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
public class Draw2DObjects extends JFrame {
  Shape shapes[] = new Shape[5];
  public static void main(String args[]) {
    Draw2DObjects app = new Draw2DObjects();
  }
  public Draw2DObjects() {
    add("Center", new MyCanvas());
    shapes[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
    shapes[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
    shapes[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
    GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0, 400.0, 150.0));
    path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
    shapes[3] = path;
    shapes[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0, 50.0, 25.0);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }
  class MyCanvas extends Canvas {
    public void paint(Graphics graphics) {
      Graphics2D g = (Graphics2D) graphics;
      for (int i = 0; i < shapes.length; ++i) {
        if (shapes[i] != null)
          g.draw(shapes[i]);
      }
    }
  }
}





Draw Arcs

import java.awt.Graphics;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public static void main(String[] a){
    MainClass f = new MainClass();
    f.setSize(300,300);
    f.setVisible(true);
  }
  
  
  public void paint(Graphics g) {
    g.drawArc(10, 40, 70, 70, 0, 75);
    g.fillArc(100, 40, 70, 70, 0, 75);
    g.drawArc(10, 100, 70, 80, 0, 175);
    g.fillArc(100, 100, 70, 90, 0, 270);
    g.drawArc(200, 80, 80, 80, 0, 180);
  }
}





Draw Ellipses

import java.applet.Applet;
import java.awt.Graphics;
public class MainClass extends Applet {
  public void paint(Graphics g) {
    g.drawOval(10, 10, 50, 50);
    g.fillOval(100, 10, 75, 50);
    g.drawOval(190, 10, 90, 30);
    g.fillOval(70, 90, 140, 100);
  }
}





Draw rectangles

import java.awt.Graphics;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public static void main(String[] a){
    MainClass f = new MainClass();
    f.setSize(300,300);
    f.setVisible(true);
  }
  
  
  public void paint(Graphics g) {
    g.drawRect(10, 10, 60, 50);
    g.fillRect(100, 10, 60, 50);
    g.drawRoundRect(190, 10, 60, 50, 15, 15);
    g.fillRoundRect(70, 90, 140, 100, 30, 40);
  }
}





Fill a solid three-dimensional rectangle

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.add(new MainClass());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  public void paint(Graphics g) {
    g.setColor (Color.yellow);  
    g.fill3DRect (5, 15, 50, 75, true); 
  }
}





Resizing output to fit the current size of a window.

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainClass extends Applet {
  final int inc = 25;
  int max = 500;
  int min = 200;
  Dimension d;
  public MainClass() {
    addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent me) {
        int w = (d.width + inc) > max ? min : (d.width + inc);
        int h = (d.height + inc) > max ? min : (d.height + inc);
        setSize(new Dimension(w, h));
      }
    });
  }
  public void paint(Graphics g) {
    d = getSize();
    g.drawLine(0, 0, d.width - 1, d.height - 1);
    g.drawLine(0, d.height - 1, d.width - 1, 0);
    g.drawRect(0, 0, d.width - 1, d.height - 1);
  }
}