Reputation: 21
I have a program that I've been working: it takes data from the user does some maths with it and then displays an ellipse to the screen, when new data is entered the old ellipses disappears and the new one replaces it. However I need the program to keep the old ellipse on the screen as well as the new ones so I can compare sizes. My solution to this is to have it so that when an ellipse is created it is stored in an array, and then the array of ellipse is drawn onto the screen, I also need it so that the user can clear the array and start over. However I cannot get the code to work. Will you please help?
Below is the code that I used to create and draw the ellipse all of the variables used are just numbers.
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.white);
g2.draw(new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblsemiMinoraxis / 2), gblSemiMajaxis, gblsemiMinoraxis));
}
Upvotes: 2
Views: 1913
Reputation: 6642
Just to expand on Ian McLarid's answer:
// imports
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
...
ArrayList<Ellipse2D> ellipseList = new ArrayList<Ellipse2D>();
public void createEllipse(double gblSemiMajaxis, double gblSemiMinoraxis) {
Ellipse2D e = new Ellipse2D.Double(((Background.getWidth()) / 2) - (gblSemiMajaxis / 2), ((Background.getHeight()) / 2) - (gblSemiMinoraxis / 2), gblSemiMajaxis, gblSemiMinoraxis);
ellipseList.add(e);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.white);
for (Ellipse2D e : ellipseList) {
g2.draw(e);
}
}
Upvotes: 1
Reputation: 5585
I would suggest giving your class a member variable of type ArrayList<Ellipse2D>
. When your user enters their input, create the Ellipse2D and add it to the list. In your paint function, you could iterate over the list and paint each of the Ellipses you've already made. When the user wants to clear all of the Ellipses, you could use the ArrayList's clear()
method.
Upvotes: 0