使用按钮移动圆圈而不重叠边框

时间:2014-06-24 15:40:42

标签: java frame geometry

我必须创建一个程序,使用按钮移动圆圈,它移动,但圆圈通过框架边界。我尝试了很多东西,但我担心我应该以不同的方式组织课程,或者我错过了什么。这里是移动圆圈而没有重叠条件的代码。

Circle类。

public class Circle extends JComponent {
Ellipse2D.Double e;



public Circle ()
{
     e=new Ellipse2D.Double (30,30,50,50);

    }

public void paintComponent (Graphics g)
{
    Graphics2D g2=(Graphics2D) g;
    g2.setColor(Color.RED);
    g2.fill(e);

}



public void moveUp()
{

    e.setFrameFromCenter(e.getCenterX(), e.getCenterY()-10,e.getX(), e.getY()-10);
    repaint();
}

public void moveDown()
{
    e.setFrameFromCenter(e.getCenterX(), e.getCenterY()+10,e.getX(), e.getY()+10);
    repaint();
}

public void moveRight ()
{
    e.setFrameFromCenter(e.getCenterX()-10, e.getCenterY(),e.getX()-10, e.getY());
    repaint();

}

public void moveLeft()
{
    e.setFrameFromCenter(e.getCenterX()+10, e.getCenterY(),e.getX()+10, e.getY());
    repaint();

}
}

主要课程

    public class Main{


public static void main(String[] args) {
    final JButton button1;
    final JButton button2;
    final JButton button3;
    final JButton button4;
    JPanel panel;
    final JFrame frame=new JFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,200);
    final Circle c=new Circle();
    frame.add(c);


    button1=new JButton(" Right ");
     button2=new JButton(" Left ");
     button3=new JButton(" Up ");
     button4=new JButton(" Down ");
     panel=new JPanel();
     panel.setLayout(new BorderLayout());
     panel.add(button1,BorderLayout.WEST);
     panel.add(button2,BorderLayout.EAST);
     panel.add(button3,BorderLayout.NORTH);
     panel.add(button4,BorderLayout.SOUTH);
     frame.add(panel,BorderLayout.EAST);




    class ListenerClass implements ActionListener {
     public void actionPerformed (ActionEvent e)
     {

         if (e.getSource()==button3)
         {
             c.moveUp();
         }

         else if (e.getSource()==button4)
         {
             c.moveDown();
         }
         else if (e.getSource()==button1)
         {
             c.moveRight();
         }
         else if (e.getSource()==button2)
         {
             c.moveLeft();
         }


     }
 }

ListenerClass listen=new ListenerClass();
button1.addActionListener (listen);
button2.addActionListener (listen);
button3.addActionListener (listen);
button4.addActionListener (listen);
}

    }

1 个答案:

答案 0 :(得分:1)

所以你需要做的就是检查边界。基本上你需要一个简单的碰撞/重叠检查。这是对圆圈类的一个小改动,应该有助于详细说明。

double moveSpeed = 10;
public void moveUp() {
    if(e.getBounds().getMinY() - moveSpeed >= 0){  // make sure the future location of the circle is within bounds of visual frame
        e.setFrameFromCenter(e.getCenterX(), e.getCenterY() - moveSpeed, e.getX(),
            e.getY() - moveSpeed);
    }
    repaint();
}

现在这个答案并没有真正完成。例如,如果圆的顶部距离屏幕顶部9个像素,则会发生什么情况ieegetBounds.getMinY == 9然后,因为它检查10的步骤,它将看到e.getBounds.getMinY - moveSpeed = -1&lt ; 0不会让它移动。您可以使用Math.min(double x,double y)解决此问题。在这种情况下,在圆圈向下移动的情况下,Math.max(双倍x,双倍y)。

    double moveSpeed = 10;
    public void moveUp() {
        moveDistance = Math.min(y, moveSpeed);
        if(e.getBounds().getMinY() - moveDistance >= 0){  // make sure the future location of the circle is within bounds of visual frame
            e.setFrameFromCenter(e.getCenterX(), e.getCenterY() - moveDistance, e.getX(),
                e.getY() - moveDistance);
        }
        repaint();
    }