クリックすると画像が表示され、開始点からクリックした点までの線に沿って移動する簡単なプログラムを作成しようとしています。傾斜に問題があるため、画像が線に沿って移動できないようです。また、アニメーションのように写真を動かしたり、方向を変えたりすることができないようです。何かアドバイス?
(私が持っていた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);
}