redsuner
发贴: 9
积分: 0
|
于 2004-10-29 10:38
package java2ddemo;
import java.awt.*; import java.awt.geom.*; import java.awt.geom.Ellipse2D; import java.awt.event.*; import javax.swing.*;
public class Java2DFrame extends JFrame{ JPanel contentPane; GeneralPath rect = new GeneralPath(GeneralPath.WIND_EVEN_ODD); JButton jButton1 = new JButton();
//Construct the frame public Java2DFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.cyan); rect.moveTo(20,20); rect.lineTo(120,20); rect.lineTo(120,20); rect.lineTo(120,60); rect.lineTo(20,60); rect.closePath(); g2d.fill(rect); g2d.setColor(Color.blue); Ellipse2D ellipse = new Ellipse2D.Float(100,100,300,300); //g2d.fill(ellipse); //g2d.draw(ellipse); MyEllipse ell2 = new MyEllipse(100,100,300,300); g2d.fill(ell2); g2d.draw(ell2); ell2.addMouseListener(new MyEllipseListener());
} //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); jButton1.setBounds(new Rectangle(177, 147, 71, 33)); jButton1.setText("jButton1"); contentPane.setLayout(null); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); contentPane.add(jButton1, null); ActionListener listener = new C2DListener();
} //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent; if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } class C2DListener implements ActionListener { public void actionPerformed(ActionEvent e){ if(e.getSource() == "rect"){ System.out.println("2D recall sucessful!"); } if(e.getSource() == "jButton1"){ System.out.println("Button recall sucessful!"); }
}
} class MyEllipse extends Ellipse2D.Float{ public MyEllipse(float x1,float x2,float y1,float y2){ super(x1,x2,y1,y2); } public MyEllipse createEllipse(float x1,float x2,float y1,float y2){ Ellipse2D ell = new Ellipse2D.Float(x1,x2,y1,y2); return (MyEllipse)ell;
} public void addMouseListener(MouseListener listener){
} } class MyEllipseListener implements MouseListener{ public void mouseClicked(MouseEvent e){ System.out.println("Button recall sucessful!");
}
public void mouseEntered(MouseEvent e) {
}
public void mousePressed(MouseEvent e) { System.out.println("Button recall sucessful!");
}
public void mouseExited(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
}
}
|