JFrame

时间:2018-03-20 00:23:30

标签: java swing jframe

我正在制作一个圆圈在框架内反弹的程序,我之前已经在python中完成了这个,但我没有遇到我现在正在做的奇怪问题。

我尝试过以几种不同的方式设置我的JFrame大小,无论它总是不一致。我把它设置为(600,600),当我调用getSize()时它返回(578,544),我认为这是由于窗口的边界,但即使我在这些数字中进行硬编码,它仍然有一个奇怪的界限。每个圆圈似乎都创造了自己的界限,我无法弄清楚原因。

public Circle(Color fillColor, Color borderColor, int x, int y, int radius, String direction)
{
    super(fillColor, borderColor, x, y, (2*radius), (2*radius));
    this.radius = radius;
    this.direction = direction;
    rectShell=getBounds();
    switch(direction)
    {
        case "N" : dx = 0; dy = -1;
                   break;
        case "NW": dx = -1; dy = -1;
                   break;
        case "W" : dx = -1; dy = 0;
                   break;
        case "SW": dx = -1; dy = 1;
                   break;
        case "S" : dx = 0; dy = 1;
                   break;
        case "SE": dx = 1; dy = 1;
                   break;
        case "E" : dx = 1; dy = 0;
                   break;
        case "NE": dx = 1; dy = -1;
                   break;
    }
}


public void wallCollision(int width, int height)
{
    int x = getX();
    int y = getY();

    if(x <= 0 && (direction.equals("W") || direction.equals("NW") || direction.equals("SW")))
    {
        if(direction.equals("W"))
        {
            direction = "E";
        }
        else if(direction.equals("NW"))
        {
            direction = "NE";
        }
        else
        {
            direction = "SE";
        }
        switchDx();
    }
    if(y <= 0 && (direction.equals("N") || direction.equals("NE") || direction.equals("NW")))
    {
        if(direction.equals("N"))
        {
            direction = "S";
        }
        else if(direction.equals("NE"))
        {
            direction = "SE";
        }
        else
        {
            direction = "SW";
        }
        switchDy();
    }
    if(x+getWidths() >= 578 && (direction.equals("E") || direction.equals("NE") || direction.equals("SE")))
    {
        System.out.println("Collision");
        if(direction.equals("E"))
        {
            direction = "W";
        }
        else if(direction.equals("NE"))
        {
            direction = "NW";
        }
        else
        {
            direction = "SW";
        }
        switchDx();
    }
    if(y+getHeights() >= 544 && (direction.equals("S") || direction.equals("SW") || direction.equals("SE")))
    {
        if(direction.equals("S"))
        {
            direction = "N";
        }
        else if(direction.equals("SE"))
        {
            direction = "NE";
        }
        else
        {
            direction = "NW";
        }
        switchDy();
    }

}

这是JFrame类

public ShapeGenerator2()
{
    setSize(600,600);
    setTitle("Random Shapes");
    getContentPane().setBackground(Color.ORANGE);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    addKeyListener(new Key());

    timer = new Timer(20, this);
    timer.start();


}


@Override
public void actionPerformed(ActionEvent e)
{
    for(Circle c: circles)
    {
        System.out.println("("+c.getX()+","+c.getY()+") Moving "+c.getDirection()+" ("+(c.getX()+c.getRadius())+", "+(c.getY()+c.getRadius())+")");
        c.wallCollision(getWidth(), getHeight());
        if(circles.size()>1)
        {
            for(Circle i: circles)
            {
                if(i!=c)
                c.hitCircle(i, false);
            }
        }
        c.move();
        System.out.println(getSize());
        System.out.println("("+getContentPane().getWidth()+", "+getContentPane().getHeight()+")");
    }
}

public class Key implements KeyListener
{
    @Override
    public void keyPressed(KeyEvent e)
    {
        Random r = new Random();
        Color fillColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
        Color borderColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
        switch(e.getKeyCode())
        {
            case KeyEvent.VK_C: boolean goodCircle = false;
                                Circle ci = null;
                                while(!goodCircle)
                                {
                                    ci = new Circle(fillColor, borderColor, r.nextInt(200), r.nextInt(200), r.nextInt(150), directions[r.nextInt(directions.length)]);
                                    goodCircle = true;
                                    for(Circle c: circles)
                                    {
                                        if(ci.hitCircle(c,true))
                                        {
                                            goodCircle = false;
                                            break;
                                        }
                                    }
                                    //goodCircle = true;
                                }
                                add(ci);
                                circles.add(ci);
                                break;
        }


        setVisible(true);
    }

Ellipse,这是Circle的超类

public class Ellipse extends Shape
{
private int width;
private int height;
private Color fillColor;

public Ellipse(Color fillColor, Color borderColor, int x, int y, int width, int height)
{
    super(fillColor, borderColor, x, y);
    this.width = width;
    this.height = height;
}

public Ellipse(int x, int y, int width, int height)
{
    super(Color.WHITE, Color.BLACK, x, y);
    this.width = width;
    this.height = height;
}

public int getWidths()
{
    return width;
}

public int getHeights()
{
    return height;
}
    @Override
void draw(Graphics g)
{
    g.setColor(getFillColor());
    g.fillOval(getX(), getY(), width, height);
    g.setColor(getBorderColor());
    g.drawOval(getX(), getY(), width, height);
    g.setColor(Color.BLACK);
    g.drawRect(getBounds().x,getBounds().y,getBounds().width, getBounds().height/*getX(), getY(), width, height*/);
}

public Rectangle getBounds()
{
    return new Rectangle(getX(), getY(), width, height);
}

}

对不起所有代码。我从来没有因为看起来如此微不足道的事情而迷失方向。谢谢你的帮助。

0 个答案:

没有答案