Making overridden paintComponent editable?
-
09-12-2019 - |
题
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:
public class City extends JComponent{
private boolean active=false;
protected int x;
private int y;
public City(int x, int y){
setBounds(x, y, 20, 20);
setPreferredSize(new Dimension(20,20));
setMaximumSize(new Dimension(20,20));
setMinimumSize(new Dimension(20,20));
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(active==true)
active(g);
else if(active==true)
notActive(g);
}
public void active(Graphics g){
g.setColor(Color.RED);
g.fillRect(0,0,getWidth(),getHeight());
}
public void notActive(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, getWidth(),getHeight());
}
public boolean clicked(boolean b){
active=b;
repaint(); //repaint whenever the flag active changes
}
public boolean isActive(){
return active;
}
where x and y are coordinates
And in your main class: //The listener which should be added to each city object
public class cityListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
City current=(City)e.getSource();
if(current.active==false)
current.clicked(true);
else if(current.active==true)
current.clicked(false);
}
}
其他提示
Could you put your cities in a data structure for either "active" or "inactive" and have an if statement in the above to change the display colour by the state of the model?
- 1) Create a City class which has its coordinates
- In your paint panel, keep a list of Cities and a selected City variable
- In paintComponent(), loop over the cities and paint them, using a different color when a City == the selected City
- In your mouse listener, change the selected city variable and call repaint on your paint panel