我必须在Java中绘制一些椭圆,然后单击以更改其颜色。 一开始我尝试在20毫秒后改变颜色,但它不起作用。
我的代码是:
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);
}
}
在使用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();
椭圆的颜色保持红色。
答案 0 :(得分:1)
您需要查看Performing Custom Painting。您有点有这个概念,但您的changeColor
方法不会为您做任何事情。
有些事情需要注意。
首先,您需要添加MouseListener
。详情请见How to Write MouseListeners
其次你要做的就是拥有一个Color color
变量。您可以使用该变量来设置颜色。您的方法changeColor
只应更改颜色和repaint()
。像
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();
}
}
第三次注意我如何使用JPanel
代替JFrame
。这是首选方法。
第四,你应该使用Swing而不是AWT。
这是一个完整的例子,上面有这些点
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);
}
});
}
}
答案 1 :(得分:0)
原因是AWT会定期调用您的paint
方法,它会快速绘制原始红色椭圆形的新蓝色椭圆。您可以通过将椭圆颜色存储在成员变量中,在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
}
}
答案 2 :(得分:0)
你需要在JFrame上调用repaint()方法,但是代码是,因为将调用paint-method,所以将再次绘制红色椭圆。你需要改变它:
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();
}
}
颜色现在是一个在changeColor方法中设置的实例变量。
/尼克