I’m bulding a program where the user can add ‘places’ to a map; the map is a background-image in a JPanel, and when clicked the program creates an instance of my city-class and draws/paints out a dot where the user clicked.
The drawing is made trough an over-riding of paintComponent; like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
setCursor(handCursor);
g.setColor(Color.RED);
g.fillOval(0, 0, 15, 15);
g.setColor(Color.GRAY);
g.fillOval(2, 2, 11, 11);
g.setColor(Color.GRAY);
g.drawString(globalString, 0, 25);
g.setColor(Color.RED);
g.drawString(globalString, 1, 26);
bild.setCursor(oldCursor);
bild.removeMouseListener(mln);
}
Now i’d like to make this method editable, in some way.
My goal is to have the ‘cities’ react to clicks (which already is settled in the city-class), one click makes the city ‘active’ and another makes it ‘inactive’.
My problem now is how to make changes in the drawing itself,
change:
g.setColor(Color.GRAY);
g.fillOval(2, 2, 11, 11);
to:
g.setColor(Color.WHITE);
g.fillOval(2, 2, 11, 11);
Is it possible to override the paintComponent once again, or can i make calls to the overridden version that i’ve created?
Edit:
I’m using two boolenas to settle if a city is selected or not, only two cities can be selected (and they are then avalible for further operations).
The boolens are sel1 and sel2
public void paintComponent(Graphics g) {
if (sel1==false)
{
super.paintComponent(g);
setCursor(handCursor);
g.setColor(Color.RED);
g.fillOval(0, 0, 15, 15);
g.setColor(Color.GRAY);
g.fillOval(2, 2, 11, 11);
g.setColor(Color.GRAY);
g.drawString(globalString, 0, 25);
g.setColor(Color.RED);
g.drawString(globalString, 1, 26);
bild.setCursor(oldCursor);
bild.removeMouseListener(mln);
}
else if(sel1==true)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(0, 0, 25, 25);
g.setColor(Color.WHITE);
g.fillOval(2, 2, 5, 5);
g.setColor(Color.WHITE);
g.drawString(globalString, 0, 25);
g.setColor(Color.BLACK);
g.drawString(globalString, 1, 26);
}
}
This code makes change, but only when a new city is created, and then it makes changes to all of them..
Sorry, i’m rather fresh to the logics here.
Try something like this:
where x and y are coordinates
And in your main class:
//The listener which should be added to each city object