Я изменяю часть написанного кода и хочу, чтобы рисование в облаке двигалось независимо от 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);
}
}
Целочисленное целое, как было сказано выше, просто не перемещает облако на два пикселя влево.Вместо этого пользователю необходимо переместить мышь, чтобы это работало.Я хочу, чтобы облако перемещалось само по себе, без необходимости полагаться на движения мыши пользователя.Как этого достичь?