0

im trying to code a GUI for "Towers of Hanoi" with a Java JFrame. I thought I can just make a array with all rectangles (I use rectangles to display the elements, that the player can move) and then use something like this "recList[1].move()"to move the rectangle on the canvas, but I don't finde a funktion, that can do this. It would be grade, if somebody could say me how i can move a rectangle to specific coordinates.

Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class GUIBuilder extends JFrame {
    //object classes
    public static class Rectangle {
        Rectangle2D.Double Rectangle;

        public Rectangle(double x, double y, double w, double h) {
            Rectangle = new Rectangle2D.Double(x, y, w, h);
        }

        public void draw(Graphics2D g) {
            g.draw(Rectangle);
        }
    }

    public static class Stick {
        Line2D.Double stick;
        public Stick(double x1, double y1, double x2, double y2) {
            this.stick = new Line2D.Double(x1, y1, x2, y2);
        }

        public void draw(Graphics2D g) {
            g.draw(stick);
        }
    }

    //draw
    static class DrawPane extends JPanel {
        public void paintComponent(Graphics g) {
            double s = 3; // s = amount of sticks
            double n = 5; // n = amount of elements
            //base
            Rectangle rectangle = new Rectangle(300, 700, (int) s * 100 + 100, 50);
            rectangle.draw((Graphics2D)g.create());
            //s sticks
            for (int i=0; i<s; i++) {
                Stick stick = new Stick(400 + 100 * i, 700 - (25 * (n + 1)), 400 + 100 * i, 700);
                stick.draw((Graphics2D)g.create());
            }
            //n elements
            Rectangle[] recList = new Rectangle[(int)n]; //declaration a list for the rectangles
            for (int i=0;i<n;i++) {
                double w = 90/n*(n-i);
                double x = 355+90/n/2*i;
                double y = 675-25*i;
                double h = 25;
                recList[i] = new Rectangle(x, y, w, h);
                recList[i].draw((Graphics2D)g.create());
            }
        }
    }
    //guibuilder
    public GUIBuilder() {
        JFrame frame = new JFrame("Towers of Hanoi by ByPander");
        frame.setContentPane(new DrawPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
    //calling the guibuilder
    public static void main(String[] args){
        //start the Guibuilder
        new GUIBuilder();
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The `shape` package I put together years ago may be useful for what you are doing: https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/ The idea is to treat shapes of any kind as 1st class components (ie draggable, clickable, etc). You can see/run the example in AreaManager – ControlAltDel Sep 06 '22 at 18:08
  • The other thing I'd recommend is looking into JOGL. It is much easier to do custom animations in OGL than in Swing – ControlAltDel Sep 06 '22 at 18:09
  • 1
    There is no need to continually create a Graphics object using the create() method. You only need to create the object once and use the same Graphics object for all further painting. Your current code is wasting resources by continually creating new instances. Also, I'm not sure why you are creating new instances of all your Rectangles. A painting class should NOT create new objects, only paint the current state of the objects in your class. – camickr Sep 06 '22 at 23:14
  • *how i can move a rectangle to specific coordinates.* - you change the coordinates of the Rectangle then simple paint the rectangle. So typically you might keep an ArrayList of objects you want to paint. Then in the paintComponent() method you iterate through the List and paint each object. Maybe this example will help: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681. Your code should be simplier since you don't need the animation. – camickr Sep 06 '22 at 23:16

0 Answers0