I'm trying to change the Rectangle colour to Black but its not working

时间:2017-12-18 05:17:57

标签: java graphics2d

I'm new to Java and don't know exactly what the cause.Let me explain the issue

I created a Rectangle Shape and its working, then i thought about changing its color to black for some testing but it seems not working below is my code. When i call the method from paintComponent itself then its working but if i do the same from any other method then its not changing the color. I tried calling the method repaint also but still the same

public class Meme extends JPanel {

Rectangle2D.Float myRect = new Rectangle2D.Float(90, 90, 90, 90);
Graphics2D graphics2d;

public void DRAW() {
    graphics2d.setColor(new Color(0, 0, 200));
    graphics2d.fill(myRect);
}

public void ChangeColour() {
    System.out.println("Called");
    graphics2d.setPaint(Color.BLACK);
    System.out.println("Called2");
    graphics2d.fill(myRect);
    System.out.println("Called3");
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    graphics2d = (Graphics2D) g;
    graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    DRAW();
}

}

Button click listener method

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    meme1.ChangeColour();
} 

3 个答案:

答案 0 :(得分:0)

As far as I can remember, whenever you change some properties (color in this case), you have to call repaint. This will invoke a call to paintComponent and the frame will be drawn once again.

In your case, I am guessing even if you call repaint after changing color, the DRAW method gets called again in paintComponent which resets the changed color back to (0, 0, 200). Therefore, you don't see any change in the screen. But when you call changeColor in paintComponent method (assuming after the call to DRAW), the change of color persists and does not get overridden.

POSSIBLE SOLUTION

Just keep the color stored somewhere else. Like

Color myColor = new Color(0,0,200);

then in DRAW:

private void DRAW() {
    graphics2d.setColor(myColor);
    graphics2d.fill(myRect);
}

and in ChangeColor:

private void ChangeColour() {
    myColor = Color.BLACK;
}

Hope it helps.

答案 1 :(得分:0)

update your function like this

public void ChangeColour() {
    System.out.println("Called");
    graphics2d.setColor(new Color(1, 1, 200));
    System.out.println("Called2");
    graphics2d.fill(myRect);
    System.out.println("Called3");
}

答案 2 :(得分:0)

Painting in Swing is both passive and destructive. That is, a paint pass can occur at anytime for any number of reasons, many which you don't control. Destructive means, on each paint pass you are expected to repaint the entire component from scratch.

In Swing, you update the state you want change and then call repaint to trigger a new paint pass.

Painting should only ever paint the current state, it should never try and change it

public class Meme extends JPanel {

    Rectangle2D.Float myRect = new Rectangle2D.Float(90, 90, 90, 90);
    private Color color;

    public void draw(Graphics2D graphics2d) {
        graphics2d.setColor(color);
        graphics2d.fill(myRect);
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    public void ChangeColour() {
        color = Color.BLACK;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        graphics2d = (Graphics2D) g.create();
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        draw(graphics2d);
        graphics2d.dispose();
    }
}

Also, the graphics context passed to your component is shared with all the other components, so it's important that any significant changes you make to the context are undone before the method exists - in most cases, it's just a simple case of calling create on the Graphics context to snapshot it state and dispose (on the copy you created) when you're done