英文:
How to use Java Graphics?
问题
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 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);
}
}
英文:
I'm getting an error saying "error: paintComponent(Graphics) in HelloGraphics cannot override paintComponent(Graphics) in JComponent
public static void paintComponent(Graphics g) {"
I'm trying to repeat a certain image 25 times in the window, but I didn't write the starter code, so I am unfamiliar with @Override and super.paintComponent(g)
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);
}
}
答案1
得分: 3
Remove the static keyword
@Override
public void paintComponent(Graphics g) {
The method is not static in the base class, and even if it were, you can't override static methods anyway.
英文:
Remove the static keyword
@Override
public static void paintComponent(Graphics g) {
becomes
@Override
public void paintComponent(Graphics g) {
The method is not static in the base class, and even if it were, you can't override static methods anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论