计算机图形学来日再战:(先立一个flag)
package com.java2d; /** * 实现进行几何变换的圆与字符 * @author Administrator */ import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JPanel; public class Test1 extends JApplet { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Hello 2D"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new Test1(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); } // 重写初始化函数 public void init() { JPanel panel = new Hello(); getContentPane().add(panel); } // 定义一个hello类 class Hello extends JPanel { public Hello() { setPreferredSize(new Dimension(640, 480)); } // 重写组件绘制函数 public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.blue); Ellipse2D e = new Ellipse2D.Double(-100, -50, 200, 100); AffineTransform tr = new AffineTransform(); tr.rotate(Math.PI / 6.0);// 设置旋转变换 Shape shape = tr.createTransformedShape(e); g2.translate(300, 300); g2.scale(2, 2); g2.draw(shape); g2.drawString("Hello 2D", 0, 0); } } }
修改后:
package com.java2d; /** * 实现进行几何变换的圆与字符 * @author Administrator */ import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JPanel; public class Test1 extends JApplet { public static void main(String[] args) { JApplet applet = new Test1(); } public Test1(){ JFrame frame = new JFrame(); frame.setTitle("Hello 2D"); this.init(); frame.getContentPane().add(this); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 重写初始化函数 public void init() { JPanel panel = new Hello(); getContentPane().add(panel); } // 定义一个hello类 class Hello extends JPanel { public Hello() { setPreferredSize(new Dimension(640, 480)); } // 重写组件绘制函数 public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.blue); Ellipse2D e = new Ellipse2D.Double(-100, -50, 200, 100); AffineTransform tr = new AffineTransform(); tr.rotate(Math.PI / 6.0);// 设置旋转变换 Shape shape = tr.createTransformedShape(e); g2.translate(300, 300); g2.scale(2, 2); g2.draw(shape); g2.drawString("Hello 2D", 0, 0); } } }