GUI 不在绘图面板上绘图?
GUI not drawing on drawpanel?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener{
JFrame frame;
public static void main (String[] args){
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Change colours");
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{
protected void paintComponenet(Graphics g){
g.fillRect(0,0,this.getWidth(),this.getHeight())
Graphics2D g2d = (Graphics2D) g;
int red = (int) Math.random() * 256;
int green = (int) Math.random() * 256;
int blue = (int) Math.random() * 256;
Color startColour = new Color(red,green,blue);
red = (int) Math.random() * 256;
green = (int) Math.random() * 256;
blue = (int) Math.random() * 256;
Color endColour = new Color(red,green,blue);
GradientPaint gradient = new GradientPaint(70,70,startColour,150,150,endColour);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}
我正在尝试了解 GUI,我有一些代码可以在每次按下按钮时绘制一个随机颜色的圆圈。
但目前拉板总是像这样保持灰色http://i.imgur.com/Kz84rKR.png
是否有任何明显的因素阻止代码按预期运行?
现在圆圈总是黑色的,永远不会变成随机颜色。
你拼错了paintComponent。始终在此方法之前加上 @Override
所以改变这个:
public void paintComponenet(Graphics g) {
g.fillRect(0, 0, this.getWidth(),this.getHeight());
对此:
@Override // add so the compiler will warn you if you spell it wrong
protected void paintComponent(Graphics g) { // should be protected, not public
super.paintComponent(g); // so that you erase old images
g.fillRect(0, 0, this.getWidth(),this.getHeight());
编辑
您需要在括号中进行双重数学运算,以便将正确的值转换为 int,因为:
red = (int) Math.random() * 256;
相当于这个
red = ((int) (Math.random())) * 256;
这等于:
red = 0 * 256;
所以,改为这样做:
red = (int) (Math.random() * 256);
但更重要的是你需要学会调试。使用 printlns 打印出您的颜色值或使用调试器来尝试隔离您的错误。来这里之前先做这件事。例如:
red = (int) Math.random() * 256;
green = (int) Math.random() * 256;
blue = (int) Math.random() * 256;
// this will show you what your values are
// and key you in to where you should look:
System.out.printf("Colors: [%d, %d, %d]%n", red, green, blue);
Color endColour = new Color(red,green,blue);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener{
JFrame frame;
public static void main (String[] args){
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Change colours");
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{
protected void paintComponenet(Graphics g){
g.fillRect(0,0,this.getWidth(),this.getHeight())
Graphics2D g2d = (Graphics2D) g;
int red = (int) Math.random() * 256;
int green = (int) Math.random() * 256;
int blue = (int) Math.random() * 256;
Color startColour = new Color(red,green,blue);
red = (int) Math.random() * 256;
green = (int) Math.random() * 256;
blue = (int) Math.random() * 256;
Color endColour = new Color(red,green,blue);
GradientPaint gradient = new GradientPaint(70,70,startColour,150,150,endColour);
g2d.setPaint(gradient);
g2d.fillOval(70,70,100,100);
}
}
我正在尝试了解 GUI,我有一些代码可以在每次按下按钮时绘制一个随机颜色的圆圈。
但目前拉板总是像这样保持灰色http://i.imgur.com/Kz84rKR.png
是否有任何明显的因素阻止代码按预期运行?
现在圆圈总是黑色的,永远不会变成随机颜色。
你拼错了paintComponent。始终在此方法之前加上 @Override
所以改变这个:
public void paintComponenet(Graphics g) {
g.fillRect(0, 0, this.getWidth(),this.getHeight());
对此:
@Override // add so the compiler will warn you if you spell it wrong
protected void paintComponent(Graphics g) { // should be protected, not public
super.paintComponent(g); // so that you erase old images
g.fillRect(0, 0, this.getWidth(),this.getHeight());
编辑
您需要在括号中进行双重数学运算,以便将正确的值转换为 int,因为:
red = (int) Math.random() * 256;
相当于这个
red = ((int) (Math.random())) * 256;
这等于:
red = 0 * 256;
所以,改为这样做:
red = (int) (Math.random() * 256);
但更重要的是你需要学会调试。使用 printlns 打印出您的颜色值或使用调试器来尝试隔离您的错误。来这里之前先做这件事。例如:
red = (int) Math.random() * 256;
green = (int) Math.random() * 256;
blue = (int) Math.random() * 256;
// this will show you what your values are
// and key you in to where you should look:
System.out.printf("Colors: [%d, %d, %d]%n", red, green, blue);
Color endColour = new Color(red,green,blue);