简体   繁体   中英

How can I recolor a rectangle?

So basically I have a class of a rectangle and I want to implement a method that changes its color after it has already been drawn.

Here´s my code so far. Thought it might work like changing the position of an object, but it didn´t.

public class rectangle extends JPanel(){

@Override
protected void paintComponent(Graphics g){
   super.paintComponent(g);
   g.setColor(Color.red)
   g.fillRect(0, 0, 30, 30);
}

public void recolor(){
  Color.blue;
  repaint();
}
}

You could do the following

Main.java

import java.awt.Dimension;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main {

public static void main(String[] args) throws InterruptedException {
    SwingUtilities.invokeLater(new Runnable() { //do some research about SwingUtilities.invokeLater
        @Override
        public void run() {
            JFrame frame = new JFrame();
            JPanel panel = (JPanel) frame.getContentPane();
            Rectangle rectangle = new Rectangle();
            panel.add(rectangle);

            frame.setPreferredSize(new Dimension(400, 400));
            frame.pack();
            frame.setVisible(true);

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    rectangle.recolor();
                }
            }, 0, 1000); // 0 = initial delay, 1000 = intervall
        }
    });

}

}

Rectangle.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JPanel;

public class Rectangle extends JPanel {

    private Color color;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public void recolor() {
        Random r = new Random();
        color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        repaint();
    }
}

Don't keep hardcoding values. When you use Swing components they have methods that allow you to dynamically change a property.

For example all components have methods like:

  1. setFont(...)
  2. setBorder(...);
  3. setBackground(...);
  4. setForeground(...);

In your case you don't even need a custom method. In your application code you can just use:

someComponent.setForeground( Color.BLUE );

Then in your painting code you use:

@Override
protected void paintComponent(Graphics g)
{
   super.paintComponent(g);
   //g.setColor(Color.red)
   g.setColor( getForeground() ); 
   g.fillRect(0, 0, 30, 30);
}

If you don't want to use the foreground color property then you can add a custom property to your code. The basic steps would be

  1. create a method like setColor(Color color)
  2. create an instance variable "color" to save the property in your class
  3. customize the painting code to use the "color" property

The above 3 steps would apply for any custom property to want to add to your class.

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