gpt4 book ai didi

java - 如何在Java中实现Graphic2D对象的颜色变化?

转载 作者:行者123 更新时间:2023-12-02 10:38:03 26 4
gpt4 key购买 nike

我试图了解是否可以在应用程序运行时更改现有图形的颜色(多种不同颜色之间的闪烁)。

我有一个使用 GeneralPath 类绘制的起始形状,并且我还有一个正常工作的随机 RGB 颜色代码选择器。是否可以使用 repaint(); 更改颜色,让我的星星的颜色在从 createRandomColor() 中随机选择的 3 个选定颜色之间变化方法?

代码

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import static java.lang.Math.random;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Sandbox extends JApplet {
DrawingStar canvas;

public static void main(String[] args) {
JFrame frame = new JFrame();
Sandbox path = new Sandbox();
path.init();
frame.getContentPane().add(path);
frame.setSize(250, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void init() {
Container container = getContentPane();
canvas = new DrawingStar();
container.add(canvas);
}

}

class DrawingStar extends Canvas {
Vector generalPaths;
private List<Color> colors;

public DrawingStar() {
colors = new ArrayList<Color>();

for (int i = 0; i < 3; i++) {
colors.add(createRandomColor());
}

setBackground(Color.white);
setSize(400, 400);
generalPaths = new Vector();

GeneralPath gp1;

gp1 = new GeneralPath();
gp1.moveTo(50, 120);
gp1.lineTo(70, 180);
gp1.lineTo(20, 140);
gp1.lineTo(80, 140);
gp1.lineTo(30, 180);
gp1.closePath();
generalPaths.addElement(gp1);
}

public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.translate(70.0f, -85.0f);
g2D.draw((GeneralPath) generalPaths.elementAt(0));

for (int i = 0; i < 3; i++) {
Color color = colors.get(i);
g2D.setColor(color);
g2D.fill((GeneralPath) generalPaths.elementAt(0));
}

System.out.println("Is this working");
repaint();
}


private Color createRandomColor(){
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
Color color = new Color(r,g,b);

return color;
}

}

最佳答案

小程序已死,HTML5 万岁

是时候继续了……而且,小程序的生命周期比您实现的要复杂得多。

“替代方案”

Canvas 是一个低级组件,可能不太适合您的需求。它也不是双缓冲的,因此重新绘制时会出现闪烁。

不要从任何绘制方法中调用repaint,您不会喜欢结果。相反,安排在将来的某个时间进行更新。

此示例仅使用 Swing Timer 充当伪循环,每秒递增计数器并获取下一个颜色并调用重绘。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Sandbox {

DrawingStar canvas;

public static void main(String[] args) {
new Sandbox();
}

public Sandbox() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
canvas = new DrawingStar();
frame.getContentPane().add(canvas);
frame.setSize(250, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}

class DrawingStar extends JPanel {

Vector generalPaths;
private List<Color> colors;
private int colorIndex = 0;
private Color color;

public DrawingStar() {

colors = new ArrayList<Color>();

for (int i = 0; i < 3; i++) {
colors.add(createRandomColor());
}

color = colors.get(0);

setBackground(Color.white);
generalPaths = new Vector();

GeneralPath gp1;

gp1 = new GeneralPath();
gp1.moveTo(50, 120);
gp1.lineTo(70, 180);
gp1.lineTo(20, 140);
gp1.lineTo(80, 140);
gp1.lineTo(30, 180);
gp1.closePath();
generalPaths.addElement(gp1);

Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
colorIndex++;
if (colorIndex >= colors.size()) {
colorIndex = 0;
}

color = colors.get(colorIndex);
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
Graphics2D g2D = (Graphics2D) g;
g2D.translate(70.0f, -85.0f);
g2D.draw((GeneralPath) generalPaths.elementAt(0));

g2D.setColor(color);
g2D.fill((GeneralPath) generalPaths.elementAt(0));
}

private Color createRandomColor() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
Color color = new Color(r, g, b);

return color;
}

}
}

Swing 不是线程安全的并且是单线程的,因此使用 Swing Timer

关于java - 如何在Java中实现Graphic2D对象的颜色变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com