【问题标题】:Cannot see oval shape moving down the panel看不到面板向下移动的椭圆形
【发布时间】:2016-06-22 12:16:42
【问题描述】:

我有这个程序,当用户点击面板时,椭圆形将向下移动面板。我使用 System.out.print 打印了 yLocation(y 坐标上椭圆的位置),我可以看到值发生了变化。但问题是我无法看到椭圆形移动。

public class OvalWithThreading {

public OvalWithThreading(){
    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run() {
            JFrame frame = new JFrame("Click On The Canvas");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new OvalPane());
            frame.pack() ;
            frame.setVisible(true);
        }

    });
}
public static void main(String[] args) {
    new OvalWithThreading();
}

//Panel that will hold the oval shape
public class OvalPane extends JPanel{

    private int xPosition = 50;
    private int yPosition = 50;
    private int xSize = 50;
    private int ySize = 50;
    boolean clicked = true;
    boolean horizontalBoundary = true;
    boolean verticalBoundary = true;

    private List<Ellipse2D> shapes;

    public OvalPane(){
        shapes = new ArrayList<>();
        setBackground(Color.CYAN);
        addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                if(clicked) clicked = false;
                else if(!clicked) yPosition = 50; 

                shapes.add(new Ellipse2D.Double(xPosition, yPosition , 50, 50)); 
                System.out.print("Clicked");
                repaint();
            }

       });
   public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        for (Ellipse2D shape : shapes) {
            g2d.fill(shape);
            Rectangle2D bounds = shape.getBounds2D();
            double yPos = bounds.getY();
            double xPos = bounds.getX();
            shape.setFrame(xPos, yPos, bounds.getWidth(), bounds.getHeight());
        }
    }

【问题讨论】:

    标签: java swing arraylist awt


    【解决方案1】:

    问题是,yPosition一旦做成shape就和shape没有关系了(跟Java传参的方式有关,是个好东西),所以不管你怎么改,不会影响您的形状。

    你也有多个形状,所以你不希望它们都具有相同的值。

    正如您在previous question 中所述,您需要遍历每个形状并更新其位置,例如...

    for (Ellipse2D shape : shapes) {
        Rectangle2D bounds = shape.getBounds2D();
        double yPos = bounds.getY();
        yPos += 30;
        shape.setFrame(bounds.getX(), yPos, bounds.getWidth(), bounds.getHeight());
    }
    

    【讨论】:

    • 当椭圆向下移动时,我看不到椭圆,但当它向上移动时,我看不到椭圆移动。我所做的是分配 ypos = yPosition 及其工作。但是单击面板时创建的第一个椭圆将消失,第二个将动画化。我想要的是我可以看到它们向下和向上的动画。我怎样才能做到这一点?
    • 我有多个椭圆滑下来。如果您将 yPosition 分配给 ypos,那么它不会做任何事情,因为值不会改变
    • 谢谢。但我怎样才能做到这一点?
    • @JayGorio 实现什么?有多个球滑下屏幕?更新您的代码以使用我的代码,这就是我所做的一切
    • 是的,我做到了,这是第一个要求。然而他们改变了它。球应继续在面板上设置动画。如果球下降并超出水平或垂直边界,则球应向上移动或向左移动。
    【解决方案2】:

    您通过 sleep() 阻止了 Event Dispather 线程。请改用javax.swing.Timer。在 actionPerformed() 中更改 y 并调用 repaint()

    更新:

    将 repaint() 放在 SwingUtilities.invokeAndWait() 中,让 EDT 执行重绘

    【讨论】:

    • 谢谢。但我不能使用 Timer,因为这不是要求。我需要使用线程。
    • @StanislavL Stas invokeAndWait() 需要退出 EDT
    • repaint 本质上是线程安全的,重绘会独立发布一个“重绘事件”到事件队列中
    • 还有,yPosition和Ellipse2D的多个实例有什么关系;)
    【解决方案3】:
    for(;;){   
    if(horizontalBoundary){
        if((yPosition += 30) >= getHeight()-50){
            horizontalBoundary = false;
        }
    
    }else if(!horizontalBoundary){
        if((yPosition -= 30)<= 0){
            horizontalBoundary = true;
        }
    }
    if(verticalBoundary){
        if((xPosition += 30) >= getWidth()-50){
            verticalBoundary = false;
        }
    }else if(!verticalBoundary){
        if((xPosition -= 30) <= 0){
            verticalBoundary = true;
        }
    }
    System.out.println(yPosition + " " + xPosition);
      repaint();
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 2014-05-13
      • 2019-05-12
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多