Java:Graphics2D
所以我已经连续两个小时试图解决这个问题,但似乎无法解决这个问题。这个“程序”应该有一个椭圆形,屏幕底部有一个按钮,允许我随机选择椭圆形的颜色。我使用的是netbeans,每当我单击run时,我都会看到:Java:Graphics2D,java,swing,jpanel,paintcomponent,Java,Swing,Jpanel,Paintcomponent,所以我已经连续两个小时试图解决这个问题,但似乎无法解决这个问题。这个“程序”应该有一个椭圆形,屏幕底部有一个按钮,允许我随机选择椭圆形的颜色。我使用的是netbeans,每当我单击run时,我都会看到: 有人能解决我的问题吗?如果这是一个愚蠢的问题,很抱歉浪费您的时间。拼写问题:paintComponet!=油漆组件 始终使用@Override作为重写方法的开头。如果您这样做了,编译器会警告您错误 因此,改变这一点: import java.awt.*; import javax.swing.*
有人能解决我的问题吗?如果这是一个愚蠢的问题,很抱歉浪费您的时间。拼写问题:
paintComponet!=油漆组件
始终使用@Override
作为重写方法的开头。如果您这样做了,编译器会警告您错误
因此,改变这一点:
import java.awt.*;
import javax.swing.*;
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 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.*;
import java.awt.event.*;
class MyDrawPanel extends JPanel {
public void paintComponet(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 startColor = 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, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}
为此:
public void paintComponet(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 startColor = 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, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
你有什么问题?圆圈没有出现,如图所示。底部出现了。请发布一个链接,更好的图片。链接应该是图像,而不是网页。对不起,先生,我修复了它。哇!我真的看了这个代码永远!而我却找不到问题所在!非常感谢你!我希望有一天我能像你一样精通代码!
@Override // don't forget this
protected void paintComponent(Graphics g) { // spelling matters. Also make it protected
// !!!! don't forget this!
super.paintComponent(g); // to have the JPanel do housekeeping painting
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
Color startColor = 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, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}