[英]Change the color of an oval at click in Java AWT
I have to draw some ovals in Java, and at click to change their color. 我必须在Java中绘制一些椭圆,然后单击以更改其颜色。 For the beginning I tried to change the color after 20 ms, but it doesn't work.
首先,我尝试在20毫秒后更改颜色,但是它不起作用。
My code is: 我的代码是:
public class MyComponentNew extends Frame {
public Graphics2D g2d;
public MyComponentNew(String title) {
super(title);
setSize(400, 550);
}
@Override
public void paint(Graphics g) {
this.g2d = (Graphics2D) g;
this.g2d.setColor(Color.red);
this.g2d.fillOval(10, 55, 50, 100);
}
public void changeColor () {
this.g2d.setColor(Color.blue);
this.g2d.fillOval(10, 55, 50, 100);
}
}
And in the class with main method I have: 在具有main方法的类中,我有:
MyComponentNew m;
m = new MyComponentNew("Fereastra cu baloane");
m.setVisible(true);
m.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
try {
Thread.sleep(20);
} catch(InterruptedException e) {}
m.changeColor();
The color of the oval remains red. 椭圆形的颜色保持红色。
You need to look at Performing Custom Painting . 您需要查看“ 执行自定义绘画” 。 You kinda have the concept, but your
changeColor
method isn't going to do anything for you. 您有点概念,但是您的
changeColor
方法不会为您做任何事情。
Some things to note. 一些注意事项。
First you need to add a MouseListener
. 首先,您需要添加一个
MouseListener
。 See more at How to Write MouseListeners 在如何编写MouseListeners中查看更多信息
Second what you want to do is just have a Color color
variable. 其次,您要做的只是拥有一个
Color color
变量。 You use that variable to set the color. 您可以使用该变量设置颜色。 Your method
changeColor
should only change the color and repaint()
. 您的方法
changeColor
应该只更改颜色和repaint()
。 Something like 就像是
public class MyComponentNew extends JPanel { private Color color = Color.BLUE; protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(color); } public void changeColor() { if (color == Color.BLUE) { color = Color.RED: } else { color = color.BLUE; } repaint(); } }
Third notice how I use JPanel
instead of JFrame
. 第三个注意事项是我如何使用
JPanel
而不是JFrame
。 This is the preferred approach. 这是首选方法。
Fourth you should be using Swing and not AWT. 第四,您应该使用Swing而不是AWT。
Here's a full example, with those points above 这是一个完整的示例,上面有几点
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CircleChangeColor extends JPanel {
private Ellipse2D circle = new Ellipse2D.Double(0, 0, 200, 200);
private Color color = Color.blue;
public CircleChangeColor() {
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
if (circle.contains(e.getPoint())) {
changeColor();
}
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);
g2.fill(circle);
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void changeColor() {
if (color == Color.BLUE) {
color = Color.RED;
} else {
color = color.BLUE;
}
repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JFrame frame = new JFrame();
frame.add(new CircleChangeColor());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
The reason is that your paint
method will be called periodically by AWT and it will quickly draw over your new blue oval with the original red one. 原因是AWT会定期调用您的
paint
方法,它会快速绘制新的蓝色椭圆和原始的红色椭圆。 You can get the behavior you want by storing the oval color in a member variable, reading it in the paint
method, and changing it in your changeColor
method. 通过将椭圆形颜色存储在成员变量中,在
paint
方法中读取它,然后在changeColor
方法中对其进行更改,可以获得所需的行为。
public class MyComponentNew extends Frame {
private Color ovalColor;
public MyComponentNew(String title) {
super(title);
setSize(400, 550);
ovalColor = Color.red; // initial oval color
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(ovalColor); // use whatever the current color is
g2d.fillOval(10, 55, 50, 100);
}
public void changeColor() {
ovalColor = Color.blue; // change color
repaint(); // force redraw with new color
}
}
You need to call the repaint() method on the JFrame, but as the code is, the red oval will be drawn again since the paint-method will be called. 您需要在JFrame上调用repaint()方法,但是正如代码所示,由于将调用绘画方法,因此将再次绘制红色椭圆形。 You need to alter it something like this:
您需要像这样更改它:
import java.awt.Graphics2D;
public class MyComponentNew extends Frame{
public Graphics2D g2d;
public Color color = Color.red;
public MyComponentNew(String title) {
super(title);
setSize(400, 550);
}
@Override
public void paint(Graphics g) {
this.g2d = (Graphics2D) g;
this.g2d.setColor(color);
this.g2d.fillOval(10, 55, 50, 100);
//this.g2d.drawLine (WIDTH, WIDTH, WIDTH, WIDTH);
}
public void changeColor (){
color = Color.blue;
this.repaint();
}
}
The color is now an instance variable which is set during the changeColor method. 现在,颜色是在changeColor方法期间设置的实例变量。
/Nick /缺口
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.