Java 改变绘制的椭圆的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18173716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:01:32  来源:igfitidea点击:

Changing color of a drawn oval

javaswingobjectawtjava-2d

提问by user2551556

I create an object called "City"

我创建了一个名为“城市”的对象

    City city = new City (name, rec, g);

The object's constructor looks like this:

对象的构造函数如下所示:

    public City (String name, Rectangle r, Graphics g){
    this.name = name; 
    this.r = r;
    this.g = g; 
    }

By creating this object i also draw an oval on an uploaded picture, and set it's colour to BLUE. Here's how I draw the object:

通过创建这个对象,我还在上传的图片上绘制了一个椭圆,并将其颜色设置为蓝色。这是我绘制对象的方式:

    g = (Graphics2D) window.lblNewLabel.getGraphics();
        g.setColor(Color.BLUE);
        g.fillOval(mouseX, mouseY, 15, 15);

I would like to be able to change that colour later, after clicking on the oval itself. I try to call this function, but it doesn't work:

在单击椭圆本身后,我希望能够稍后更改该颜色。我尝试调用此函数,但不起作用:

        public void isClicked(){
    clicked = true;
    this.color = Color.RED;
    this.g.setColor(Color.PINK);

}

How to change a colour of an existing object?

如何更改现有对象的颜色?

采纳答案by Reimeus

Using getGraphics()on a component causes a transient graphics Object to be used on the component itself. Any subsequent calls to repaintwill erase the painting done using that object.

使用getGraphics()在组件上导致要组件本身上使用的瞬时图形对象。任何后续调用都repaint将擦除使用该对象完成的绘画。

Change the color by overriding the paintComponentmethod. Save the Colorvariable as a class member variable and use it to determine the oval color in the method.

通过覆盖paintComponent方法更改颜色。将该Color变量保存为类成员变量,并在方法中使用它来确定椭圆颜色。

@Override
protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.setColor(savedColor);
   g.fillOval(mouseX, mouseY, 15, 15);
}

回答by camickr

Don't use getGraphics()to do painting, that painting is only temporary and will be lost the next time Swing determines a components needs to be repainted.

不要getGraphics()用来绘制,绘制只是暂时的,下次 Swing 确定需要重新绘制组件时会丢失。

Check out Playing With Shapesfor other ideas on doing painting.

查看玩形状以了解有关绘画的其他想法。

You could use a ShapeIconwhich allows you to change the color of the icon. The icon could be painted in the paintComponent() method of your label.

您可以使用ShapeIcon允许您更改图标颜色的 a。可以在标签的 paintComponent() 方法中绘制图标。

Or you could use ShapeComponentwhich uses a ShapeIcon. Then you can just add the component to the label like any other component.

或者您可以使用ShapeComponentwhich 使用 ShapeIcon。然后您可以像任何其他组件一样将组件添加到标签。

I would like to be able to change that colour later, after clicking on the oval itself

单击椭圆本身后,我希望能够稍后更改该颜色

The ShapeIcon would be added to a JLabel. Then you can just add a MouseListener to the label of the ShapeComponent to change the color of the icon.

ShapeIcon 将被添加到 JLabel。然后你可以在 ShapeComponent 的标签上添加一个 MouseListener 来改变图标的​​颜色。

回答by PurkkaKoodari

When you have drawn objects with a Graphicsobject, they are then rendered on the screen. You can't change the color of them directly, instead you will have to repaint your graphics any time when you want to change them. If you want to keep track of the colors for the objects, you will have to store the data in some kind of variable and use it when drawing.

当您使用对象绘制Graphics对象后,它们会在屏幕上呈现。您不能直接更改它们的颜色,相反,当您想更改它们时,您必须随时重新绘制图形。如果要跟踪对象的颜色,则必须将数据存储在某种变量中并在绘图时使用它。