Note that there are some explanatory texts on larger screens.

plurals
  1. POmaking an image move along a line in java
    primarykey
    data
    text
    <p>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?<br> (i excluded mousemotionlistener code i had)</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload