简体   繁体   English

如何在JPanel上移动形状?

[英]How to move shape across JPanel?

Right now I'm working on some practice problems for a Computer science test and I've run into one that is giving me nothing but trouble. 现在,我正在为计算机科学测试解决一些实践问题,但遇到了一个麻烦,却没有给我带来任何好处。 I understand swing for the most part but I don't understand how to create and move a shape across a Panel. 我大部分时间都了解挥杆动作,但不了解如何在面板上创建和移动形状。 This is what I have so far: 这是我到目前为止的内容:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingStarting extends JFrame {
     public JPanel innerPanel; // panel containing moving shape
    public JButton pauseResumeButton;
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public int direction = LEFT;
    // The dimensions of the inner panel. To simplify this problem,
    // assume the panel will always have these dimensions.
    public static final int PANEL_WIDTH = 600;
    public static final int PANEL_HEIGHT = 400;

    public Timer movementTimer = new Timer(10,new TimerListener());

    public SwingStarting() {

      innerPanel = new ShapePanel();
     innerPanel.setPreferredSize(
     new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
     innerPanel.setBorder(
     BorderFactory.createLineBorder(Color.BLACK, 2));
     pauseResumeButton = new JButton("pause");

     add(innerPanel, BorderLayout.CENTER);
     JPanel buttonPanel = new JPanel(new FlowLayout());
     buttonPanel.add(pauseResumeButton);
     add(buttonPanel, BorderLayout.SOUTH);

     setDefaultCloseOperation(EXIT_ON_CLOSE);
     pack();

     setVisible(true);
     movementTimer.start();
    } // end constructor

    public class ShapePanel extends JPanel {
       public void paint(Graphics gc) {
         super.paintComponent(gc);
         int circleX = 0;
         int circleY = 100;
         gc.setColor(Color.RED);
         gc.fillOval(circleX,circleY,20,20);
        }
   } // end inner class



   public class TimerListener implements ActionListener {
     public void actionPerformed(ActionEvent e) {
        } // end actionPerformed
   } // end inner class

   public static void main(String args[]) {
     new SwingStarting();
    } // end main
 }// end class

So far I've created a small red circle. 到目前为止,我已经创建了一个小红色圆圈。 But how do I make it cross the screen horizontally? 但是,如何使其水平跨屏幕? Any help is greatly appreciated. 任何帮助是极大的赞赏。

In your panel class why not make a timer with an Action listener? 在您的小组课程中,为什么不使用Action侦听器创建计时器?

    // Make the shape here or earlier whenever you want.
    // By the way, I would change ShapePanel's paint method to paintComponent because it extends JPanel not JFrame
    // Create the object by giving ShapePanel a constructor
    ShapePanel s = new ShapePanel();
    ActionListener listener = new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            // IN HERE YOU MOVE THE SHAPE
            s.moveRight();
            // Use any methods for movement as well.
            repaint();
        }
    };
    Timer timer = new Timer(5, listener);
    timer.start();

Also, because you are using swing you want to make sure you do all your motions and things on one EDT. 另外,由于您使用的是挥杆动作,因此您要确保在一个EDT上进行所有动作和事情。 Try using this in your main method instead of making a new SwingStarting 尝试在主要方法中使用此方法,而不要制作新的SwingStarting

public static void main(String[] args)
{
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() 
        {
           createAndShowGUI();
        }
    });
}

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

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