如何使用Java图形?
我在说错误时出错:HelloGraphics中的paintComponentGraphics无法覆盖JComponent中的paintComponentGraphics 公共静态无效paintcomponent图形{ 我试图在窗口中重复某个图像25次,但我没有编写起始代码,因此我不熟悉@Override和super.paintcomponent如何使用Java图形?,java,graphics,Java,Graphics,我在说错误时出错:HelloGraphics中的paintComponentGraphics无法覆盖JComponent中的paintComponentGraphics 公共静态无效paintcomponent图形{ 我试图在窗口中重复某个图像25次,但我没有编写起始代码,因此我不熟悉@Override和super.paintcomponent import java.awt.Color; import java.awt.Font; import java.awt.Graphics; impor
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class HelloGraphics extends JPanel{
public static final int BOX_WIDTH = 1024;
public static final int BOX_HEIGHT = 768;
public static final Color MAMMOTH_PURPLE = new Color(63, 31, 105);
public static final Color SPRING_LEAF = new Color(91, 161, 81);
public static int x = 0;
public static int y = 0;
public HelloGraphics(){
this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
}
@Override
public static void paintComponent(Graphics g) {
super.paintComponent(g);
//Your code here: feel free to remove what is below
for (int i = 0; i < 25; i++) {
x += 5;
y += 5;
g.setColor(Color.WHITE);
g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.ORANGE);
g.fillOval(x + 60, 70, 120, 140);
g.fillOval(x + 65, 170, 40, 50);
g.fillOval(x + 140, 170, 40, 50);
g.setColor(Color.black);
g.fillOval(x + 70, y + 100, 10, 15);
g.fillOval(x + 100, y + 100, 10, 15);
g.setColor(Color.MAGENTA);
g.drawOval(x + 70, y + 120, 50, 40);
}
}
public static void main(String args[]){
JFrame frame = new JFrame("Hello, Graphics!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new HelloGraphics());
frame.pack();
frame.setVisible(true);
}
}
删除static关键字
@Override
public static void paintComponent(Graphics g) {
变成
@Override
public void paintComponent(Graphics g) {
该方法在基类中不是静态的,即使是静态的,您也不能重写静态方法。您应该得到一个编译器错误,因为您不能重写静态方法。