使用按钮更新图形颜色

发布于 2024-12-14 16:09:05 字数 2169 浏览 2 评论 0原文

我正在尝试更新构建框架时绘制的一些椭圆和线条的颜色。 然后我想使用框架上的按钮更改颜色。

package animation.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GraphDisplayTest extends JFrame {

static GraphDisplayTest gui;

private JButton changeColorBtn = new JButton("Change Color");
private Graphics2D g2;
private Ellipse2D e1;
private Ellipse2D e2;
private Ellipse2D e3;
private Ellipse2D e4;
int x = 50, y = 50, w = 20, h = 20;

public static void main(String[] args) {
    gui = new GraphDisplayTest();
    gui.launch();

}

public void launch(){
    gui.setLayout(new BorderLayout());
    gui.add(changeColorBtn, BorderLayout.SOUTH);

    changeColorBtn.addActionListener(new ChangeColor());

    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setTitle("Graph Display Example");
    gui.setSize(350, 400);
    gui.setVisible(true);
}

public class ChangeColor implements ActionListener{
    public void actionPerformed(ActionEvent e){

    //HERE IS THE PROBLEM - the colors are not being reset
        g2.setPaint(Color.BLACK);
        g2.fill(e1);
        g2.drawLine(x+10, y+10, x+210, y+10);
        g2.setColor(Color.WHITE);
    }
}


public void paint(Graphics g) {
    g2 = (Graphics2D)g;
    g2.drawLine(x+10, y+10, x+210, y+10);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+210, y+10, x+210, y+210);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+10, y+210, x+210, y+210);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+10, y+10, x+10, y+210);
    g2.setColor(Color.BLACK);

    e1 = new Ellipse2D.Double(x, y, w, h);
    g2.setPaint(Color.BLUE);
    g2.fill(e1);
    e2 = new Ellipse2D.Double(x+200, y, w, h);
    g2.setPaint(Color.RED);
    g2.fill(e2);
    e3 = new Ellipse2D.Double(x, y+200, w, h);
    g2.setPaint(Color.GREEN);
    g2.fill(e3);
    e4 = new Ellipse2D.Double(x+200, y+200, w, h);
    g2.setPaint(Color.YELLOW);
    g2.fill(e4);

}


}

我已经评论过我不知道该怎么办!有人可以帮忙吗?!?!

预先非常感谢 乔什

I am trying to update the colors of some elipses and lines that are drawn when the frame is constructed.
I then want to change the color using a button on the frame.

package animation.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GraphDisplayTest extends JFrame {

static GraphDisplayTest gui;

private JButton changeColorBtn = new JButton("Change Color");
private Graphics2D g2;
private Ellipse2D e1;
private Ellipse2D e2;
private Ellipse2D e3;
private Ellipse2D e4;
int x = 50, y = 50, w = 20, h = 20;

public static void main(String[] args) {
    gui = new GraphDisplayTest();
    gui.launch();

}

public void launch(){
    gui.setLayout(new BorderLayout());
    gui.add(changeColorBtn, BorderLayout.SOUTH);

    changeColorBtn.addActionListener(new ChangeColor());

    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setTitle("Graph Display Example");
    gui.setSize(350, 400);
    gui.setVisible(true);
}

public class ChangeColor implements ActionListener{
    public void actionPerformed(ActionEvent e){

    //HERE IS THE PROBLEM - the colors are not being reset
        g2.setPaint(Color.BLACK);
        g2.fill(e1);
        g2.drawLine(x+10, y+10, x+210, y+10);
        g2.setColor(Color.WHITE);
    }
}


public void paint(Graphics g) {
    g2 = (Graphics2D)g;
    g2.drawLine(x+10, y+10, x+210, y+10);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+210, y+10, x+210, y+210);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+10, y+210, x+210, y+210);
    g2.setColor(Color.BLACK);
    g2.drawLine(x+10, y+10, x+10, y+210);
    g2.setColor(Color.BLACK);

    e1 = new Ellipse2D.Double(x, y, w, h);
    g2.setPaint(Color.BLUE);
    g2.fill(e1);
    e2 = new Ellipse2D.Double(x+200, y, w, h);
    g2.setPaint(Color.RED);
    g2.fill(e2);
    e3 = new Ellipse2D.Double(x, y+200, w, h);
    g2.setPaint(Color.GREEN);
    g2.fill(e3);
    e4 = new Ellipse2D.Double(x+200, y+200, w, h);
    g2.setPaint(Color.YELLOW);
    g2.fill(e4);

}


}

I have commened where i have no idea what to do! Can anyone help?!?!

Many thanks in advance
Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

完美的未来在梦里 2024-12-21 16:09:09

图形对象仅在绘制方法中进行真正的更改。因此,保存 Graphics 对象(在您的情况下为 g2)是没有用的。相反,将颜色放入变量中,并在按下按钮时更改颜色。在绘制方法中使用颜色变量,这样下次调用绘制时它将用新颜色重新绘制。

也许您还需要调用repaint()。这命令 awt 线程对 Paint 进行新的调用。

The graphics-object is only making real changes, in the paint-method. So saving the Graphics-object (in your case g2) is of no use. Instead put the color in a variable and change the color the time the button is pressed. Use in the paint-method the color-variable, so it will repaint with the new color the next time paint is called.

Maybe you also need to call repaint(). That commands the awt-thread to make a new call to paint.

太阳公公是暖光 2024-12-21 16:09:09

您在 ActionListener 中使用的 Graphics 对象已在屏幕上。照着它画是不行的。您所要做的就是保留一个用于跟踪颜色 (*) 的字段。当动作侦听器运行时,将颜色设置为您想要的任何颜色,然后调用 repaint()。这是 JFrame 的一个方法,它将重新绘制窗口,它将调用您编写的 paint(Graphics g) 。在该方法中,检查您持有的颜色(与我用星号 * 标记的颜色相同)。根据颜色,设置 Graphics 对象的颜色,然后进行绘制。

The Graphics object you use in the ActionListener is already on the screen. Drawing on it won't work. What you have to do is to hold a field in which you keep track of the color (*). And when the action listener runs, set the color to whatever you want and then call repaint(). It's a method of the JFrame that will make the window repaint, which will call the paint(Graphics g) you wrote. Inside that method, check for the color you hold (the same as I marked by the asterisk *). Depending on the color, set the color of the Graphics object and then paint.

秋日私语 2024-12-21 16:09:08

不要存储 Graphics 对象的值(在本例中为:g2)。

创建一个新属性来存储 e1 的颜色:

Color c = Color.BLUE;

删除 actionPerformed(...) 中的所有代码,并将 c 的值设置为新颜色。然后,调用repaint()

c = Color.BLACK;
repaint();

paint(...) 中,将以下行:更改

g2.setPaint(Color.BLUE);
g2.fill(e1);

为:

g2.setPaint(c);
g2.fill(e1);

Don't store the value of Graphics object (in this case: g2).

Create a new attribute to store the color of e1:

Color c = Color.BLUE;

Delete all code in actionPerformed(...) and set the value of c to the new color. Then, call repaint().

c = Color.BLACK;
repaint();

In paint(...), change the lines:

g2.setPaint(Color.BLUE);
g2.fill(e1);

to:

g2.setPaint(c);
g2.fill(e1);
不寐倦长更 2024-12-21 16:09:07

每次调用 Paint 时,您都会覆盖具有颜色信息的旧图形

,最好添加一个初始化为 Color.BLACK 的字段颜色,您可以在 Paint 中使用它,并

在另一个注释 上的侦听器的 ActionPerformed 中更新它不建议保存图形并在绘制“事件”之外使用它

each time paint is called you overwrite the old graphics which has the color information

it's better add a field color initialized to Color.BLACK which you use inside paint and update that inside the actionPerformed of the listener

on another note saving the graphics and using it outside the paint "event" is not recommended

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文