Topic: [求助]Graphics2D 类中的对象可以加监听事件吗? |
Print this page |
1.[求助]Graphics2D 类中的对象可以加监听事件吗? | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-27 11:19 [求助]Graphics2D 类中的对象可以加监听事件吗? 先谢谢各位大虾! |
2.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-27 11:20 由于监听器是interface,按道理讲,加在哪里都是可以的! |
3.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-27 11:30 比如说我先画一个矩形,然后想获得对矩形的操作事件,可是rect这个对象,没有像JButton之类的add***Listener()的方法! 能不能给我个例程什么的?3Q GeneralPath rect = new GeneralPath(GeneralPath.WIND_EVEN_ODD); 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); |
4.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-27 11:38 写一个extends GeneralPath 的类,添加那些add***Listener()进去,当lineTo或者closePath是fire这些事件。 |
5.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-27 11:41 let me try! |
6.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-28 11:00 出不来啊!郁闷 |
7.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-28 11:01 redsuner wrote: 什么叫出不来?贴出代码看看 |
8.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: kavinwang] | Copy to clipboard |
Posted by: redsuner Posted on: 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) { } } |
9.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-29 10:52
然后你的程序实现LineToListener 接口,并实现update方法,在update方法中更新显示。记住,要调用MyGeneralPath .addLineTolistener(this)把实现类添加到侦听器列表中。 如果你不太明白的话,你把全部的代码发给我。 |
10.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-29 11:00 我想要实现的就是画一个矩形,或者圆,然后我能用鼠标对我画的这个矩形,或者圆进行拖动,单击等事件操作,所以想到了给这些2D对象加监视器的想法,上次我的那段程序,就是想给一个圆,加上鼠标响应 没、,就是不行,你能帮我完善一下不,谢谢你了! |
11.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-29 11:02 我那个简单的程序Demo Java2DDemo.rar (21.91k) |
12.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-29 11:10 越来越复杂了.东西有点多,我大致讲一下。 下面的鼠标事件应该添加到你的画板上,比如JPanel,jScrollPane中。 1,监控鼠标的点击事件,检查鼠标的点击位置 2,判断点击位置是否有东西在下面。 3、如果有的话,设定将有拖动的对象到临时存储区。 4,在mouseMotion事件中改变拖动的对象的位置。 5,当鼠标拖动完成后,更新拖动对象的位置,并刷新显示。 上面的过程你可进行加工,比如拖动是显示拖动对象的外观,只在拖动完成时才重新绘制。 还有你可以参考java中DND的文档,实现起来可能更加有意思。 |
13.Re:[求助]Graphics2D 类中的对象可以加监听事件吗? [Re: redsuner] | Copy to clipboard |
Posted by: redsuner Posted on: 2004-10-29 11:25 恩,你说的第2点判断点击位置是否有东西,如果实现算法比较好,我所做的可能所画的圆,矩形比较多的, 和第3点临时存储区是怎么使用的 DND文档还没看过,我先找找看 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |