我正在修改一段书面代码,并且想要使云图独立于MouseEvent移动。当前情况是这样的:当我移动鼠标时,云向左移动2个像素。但是,我希望云默认情况下向左移动2个像素,而MouseEvent不会成为其中的一部分。即使我将if else语句从方法中移出,它仍然遵循MouseEvent mouseMoved方法。需要修改“ cloudmove”以使其独立于鼠标移动。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Plane2 extends JPanel
{
boolean bLandingGearOut = false;
int planex=0; //a starting x location variant, this will adjust as the mouse moves
int planey=0; //a starting y location variant, this currently is just a placeholder for mouse movement in the y plane
int cloudx=900;
int cloudy=60;
int cloudmove=50;
boolean landingGear = false;
/*The constructer method sets some things up*/
public Plane2()
{
PolygonListener listener = new PolygonListener(); //create a polygon listener
addMouseListener(listener); //listen for mouse changes
addMouseMotionListener(listener); //listen for mouse movement
setBackground(Color.blue); //set the background of the JFrame to blue
setPreferredSize(new Dimension(1500,750)); //set default size of JFrame window
}
/*This method will be called every time we invoke repaint and it will redraw the graphics window*/
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.blue); //set color for the tires
//All you need to do is copy this text into your file and change the name of the two arrays
//If you also copy the last line (page.fillPolygon(xloc_changethisname,yloc....) make sure your
//Graphics object is called page and make sure you change the 3 array names inside the fillPolygon method
//********X*********
int[] xloc_plane = {planex+427,planex+426,planex+333,planex+324,planex+28,planex+30,planex+80,planex+90,planex+375,planex+414,planex+451,planex+456,planex+475,planex+470,planex+477,planex+478,planex+467,planex+405,planex+426};
//********Y*********
int[] yloc_plane = {planey+87,planey+89,planey+88,planey+81,planey+71,planey+67,planey+58,planey+53,planey+52,planey+39,planey+20,planey+16,planey+20,planey+54,planey+56,planey+66,planey+68,planey+74,planey+86};
page.setColor(Color.yellow); //set car color
page.fillPolygon(xloc_plane,yloc_plane,xloc_plane.length);
page.setColor(Color.black); //set color for the window
int[] xloc_window = {planex+83,planex+98,planex+101,planex+102,planex+102,planex+88,planex+83,planex+84};
int[] yloc_window = {planey+59,planey+59,planey+59,planey+56,planey+54,planey+54,planey+57,planey+58};
page.fillPolygon(xloc_window,yloc_window,xloc_window.length); //this does the actual drawing of the car window
page.setColor(Color.blue); //set color for the tires
page.fillOval(planex+126,planey+75,16,16); //front tire
page.fillOval(planex+275,planey+79,16,16); //back tire
cloudmove-=2; //every time mouse is moved it will move the cactus 2 pixels to left
if(bLandingGearOut == true)
{
paintGears(page);
}
else if(bLandingGearOut == false)
{
}
//All you need to do is copy this text into your file and change the name of the two arrays
//If you also copy the last line (page.fillPolygon(xloc_changethisname,yloc....) make sure your
//Graphics object is called page and make sure you change the 3 array names inside the fillPolygon method
//********X*********
int[] xloc_cloud = {cloudmove+cloudx+249,cloudmove+cloudx+263,cloudmove+cloudx+269,cloudmove+cloudx+273,cloudmove+cloudx+273,cloudmove+cloudx+266,cloudmove+cloudx+257,cloudmove+cloudx+245,cloudmove+cloudx+234,cloudmove+cloudx+224,cloudmove+cloudx+217,cloudmove+cloudx+215,cloudmove+cloudx+208,cloudmove+cloudx+204,cloudmove+cloudx+192,cloudmove+cloudx+187,cloudmove+cloudx+177,cloudmove+cloudx+173,cloudmove+cloudx+168,cloudmove+cloudx+164,cloudmove+cloudx+157,cloudmove+cloudx+155,cloudmove+cloudx+152,cloudmove+cloudx+146,cloudmove+cloudx+146,cloudmove+cloudx+137,cloudmove+cloudx+125,cloudmove+cloudx+112,cloudmove+cloudx+98,cloudmove+cloudx+82,cloudmove+cloudx+72,cloudmove+cloudx+70,cloudmove+cloudx+56,cloudmove+cloudx+46,cloudmove+cloudx+35,cloudmove+cloudx+25,cloudmove+cloudx+21,cloudmove+cloudx+12,cloudmove+cloudx+12,cloudmove+cloudx+12,cloudmove+cloudx+12,cloudmove+cloudx+17,cloudmove+cloudx+18,cloudmove+cloudx+24,cloudmove+cloudx+30,cloudmove+cloudx+38,cloudmove+cloudx+44,cloudmove+cloudx+51,cloudmove+cloudx+60,cloudmove+cloudx+60,cloudmove+cloudx+62,cloudmove+cloudx+64,cloudmove+cloudx+64,cloudmove+cloudx+75,cloudmove+cloudx+79,cloudmove+cloudx+79,cloudmove+cloudx+82,cloudmove+cloudx+87,cloudmove+cloudx+93,cloudmove+cloudx+102,cloudmove+cloudx+106,cloudmove+cloudx+111,cloudmove+cloudx+121,cloudmove+cloudx+125,cloudmove+cloudx+126,cloudmove+cloudx+128,cloudmove+cloudx+138,cloudmove+cloudx+139,cloudmove+cloudx+146,cloudmove+cloudx+156,cloudmove+cloudx+165,cloudmove+cloudx+171,cloudmove+cloudx+174,cloudmove+cloudx+180,cloudmove+cloudx+189,cloudmove+cloudx+196,cloudmove+cloudx+206,cloudmove+cloudx+215,cloudmove+cloudx+219,cloudmove+cloudx+222,cloudmove+cloudx+227,cloudmove+cloudx+232,cloudmove+cloudx+233,cloudmove+cloudx+235,cloudmove+cloudx+236,cloudmove+cloudx+237,cloudmove+cloudx+239,cloudmove+cloudx+239,cloudmove+cloudx+249,cloudmove+cloudx+254};
//********Y*********
int[] yloc_cloud = {cloudy+53,cloudy+61,cloudy+74,cloudy+98,cloudy+113,cloudy+125,cloudy+129,cloudy+138,cloudy+139,cloudy+141,cloudy+139,cloudy+143,cloudy+148,cloudy+152,cloudy+156,cloudy+158,cloudy+159,cloudy+159,cloudy+159,cloudy+158,cloudy+154,cloudy+153,cloudy+151,cloudy+146,cloudy+150,cloudy+152,cloudy+154,cloudy+154,cloudy+154,cloudy+153,cloudy+147,cloudy+145,cloudy+148,cloudy+148,cloudy+142,cloudy+134,cloudy+131,cloudy+120,cloudy+114,cloudy+108,cloudy+103,cloudy+96,cloudy+91,cloudy+83,cloudy+79,cloudy+77,cloudy+72,cloudy+72,cloudy+69,cloudy+69,cloudy+65,cloudy+58,cloudy+56,cloudy+54,cloudy+51,cloudy+49,cloudy+44,cloudy+42,cloudy+39,cloudy+35,cloudy+33,cloudy+33,cloudy+34,cloudy+35,cloudy+35,cloudy+35,cloudy+24,cloudy+14,cloudy+12,cloudy+9,cloudy+4,cloudy+1,cloudy+0,cloudy+0,cloudy+0,cloudy+2,cloudy+5,cloudy+11,cloudy+19,cloudy+20,cloudy+22,cloudy+27,cloudy+32,cloudy+36,cloudy+41,cloudy+45,cloudy+49,cloudy+49,cloudy+54,cloudy+55};
page.setColor(Color.white); //set color for the cactus
page.fillPolygon(xloc_cloud,yloc_cloud,xloc_cloud.length); //this does the actual drawing of the cactus
} //end paintComponent
这是paintComponent的完整代码。 PolygonListener的完整代码在这里:
private class PolygonListener implements MouseListener, MouseMotionListener
{
public void mousePressed(MouseEvent event){} //unused event
public void mouseDragged(MouseEvent event){} //unused event
//The next method will determine what to do when a mouse button is clicked
//In this case, a left click will downshift and a rightclick will upshift
public void mouseClicked(MouseEvent event){
if (event.getButton() == MouseEvent.BUTTON1)
{
System.out.println("landing gear activated");
bLandingGearOut = true;
}
else if (event.getButton() == MouseEvent.BUTTON3)
{
System.out.println("landing gear retracted");
bLandingGearOut = false;
}
repaint();
} //end mouseClicked
public void mouseReleased(MouseEvent event) {} //unused event
public void mouseEntered(MouseEvent event) {} //unused event
public void mouseExited(MouseEvent event) {} //unused event
//The next method will determine what to do when a mouse is moved
//In this case that affects the speed and rpms of the car
public void mouseMoved(MouseEvent event)
{
// if (cloudmove+cloudx>2)
// cloudmove-=2; //every time mouse is moved it will move the cactus 2 pixels to left
// else
// cloudmove=1000; //resets the location once cactus "drifts" off screen
System.out.println ("Current Location: "+event.getPoint()); //reports current x|y location on screen
planex=event.getPoint().x; //grab the current mouse location and set planex to that location, this will allow the car to move in relation to where the mouse is moving
planey=event.getPoint().y;
repaint(); //redraw the image - this automatically calls the paintComponent method to redraw the frame
} //end mouseMoved
} //end PolygonListener
主要方法:
import javax.swing.*;
public class Flyit2
{
public static void main(String[] args)
{
JFrame frame=new JFrame("Flying Plane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Plane2 plane = new Plane2();
frame.getContentPane().add(plane);
frame.pack();
frame.setVisible(true);
}
}
如前所述,cloudmove整数不会将云向左移动两个像素。取而代之的是,要求用户移动鼠标才能使其工作。我希望云能够自行移动,而无需依赖用户的鼠标移动。如何实现?
答案 0 :(得分:1)
我希望云自行移动,而无需依赖用户的鼠标移动。
云移动的原因是因为您在MouseListener中调用了repaint()。
在绘画方法中,您将拥有:
cloudmove-=2;
每次绘制组件时执行。
此代码是错误的。绘制方法永远不要更新组件的状态。从paintComponent()方法中删除以上语句。
如果您希望对Clood进行动画处理,则需要使用Swing Timer
。然后,当Timer
触发您的ActionListener
的代码时
Timer
将被执行。 ActionListener
的代码很简单:
cloudmove -= 2;
repaint();
阅读How to Use Swing Timers的Swing教程中的部分,以获得更多信息。
您还可以签出:Program freezes during Thread.sleep() and with Timer,以获取使用Timer
的简单示例。