简体   繁体   English

从JFrame中的类绘制很多相同的对象

[英]Drawing a lot of same objects from a class in JFrame

Answer is written by me in the bottom of the question 答案是我在问题的底部写的

I want to get a lot of 'Ates' objects in a frame. 我想在框架中获得很多“ Ates”对象。 I tried a lot of examples but always failed. 我尝试了很多示例,但始终失败。

In this context, I want to see a lot of rectangles which are going to left. 在这种情况下,我想看到很多矩形将要离开。 However, there is just one and it is going faster and faster... 但是,只有一个,并且它的运行速度越来越快...

It does not show more than one object at the same time. 它不能同时显示多个对象。 Can you tell me what is the problem? 你能告诉我是什么问题吗?

I used this code: 我使用以下代码:

public class GamePanel extends JPanel
{
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.BLACK);
        for(Ates a1 : StartGame.alist) // alist is an arraylist for Ates class objects
        {
            g.fillRect(a1.getX(), a1.getY(), 20, 20);
        }
        ...

Example creating: 创建示例:

public void sentAtes()
{
    r = rand.nextInt(471)+60;
    Ates a = new Ates(r);
    alist.add(a);
}

Ates class: 雅典班:

public Ates(int a)
{
    x = 700;
    y = a;
}

public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

public void setX( int a )
{
    x = a;
}

public void setY( int a )
{
    y = a;
}

StartGame class: StartGame类:

public class StartGame extends JFrame implements KeyListener, ActionListener
{
    protected static ArrayList<Ates> alist = new ArrayList<Ates>();
    public static int cen = 0;
    ...
    public StartGame()
    {
        jp = new GamePanel();
        add(jp);
        ... 
        int delay = 10;
        ActionListener taskPerformed = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                cen++;
                if(cen > 50)
                {
                    cen = 0;
                    sentAtes();
                }
                for(Ates a1 : alist)
                {
                    a1.setX(a1.getX()-1);
                }
                repaint();
            }
        };
        new Timer(delay,taskPerformed).start();
        ...

Info: If there is only one object, it is going left as expected. 信息:如果只有一个对象,它会按预期向左移动。

Answer to my question. 回答我的问题。

In Ates class, you should not use static word for variables and use this. Ates类中,不应将static单词用作变量,而应使用this. prefix to set them. 设置它们的前缀。

This should be work. 这应该是可行的。

public class Ates extends JFrame
{

    protected int x;
    protected int y;

    public Ates(int a)
    {
        this.x = 700;
        this.y = a;
    }

Ok the only other thing I can see is that your StartGame() is only initialised once. 好的,我能看到的唯一另一件事是您的StartGame()仅初始化一次。 Therefore your new Timer is only called once. 因此,您的new Timer仅被调用一次。 Because it is the timer that is causing the ActionListener to be created it is only ever created and run through once and therefore can never reach the stage where it will create another rectangle. 因为是导致ActionListener创建的计时器,所以它只能被创建并运行一次,因此永远无法到达将创建另一个矩形的阶段。 The actionPerformed() method only runs once in the program as there is no action or loop to cause it to run again. actionPerformed()方法在程序中仅运行一次,因为没有动作或循环导致其再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM