2

I wrote a simple program where the user can make squares. I have a JOptionPane class that gives the user 3 options: 1. make a square, 2. show the made squares and 0. quit. I want to store all the squares in an ArrayList so when the user chooses 2 it returns all the made squares, with a format() method. So the JOptionPane class has a static arraylist, but it never keeps the stored squares. This JOptionPane class is called by the main method class Launcher. But when I press 2 to show all the squares it returns nothing. There is also a really simple class Position that sets a certian position with an x and y value.

Class square:

package domain;
import java.awt.Color;

public class Square {

    private Color color;
    private int size;
    private Position position;

    public Square(Color color, int size, Position position) {
        this.setColor(color);
        this.setSize(size);
        this.setPosition(position);
    }

    public Color getColor() {
        return color;
    }
    public void setColor(Color color) {
        this.color = color;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public Position getPosition() {
        return position;
    }
    public void setPosition(Position position) {
        this.position = position;
    }

    public String format() {
        return "Square with size " + this.getSize() + " on " +     this.getPosition().format(); 
    }

}

JOptionPane class:

package ui;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;

import domain.Position;
import domain.Square;

public class JPaintMenu {

    public static ArrayList<Square> squares = new ArrayList<Square>();

    public int menu() {
        String choice = JOptionPane.showInputDialog("1. Create square\n2. Show squares\n\n0. Quit\nMake your choice:\n");
        return Integer.parseInt(choice);
    }

    public Square newSquare() {
        Color color = JColorChooser.showDialog(null, "Color of the square?", null);
        String sizeString = JOptionPane.showInputDialog("Size of the square?");
        int size = Integer.parseInt(sizeString);
        String xString = JOptionPane.showInputDialog("X coordinate?");
        int x = Integer.parseInt(xString);
        String yString = JOptionPane.showInputDialog("Y coordinate?");
        int y = Integer.parseInt(yString);

        Position position = new Position(x, y);
        Square square = new Square(color, size, position);
        squares.add(square);
        return square;
    }

    public String format() {
        String result = "";
        for(Square square : squares) {
            result += square.format();
        }
        return result;
    }

}

And the main method class Launcher:

package ui;
import javax.swing.JOptionPane;

public class Launcher {

    public static JPaintMenu menu = new JPaintMenu();

    public static void main(String[] args) {
        int result = menu.menu();
        if(result == 1) {
            menu.newSquare();
        }
        if (result == 2) {
            JOptionPane.showMessageDialog(null, menu.format());
        }
    }   
}
2
  • 4
    You're only performing one action each time you run the program. When the program exits, anything still just in memory is lost. You either need to store the results in a file (or something similar), or loop round giving the user the menu multiple times, until they quit... Commented May 23, 2015 at 13:42
  • You call the program multiple times? Commented May 23, 2015 at 13:43

1 Answer 1

4

I think either (1) you confuses static with persistent, or (2) your main method should be implemented in a loop.

With static one means you don't store the state of an object into another object, but shared over all objects of the same type. Persistent storage means you store the state of an object such that it is accessible the next time you call the program. In that case you need to serialize the data; and store it into a file (or webserver,...)

Another possibility is to make the program query the user multiple times; like:

int result = -1;
do {
    result = menu.menu();
    if(result == 1) {
        menu.newSquare();
    }
    if (result == 2) {
        JOptionPane.showMessageDialog(null, menu.format());
    }
} while(result == 1 || result == 2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.