0

Im trying to make a simple program where I click and an image appears and moves along the line from the starting point to the point where I clicked. I cant seem to get the image to travel along the line because I am getting problems with the slope. Also, I cannot seem to get the picture to move along and direction in an animated fashion. Any advice?
(i excluded mousemotionlistener code i had)

      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);    

}
Cameron Roberson
  • 111
  • 4
  • 20
  • Take a look at [this example](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184) it basically generates a random line (start point and end point) for an image to travel along, angles the image accordingly and animates it along the path...lots of fun for the whole family. – MadProgrammer Apr 22 '13 at 03:00
  • That example seems a little complex for me as I am just beginning. Do you know of any others that may help? – Cameron Roberson Apr 22 '13 at 03:33

1 Answers1

0
   double a=Math.atan2(numerator, denominator);

    double rate=100;   //rate at which block will move
    xCoord=(int) (xCoord + rate* Math.cos(a));
    yCoord=(int) (yCoord + rate* Math.sin(a));

Result:

enter image description here

kingAm
  • 1,755
  • 1
  • 13
  • 23