⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iguana.java

📁 learning java的源代码。书中每个实例都有相关的代码example。
💻 JAVA
字号:
//file: Iguana.javaimport java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;public class Iguana extends JComponent {  private Image image;  private int theta;  public Iguana(  ) {    image = Toolkit.getDefaultToolkit(  ).getImage(        "Piazza di Spagna.small.jpg");    theta = 0;    addMouseListener(new MouseAdapter(  ) {      public void mousePressed(MouseEvent me) {        theta = (theta + 15) % 360;        repaint(  );      }    });  }  public void paint(Graphics g) {    Graphics2D g2 = (Graphics2D)g;    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,        RenderingHints.VALUE_ANTIALIAS_ON);    int cx = getSize(  ).width / 2;    int cy = getSize(  ).height / 2;    g2.translate(cx, cy);    g2.rotate(theta * Math.PI / 180);    Shape oldClip = g2.getClip(  );    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);    g2.clip(e);    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);    g2.setPaint(new GradientPaint(40, 40, Color.blue,        60, 50, Color.white, true));    g2.fill(c);    g2.setPaint(Color.yellow);    g2.fillOval(cx / 4, 0, cx, cy);    g2.setClip(oldClip);    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));    g2.setPaint(new GradientPaint(-cx, 0, Color.red,        cx, 0, Color.black, false));    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);    AlphaComposite ac = AlphaComposite.getInstance(        AlphaComposite.SRC_OVER, (float).75);    g2.setComposite(ac);    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4,        cx * 3 / 4, cy * 3 / 4, 20, 20);    g2.setStroke(new BasicStroke(4));    g2.setPaint(Color.magenta);    g2.fill(r);    g2.setPaint(Color.green);    g2.draw(r);    g2.drawImage(image, -cx / 2, -cy / 2, this);  }  public static void main(String[] args) {    JFrame f = new JFrame("Iguana");    Container c = f.getContentPane(  );    c.setLayout(new BorderLayout(  ));    c.add(new Iguana(  ), BorderLayout.CENTER);    f.setSize(300, 300);    f.setLocation(100, 100);    f.addWindowListener(new WindowAdapter(  ) {      public void windowClosing(WindowEvent e) { System.exit(0); }    });    f.setVisible(true);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -