0

I am very new to JAVA and i am just a hobbyist,Just recently I have been following some tutorials on making UI and all thee looking in to learn about this, any code snippets are welcome but also more what is this function / process called so i can look deeper into different parts of swing from the ORACLE DOCS which is really informative, I am still working my way through the components.

I do not have an overall goal at present and am just finding my feet, but i want to try and make a small top-down 2d maze game to learn.

One part got my attention and it was this demo at the bottom of the page https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html

Basically this is the code and I am trying to make it so when I click inside the frame the rectangle moves slowly towards the point of the mouse click rather than jumping to it (and later on if possible to click in another location and it would change its course before reaching its destination.

What kind of area do i need to be looking in to learn about this, any code snippets are welcome but also more what is this function / process called so i can look deeper into it.

WORKING DEMO CODE BELOW

package painting;

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class SwingPaintDemo3 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() { I am Trying to make it so when I click inside the 
            public void run() {
                createAndShowGUI(); 
            }
        });
    }

    private static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? "+
        SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    } 
}

class MyPanel extends JPanel {

    private int squareX = 50;
    private int squareY = 50;
    private int squareW = 20;
    private int squareH = 20;

    public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });

    }

    private void moveSquare(int x, int y) {
        int OFFSET = 1;
        if ((squareX!=x) || (squareY!=y)) {
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
            squareX=x;
            squareY=y;
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        } 
    }


    public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);       
        g.drawString("This is my custom Panel!",10,20);
        g.setColor(Color.RED);
        g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawRect(squareX,squareY,squareW,squareH);
    }  
}

Thanks for any advice ;)

stay-
  • 3
  • 2
  • 1
    You want to do "animation". Animation is done by using a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). You can check out: https://stackoverflow.com/a/54028681/131872. The animation is relatively simple because the change in x/y location is fixed for each ball as it moves around the screen. – camickr Sep 23 '19 at 00:26
  • In your case you would need to calculate the path between the current location and the mouse point. So maybe store the points in an ArrayList and then start the Timer. Then when the Timer fires you 1) get the next location from element 0, 2) move the square 3) remove element 0 from the ArrayList. Then when the ArrayList is empty you stop the Timer. *possible to click in another location and it would change its course* - clear the ArrayList entries and then add new locations based the current location and new mouse point. – camickr Sep 23 '19 at 00:26
  • @camickr Thankyou for your input this helps massively i was trying to find something as a reference but was struggling to search due to been a newb. – stay- Sep 23 '19 at 00:47
  • I am familiar with arrays as i have some introductory experience in pascal and python from some books i bought, so now know i can look towards rebuilding this knowledge for JAVA – stay- Sep 23 '19 at 00:49

0 Answers0