Drawing circles using the coordinate values that are stored in an ArrayList.
I want to make circles onto a JPanel using the x and y coordinates that are generated by another method in the same class. I have stored these coordinate values in an ArrayList<Integer>.
How do i access these values or the ArrayList inside the paint(Graphics g) method?
I tried making the ArrayList as final and declared it as a class variable. But, i still couldnt access the list in paint().
Is there some way to pass parameters into the paint() ??? ???
Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:confused::c onfused::confused:
Re: Drawing circles using the coordinate values that are stored in an ArrayList.
You can't pass parameters into paint. You can put the object at the class level so that code in the paint method can see it.
Quote:
i still couldnt access the list in paint()
How did you code it?
Re: Drawing circles using the coordinate values that are stored in an ArrayList.
Quote:
Originally Posted by
Norm
You can't pass parameters into paint. You can put the object at the class level so that code in the paint method can see it.
How did you code it?
I tried to simplify my code. Now, the x and y coordinates have been stored in x[] and y[] coordinates.
Code :
package manet;
import java.awt.*;
import java.awt.Point;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
public class Simulation {
public void init() //called by a method of another class in the same package
{
JFrame frame = new JFrame("Circles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DisplayPanel myPanel = new DisplayPanel();
myPanel.setBackground(Color.WHITE);
frame.add( myPanel );
frame.setSize(1000, 540 );
frame.setVisible( true );
myPanel.setSize(1000, 500);
myPanel.setVisible(true);
}
}
class DisplayPanel extends JPanel
{ private int noOfNodes1;
private int x[];
private int y[];
private ArrayList<ArrayList<String>> nodeList1 = new ArrayList<ArrayList<String>>();
ArrayList<Point>coordinates = new ArrayList();
public void set(int noOfNodes, ArrayList<String> nodeList){ //called by a //method of another class in the same package.
noOfNodes1 = noOfNodes;
x = new int[noOfNodes1+1];
y = new int[noOfNodes1+1];
nodeList1 = new ArrayList(nodeList);
System.out.println(noOfNodes1);
for(int i = 0; i<noOfNodes+1; i++){
ArrayList<String> temp = (ArrayList<String>)nodeList1.get(i);
x[i] = Integer.parseInt(temp.get(5));
y[i] = Integer.parseInt(temp.get(6));
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.translate(0,500);
for(int i = 0; i<x.length; i++){ //This is NOT working
g.fillOval(x[i],y[i], 50, 50);
// g.fillOval(1000, -50, 50, 50);
}
}
Re: Drawing circles using the coordinate values that are stored in an ArrayList.