java代码实现小人跳舞 java绘制一个小人( 二 )


MyThread mt=new MyThread(g,drawpanel);//新建一个监听器,并把参数传递进去
drawpanel.addMouseMotionListener(mt);//为画图面板添加监听对象
Thread r=new Thread(mt);
r.start();//启动线程
}
}
package s0124随鼠标动眼睛;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import javax.swing.plaf.SliderUI;
public class MyThread implements Runnable,MouseMotionListener{
int x;
int y;
Graphics g;
JPanel drawpanel;
public MyThread(Graphics g,JPanel drawpanel)
{
this.g=g;
this.drawpanel=drawpanel;
}
public void run()
{
while(true)
{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.clearRect(0, 0, 600, 600);
g.drawOval(100, 100, 100, 100);
g.drawOval(300, 100, 100, 100);
//画左眼球
int x1=150+(x-150)/5;
int y1=150+(y-150)/5;
g.fillOval(x1-15,y1-15, 30,30);
//画右眼球
int x2=350+(x-350)/5;
int y2=150+(y-150)/5;
g.fillOval(x2-15,y2-15, 30,30);
g.drawLine(200, 300, 250, 250);
g.drawLine(250, 250,300,300);
g.drawLine(150, 350, 350, 350);
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
x=e.getX();
y=e.getY();
}
}
关于java代码实现小人跳舞和java绘制一个小人的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读