如何使用Java图形?

huangapple go评论77阅读模式
英文:

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 &lt; 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(&quot;Hello, Graphics!&quot;);
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.

huangapple
  • 本文由 发表于 2020年8月27日 23:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63619609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定