我试图制作一个简单的程序,然后单击并显示图像,并沿着从起点到我点击的点的线移动。我似乎无法让图像沿着这条线移动,因为我遇到了斜坡问题。此外,我似乎无法以动画的方式让图片向前移动。有什么建议吗? (我排除了我的mousemotionlistener代码)
public class Screen extends JPanel implements Runnable {
public Thread thread = new Thread(this); //this is needed for a game loop (i think)
public static JLabel statusbar; //displays a status bar showing what mouse movements are taking place
private Image cat; //image of the cat
public int xCoord; //get the coordinates of the mouse pressed
public int yCoord;
public double xCoord2; //get the coordinates as the mouse is released
public double yCoord2;
public int yCoordMove;
public int xCoordMove;
public double slope;
public Screen(Frame frame) {
thread.start();
loadPic(); //calls the loadPic method above
Handlerclass handler = new Handlerclass(); //creates a new class to use the mouse motion listener
System.out.println("this mouse thing works!");
addMouseListener(handler);
addMouseMotionListener(handler);
statusbar = new JLabel("default");
add(statusbar);
}
public void run(){ //this is the game run loop
System.out.println("this is running");
try{
Thread.sleep(5); //sleeps the run loop after 5000 game seconds
} catch(Exception e) {}
}
public void loadPic(){ //loads the picture
cat = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\Moving Block Proj\\src\\MovingBlock\\catIcon1.png").getImage(); //gets the image
System.out.println("Image Loaded!");
}
@Override public void paintComponent(Graphics g){
super.paintComponent(g); //paints the component, the picture, on top
g.drawImage(cat, xCoord, yCoord, this); //draws the image and at a specific location
g.drawString("sup tho!", 250, 250); //draws the text to the screen
g.setColor(Color.black); //sets color to red
g.drawLine(xCoord, yCoord, xCoordMove, yCoordMove); //draws the clicked line
}
public void picMove(){
double numerator = yCoord2 - yCoord;
double denominator = xCoord2 - xCoord;
if(denominator == 0) {
denominator =.0001;
}
slope = (numerator/denominator);
System.out.printf("the slope is: %.2f \n\n ",slope);
}