2016-03-08 39 views
0

我有这个程序,当用户点击面板时,椭圆形状将向下移动面板。我使用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()); 
     } 
    } 

回答

1

的问题是,yPosition有没有关系一旦形状被制作出来(这与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()); 
} 
+0

当椭圆向下移动时,我可以看到没有椭圆,但是当它向上时,我看不到椭圆的移动。我所做的就是分配ypos = yPosition及其工作。但点击面板后创建的第一个椭圆将消失,第二个将生成动画。我想要的是我可以看到他们都向下和向上动画。我怎样才能做到这一点? –

+0

我有多个椭圆滑落。如果您将yPosition分配给ypos,则它将不执行任何操作,因为值不会更改 – MadProgrammer

+0

谢谢。但我怎么能做到这一点? –

1

您通过sleep()阻止Event Dispather Thread。改为使用javax.swing.Timer。在actionPerformed()变化y和调用重绘()

UPDATE:

将重绘()在SwingUtilities.invokeAndWait()让EDT进行重绘

+0

谢谢。但我不能使用Timer,因为这不是要求。我需要使用线程。 –

+0

@StanislavL Stas invokeAndWait()需要超出EDT – mKorbel

+1

'repaint'由它的本质是线程安全的,重绘会在事件队列上独立发布一个“重绘事件” – MadProgrammer

0
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();