问答题

本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击一下,在单击的地方就会出现一个坐标圆点,用鼠标在另外一个地方单击一下又会出现另外一个圆点,并且此时在两个坐标圆点间画出一条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外一条直线。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventQueuePanel extends JPanel implements ActionListener
EventQueuePanel()
JButton button=new JButton("Draw line");
add(button);
button.addActionListener(this);
public void actionPerformed(AclionEvent evt)
Graphics g=getGraphics();
______p=getClick();
g.drawOval(p.x-2,p.y-2,4,4);
Point q=getClick();
g.drawOval(q.x-2,q.y-2,4,4);
g.drawLine(p.x,p.y,q.x,q.y);
g.dispose();
public Point getClick()
EventQueue eq=Toolkit.getDefaultToolkit().getSystemEventQueue();
while(true)
try
AWTEvent evt=eq.getNextEvent();
if(evt.getID()==MouseEvent.MOUSE PRESSED)
MouseEvent mevt=(MouseEvent)evt;
Point p=______();
Point top=getRootPane().getLoeation()______
p.x-=top.x;
p.y-=top.y;
return p;

catch(InterruptedException e)

private int y=60;
class EventQueueFrame extends JFrame
public EventQueueFrame()
setTitle("java2");
setSize(300,200);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
Container contentPane=getContentPane();
contentPane.add(new EventQueuePanel());

public class java2
public static void main(String[]args)
Frame frame=new EventQueueFrame();
frame.show();

【参考答案】

第1处:Point
第2处:nevt.getPoint
热门 试题

问答题
本题是一个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的一个,按住鼠标左键或右键在而板中拖动,就能画出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。 import java.applet.*; import java.awt.*; import java.awt.event.*; public class java3 extends Applet implements ActionListener int x=-1,y=-1,rubberNote=0,clearNote=0; Color c=new Color(255,0,0); int con=3; Button b_red,b_blue,b_green,b_clear,b_quit; public void init() addMouseMotionListener(this); b_red=new Button( 画红色图形 ); b_blue=new Button( 画蓝色图形 ); b_green=new Button( 画绿色图形 ); b_quit=new Button( 橡皮 ); b_clear=new Button( 清除 ); add(b_red); add(b_green); add(b_blue); add(b_quit); add(b_clear); b_red.addActionListener(this); b_green.addActionListener(this); b_blue.addActionListener(this); b_quit.addActionListener(this); b_clear.addActionListener(this); public void paint() if(x!=-1&&y!=-1&&rubberNote==0&&clearNote==0) g.setColor(c); g.fillOval(x,y,con,con); else if(rubberNote==1&&clearNote==0) g.clearRect(x,y,10,10); else if(clearNote==1&&rubberNote==0) (g.clearRect(0,0,getSize().width,getSize().height); public void mouseDragged(MouseEvent e) x=(int)e.getX();y=(int)e.getY();repaint(); public void mouseMoved(MouseEvent e) public void update(Graphics g) paint(g); public void actionPerformed(Event e) if(e.getSource()==b_red) rubberNote=0;clearNote=0;c=new Color(255,0,0); else if(e.getSource()==b_green) rubberNote=0;clearNote=0;c=new Color(0,255,0); else if(e.getSource()==b blue) rubberNote=0;clearNote=0;c=new Color(0,0,255); if(e.getSource()==b_quit) rubberNote=1;clearNote=0; if(e.getSource()==b_clear) clearNote=1;rubberNote=0;repaint();