import java.awt.*;
import java.awt.event.*;
public class Ex13_2 extends Frame implements Runnable
{
int currentX=0,currentY=0;
int startX=0,startY=0;
public static void main(String []args)
{
Ex13_2 workstart=new Ex13_2();
}
public Ex13_2()
{
super("Ex13_2");
setSize(350,350);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setVisible(true);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e)
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public void processMouseEvent(MouseEvent e)
{
if(e.getID()==MouseEvent.MOUSE_PRESSED)
{
startX=e.getX();
startY=e.getY();
}
if(e.getID()==MouseEvent.MOUSE_MOVED||e.getID()==MouseEvent.MOUSE_DRAGGED)
{
currentX=e.getX();
currentY=e.getY();
//repaint();
}
}
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(30);}
catch(InterruptedException e){;}
}
}
public void paint(Graphics g)
{
g.drawLine(startX,startY,currentX,currentY);
}
}
怎么改?
java 画线问题
答案:1 悬赏:80 手机版
解决时间 2021-03-20 09:12
- 提问者网友:藍了天白赴美
- 2021-03-19 13:11
最佳答案
- 五星知识达人网友:独钓一江月
- 2021-03-19 14:27
鼠标画线程序,我给你改了两处(见注释),现在运行没问题了,你看看吧。
import java.awt.*;
import java.awt.event.*;
public class Ex13_2 extends Frame implements Runnable
{
int currentX=0,currentY=0;
int startX=0,startY=0;
public static void main(String []args)
{
Ex13_2 workstart=new Ex13_2();
}
public Ex13_2()
{
super("Ex13_2");
setSize(350,350);
enableEvents(AWTEvent.MOUSE_EVENT_MASK );
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); //这里加了一句
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setVisible(true);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e)
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public void processMouseMotionEvent(MouseEvent e){ //这里加了一个函数并且把processMouseEvent函数中的鼠标移动判断移到这里
if(e.getID()==MouseEvent.MOUSE_MOVED || e.getID()==MouseEvent.MOUSE_DRAGGED)
{
currentX=e.getX();
currentY=e.getY();
}
}
public void processMouseEvent(MouseEvent e)
{
if(e.getID()==MouseEvent.MOUSE_PRESSED)
{
startX=e.getX();
startY=e.getY();
}
}
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(30);}
catch(InterruptedException e){;}
}
}
public void paint(Graphics g)
{
g.drawLine(startX,startY,currentX,currentY);
}
}
import java.awt.*;
import java.awt.event.*;
public class Ex13_2 extends Frame implements Runnable
{
int currentX=0,currentY=0;
int startX=0,startY=0;
public static void main(String []args)
{
Ex13_2 workstart=new Ex13_2();
}
public Ex13_2()
{
super("Ex13_2");
setSize(350,350);
enableEvents(AWTEvent.MOUSE_EVENT_MASK );
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); //这里加了一句
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
setVisible(true);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e)
{
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
public void processMouseMotionEvent(MouseEvent e){ //这里加了一个函数并且把processMouseEvent函数中的鼠标移动判断移到这里
if(e.getID()==MouseEvent.MOUSE_MOVED || e.getID()==MouseEvent.MOUSE_DRAGGED)
{
currentX=e.getX();
currentY=e.getY();
}
}
public void processMouseEvent(MouseEvent e)
{
if(e.getID()==MouseEvent.MOUSE_PRESSED)
{
startX=e.getX();
startY=e.getY();
}
}
public void run()
{
while(true)
{
repaint();
try{Thread.sleep(30);}
catch(InterruptedException e){;}
}
}
public void paint(Graphics g)
{
g.drawLine(startX,startY,currentX,currentY);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯