Question

So what I want to achieve, is to create a simple program that lets the user create rectangles on the screen and then move them around.

I know that I can declare a new object in the code (i.e rectangle i = new rectangle(x,y,sizex,sizey)) however that will create only one, moreover it forces me to declare it in the code: block1 = new block block2 = new block etc

Question is: How can I let the user create infinite rectangles with the use of lets say a button (not necessarily a button, it can be anything) and then let the user be capable of modyfing them (location/size etc).

Examples would be nice. I just feel there is a better way than declaring a gazillion objects in the code and then displaying them one by one. In C++ i could declare a malloc expandable container that would just hold the values, and then just display things using these values, not sure for java.

Simple code for reference:

public static void main(String[] args){

    JFrame frame = new JFrame("A.L.T.E.S");
    //Container container = frame.getContentPane();
    frame.setSize(1024, 768);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Block object = new Block(10,10,20,20);
    frame.add(object);

    object.reDraw();
    }


public class Block extends JPanel{

int yPos;
int xPos;
int xSize;
int ySize;

public Block(int xPos, int yPos, int xSize, int ySize){
    this.xPos = xPos;
    this.yPos = yPos;
    this.xSize = xSize;
    this.ySize = ySize;
}

public void setYPos(int yPos){
    this.yPos = yPos;
}

public void setXPos(int xPos){
    this.xPos = xPos;
}

public void setXSize(int xSize){
    this.xSize = xSize;
}

public void setYSize(int ySize){
    this.ySize = ySize;
}

public int getYPos(){
    return yPos;
}

public int getXPos(){
    return xPos;
}

public int getYSize(){
    return ySize;
}

public int getXSize(){
    return xSize;
}

public void reDraw(){
    repaint();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(xPos, yPos, xSize, ySize);
}
}
Was it helpful?

Solution

Nothing in Java is simple.

First, Java already has a Rectangle class that holds the origin and size of a Rectangle. In your code, you're making all the rectangles blue. Suppose you want the user to set the color of a Rectangle. You can define your Block class like this.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Block {

    private Color       color;

    private Rectangle   rectangle;

    public Block(int x, int y, int width, int height) {
        this(new Rectangle(x, y, width, height));
    }

    public Block(Rectangle rectangle) {
        this.rectangle = rectangle;
        this.color = Color.BLUE;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Rectangle getRectangle() {
        return rectangle;
    }

    public void draw(Graphics g) {
        g.setColor(getColor());
        g.fillRect(rectangle.x, rectangle.y, 
                rectangle.width, rectangle.height);
    }
}

Next, you need a model class to hold all of the Blocks that your user defines.

Something like this BlockList class.

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

public class BlockList {

    private List<Block> blockList;

    public BlockList() {
        this.blockList = new ArrayList<Block>();
    }

    public void init() {
        this.blockList.clear();
    }

    public void addBlock(Block block) {
        this.blockList.add(block);
    }

    public void draw(Graphics g) {
        for (int i = 0; i < blockList.size(); i++) {
            blockList.get(i).draw(g);
        }
    }

}

Now that you have your GUI model defined, you would build your GUI. Your drawing JPanel would call the draw method in your BlockList class.

I strongly suggest that you go through the Oracle tutorial on Swing. Go through the complete tutorial before you attempt to create a Swing GUI.

OTHER TIPS

In java, use a Collection. In this case, use a List. You can create an ArrayList<Block>, then invoke add to add new rectangles. Then, you can iterate through this collection by using iterator, then calling hasNext and next on the iterator to find all the elements. I'm sure this brief hypnosis is going to be confusing, and if so, check out the Java Tutorial on Collections, which explains everything in all the detail you are going to need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top