用JAVA实现了控制小车的速度和方向,但是想把用KeyEvent控制变成鼠标点击button控制,有没有大神帮忙改下
importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjava.awt.geom.*;impor...
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.Timer;
import javax.swing.*;
class TestPanel extends JPanel
{
Timer myTimer;
int wait, wd, ht;
Robot robot;
double simtime = 0.0;
int x=210,y=210,velL=6,velR=6,stage=0;
TestPanel()
{
robot = new Robot(210,210,6,-6,Color.BLUE);
}
public class ShortTest2 extends JFrame implements ActionListener {
public void keyPressed(KeyEvent e){
int c=e.getKeyCode();
if (c==KeyEvent.VK_UP)
{
velR++;
}
if (c==KeyEvent.VK_DOWN)
{
velR--;
}
if (c==KeyEvent.VK_W)
{
velL++;
}
if (c==KeyEvent.VK_S)
{
velL--;
}
System.out.println("vR: "+ velR+"vL:"+velL);
}*/
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void update()
{
wd = getWidth();
ht = getHeight();
double tstep, tmore;
//tmore = 1.0;
robot.move(0.05);
simtime = simtime + 0.05;
//robot.setPose(x,y,0);
robot.setVelocity(velR,velL);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
robot.draw(g);
g.setColor(Color.RED);
int n;
for (n=0; n<9; n++) {
g.drawLine(10,10+n*50,410,10+n*50);
g.drawLine(10+n*50,10,10+n*50,410);
}
}
public void startAnimation()
{
if (myTimer == null)
{
myTimer = new Timer(20,new TimerHandler() );
myTimer.start();
}
else if (!myTimer.isRunning()) myTimer.restart();
}
public void stopAnimation()
{
myTimer.stop();
}
private class TimerHandler implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
update();
}
}
}
public class motion extends JApplet
{
TestPanel panel;
public void init()
{
try {
EventQueue.invokeAndWait(new Runnable() {
public void run()
{
panel = new TestPanel();
add(panel);
}
});
}
catch (Exception e) {
}
}
public void start() {
panel.startAnimation();
}
public void stop() {
panel.stopAnimation();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
TestPanel panel = new TestPanel();
JFrame frame = new JFrame("Robot Motion");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(440,460);
frame.setVisible(true);
panel.startAnimation();
}
});
}
ShortTest2 m = new ShortTest2();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(600, 370);
m.setLocationRelativeTo(null);
m.setVisible(true); }
}
机器人的设计再另一个程序里,字数太多,发不出来了,可私信
程序有误,最后多了些东西,大神有意向请私信!! 展开
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.Timer;
import javax.swing.*;
class TestPanel extends JPanel
{
Timer myTimer;
int wait, wd, ht;
Robot robot;
double simtime = 0.0;
int x=210,y=210,velL=6,velR=6,stage=0;
TestPanel()
{
robot = new Robot(210,210,6,-6,Color.BLUE);
}
public class ShortTest2 extends JFrame implements ActionListener {
public void keyPressed(KeyEvent e){
int c=e.getKeyCode();
if (c==KeyEvent.VK_UP)
{
velR++;
}
if (c==KeyEvent.VK_DOWN)
{
velR--;
}
if (c==KeyEvent.VK_W)
{
velL++;
}
if (c==KeyEvent.VK_S)
{
velL--;
}
System.out.println("vR: "+ velR+"vL:"+velL);
}*/
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void update()
{
wd = getWidth();
ht = getHeight();
double tstep, tmore;
//tmore = 1.0;
robot.move(0.05);
simtime = simtime + 0.05;
//robot.setPose(x,y,0);
robot.setVelocity(velR,velL);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
robot.draw(g);
g.setColor(Color.RED);
int n;
for (n=0; n<9; n++) {
g.drawLine(10,10+n*50,410,10+n*50);
g.drawLine(10+n*50,10,10+n*50,410);
}
}
public void startAnimation()
{
if (myTimer == null)
{
myTimer = new Timer(20,new TimerHandler() );
myTimer.start();
}
else if (!myTimer.isRunning()) myTimer.restart();
}
public void stopAnimation()
{
myTimer.stop();
}
private class TimerHandler implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
update();
}
}
}
public class motion extends JApplet
{
TestPanel panel;
public void init()
{
try {
EventQueue.invokeAndWait(new Runnable() {
public void run()
{
panel = new TestPanel();
add(panel);
}
});
}
catch (Exception e) {
}
}
public void start() {
panel.startAnimation();
}
public void stop() {
panel.stopAnimation();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
TestPanel panel = new TestPanel();
JFrame frame = new JFrame("Robot Motion");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(440,460);
frame.setVisible(true);
panel.startAnimation();
}
});
}
ShortTest2 m = new ShortTest2();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(600, 370);
m.setLocationRelativeTo(null);
m.setVisible(true); }
}
机器人的设计再另一个程序里,字数太多,发不出来了,可私信
程序有误,最后多了些东西,大神有意向请私信!! 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询