JAVA 更改图形颜色
JAVA Changin a graphics Color
每个人我都有一个代码可以创建一个圆圈和一个按钮。圆圈在开始时有一种颜色,每次我点击它时按钮都必须改变这个圆圈的颜色但它不会每当我点击它时控制台都会给出错误。由于我是 JAVA 的新手,所以我找不到解决方案。这是代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUI extends JPanel implements ActionListener {
JFrame frame;
public static void main(String[] args) {
GUI gui = new GUI();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("change colors");
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
我要重绘的函数:
import java.awt.*;
import javax.swing.*;
public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color starColor = new Color(red, green, blue); ;
red = (int) (Math.random() * 256);
green = (int) (Math.random() * 256);
blue = (int) (Math.random() * 256);
Color endColor = new Color(red, green, blue);
GradientPaint gradient = new GradientPaint(70, 70,starColor, 150, 150,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}
如果您在 post 中包含堆栈跟踪,我们会更容易。但我想这个问题已经很清楚了。
在 go
方法中,您将 JFrame
存储在一个局部变量中:
JFrame frame = new JFrame();
只需将此行更改为:
frame = new JFrame();
这实际上将以您想要的方式存储变量 - 作为 class GUI
的成员变量
每个人我都有一个代码可以创建一个圆圈和一个按钮。圆圈在开始时有一种颜色,每次我点击它时按钮都必须改变这个圆圈的颜色但它不会每当我点击它时控制台都会给出错误。由于我是 JAVA 的新手,所以我找不到解决方案。这是代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GUI extends JPanel implements ActionListener {
JFrame frame;
public static void main(String[] args) {
GUI gui = new GUI();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("change colors");
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
我要重绘的函数:
import java.awt.*;
import javax.swing.*;
public class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random()*256);
int green = (int) (Math.random()*256);
int blue = (int) (Math.random()*256);
Color starColor = new Color(red, green, blue); ;
red = (int) (Math.random() * 256);
green = (int) (Math.random() * 256);
blue = (int) (Math.random() * 256);
Color endColor = new Color(red, green, blue);
GradientPaint gradient = new GradientPaint(70, 70,starColor, 150, 150,endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}
如果您在 post 中包含堆栈跟踪,我们会更容易。但我想这个问题已经很清楚了。
在 go
方法中,您将 JFrame
存储在一个局部变量中:
JFrame frame = new JFrame();
只需将此行更改为:
frame = new JFrame();
这实际上将以您想要的方式存储变量 - 作为 class GUI