0

我拼命尝试实现一些我认为我不完全理解的事情。我正在尝试对其进行设置,以便可以采取注释掉的操作(我需要更改语法,但我想确保我首先走在正确的轨道上)。

我会以正确的方式解决这个问题吗?如果不在 draw 方法中,我的绘图动作会去哪里?当我把它移到那里时,我得到了很多错误。谢谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics2D;
import java.awt.Graphics;

public class Test extends JPanel{

    abstract class graphic {
        public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        private int[] location = new int[] {screenSize.width/2,screenSize.height/2}; 
    }

    public class gladiator extends graphic {
        void draw() {
        //g2d.setColor(Color.green);
        //g2d.fillArc(location[0], location[1], 100, 100, 45, 90);
        //g2d.setColor(Color.black);
        //g2d.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //              setLocation((location[0]+1),location[1]);
                repaint();
                System.out.println("repainting");
            }
        }).start();

    }

    public void setLocation(int x, int y){
        //this.location[0] = x;
        //this.location[1] = y;
    }


    public static void main(String[] args){
        JFrame jf=new JFrame();
        jf.setDefaultCloseOperation
        (JFrame.EXIT_ON_CLOSE);
        jf.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
        jf.add(new Test());

        jf.pack();
        jf.setVisible(true);

    }
}

我的原始代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test extends JPanel{

    private int[] location = new int[2]; 

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.red);
        g.fillArc(location[0], location[1], 100, 100, 45, 90);
        g.setColor(Color.black);
        g.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360);

        new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setLocation((location[0]+50),50);
                repaint();
                System.out.println("repainting");
            }
        }).start();

    }

    public void setLocation(int x, int y){
        this.location[0] = x;
        this.location[1] = y;
    }


    public static void main(String[] args){
        JFrame jf=new JFrame();
        jf.setDefaultCloseOperation
        (JFrame.EXIT_ON_CLOSE);
        jf.setPreferredSize(new Dimension(300,500));
        jf.setLocation(100,100);
        jf.add(new Test());

        jf.pack();
        jf.setVisible(true);
    }
}

编辑:应该包括这一点,第一个评论者是对的。

错误是找不到符号,指的是 g2d 或 g,无论哪个。我认为这意味着绘图只能发生在油漆组件内部,我必须找到一种方法来包含所有绘图说明。我想确保我只是在做一些根本错误的事情,尽管这是我第一次使用 java 中的抽象类和 2d 绘图。另外,我知道 location[0] 等不会按原样工作。让我们忽略它。

底部代码是我想要完成的(至少一开始是这样),但我正在尝试使用类似于顶部代码的东西来创建可以独立运行的多个实例。

4

1 回答 1

0
  1. Move the timer out of paintcomponent.
  2. You have declared an abstract class that has a draw method that needs a Graphics2D object to be able to draw, it has no access to it. It also makes little sense to declare a class just to hold two values (screensize and location) if that class also does stuff like drawing.
  3. To fix the issues with 2, you can either let your gladiator extend JComponent, override its paintcomponent and place your draw() code in there and add gladiator to the panel as a component.

  4. You can alternatively do active rendering, which means that you get the Graphic2D object of your canvas (the panel in this case) and take control of the rendering yourself instead of relying on swing.

Since you are working with swing I will give you a working example of what you probably intend to do. If you have more specific questions please do leave a comment.

public class Test extends JPanel {


public static abstract class graphic extends JComponent {

    public Dimension dim = new Dimension(500, 500);
    private int[] loc = new int[] { 250, 250 };

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }


    public int[] getLoc() {
        return loc;
    }

    public Dimension getDim() {
        return dim;
    }

}

public static class gladiator extends graphic {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.green);
        g2d.fillArc(getLoc()[0], getLoc()[1], 100, 100, 45, 90);
        g2d.setColor(Color.black);
        g2d.fillArc((getLoc()[0] + 50 - 10), (getLoc()[1] + 50 - 10), 20,
                20, 0, 360);
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    Test canvas = new Test();
    gladiator gladiator = new gladiator();

    canvas.add(gladiator);
    frame.getContentPane().add(canvas);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}

This renders

enter image description here

于 2013-05-29T17:48:34.253 回答