简体   繁体   中英

Making overridden paintComponent editable?

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);
    }
}

您能否将您的城市放入“活动”或“非活动”的数据结构中,并在上面使用if语句来根据模型状态更改显示颜色?

  1. 1) Create a City class which has its coordinates
  2. In your paint panel, keep a list of Cities and a selected City variable
  3. In paintComponent(), loop over the cities and paint them, using a different color when a City == the selected City
  4. In your mouse listener, change the selected city variable and call repaint on your paint panel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM