【问题标题】:Overrided paint method not being called after repaint is修复后未调用覆盖绘制方法是
【发布时间】:2017-11-08 17:38:01
【问题描述】:

我的问题本质上是创建一个具有几个不同场景的物理引擎。我想通过让每个单独运行的按钮将不同的场景合并到一个窗口中。框架工作正常,按钮显示并可以按下;但是,疼痛方法中的打印线从未发生过,从那里我得出结论,即使在重新绘制之后也没有调用绘制。我知道它们不一样,但我不明白为什么在这种情况下与其他情况相比没有访问绘画。

  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.RenderingHints;
  import java.awt.event.*;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import java.awt.Color;
  import javax.swing.JButton;

  public class PhysicsEngine extends JPanel{
     double x ,y;

     JFrame frame;
     JPanel pan;
     JButton b1;
     JButton b2;
     JButton b3;
     JButton b4;
     JButton b5;

     public static void main(String[] args){
        PhysicsEngine gui = new PhysicsEngine();
    }

     public PhysicsEngine(){
        frame = new JFrame("Ball Engine");

        pan = new JPanel();
        frame.add(pan);

        b1 = new JButton("1");
        b1.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event){
              try{
                 startFirst();
              } 
              catch(InterruptedException exception){}
           }
        });
        pan.add(b1);

        b2 = new JButton("2");
        b2.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
              try{
                 startSecond();
              } 
              catch(InterruptedException exception){}
           }
        });
        pan.add(b2);


        b3 = new JButton("3");
        b3.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
              try{
                 startThird();
              } 
              catch(InterruptedException exception){}
           }
        });
        pan.add(b3);

        b4 = new JButton("4");
        b4.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e){
              try{
                 startFourth();
              } 
              catch(InterruptedException exception){}
           }
        });
        pan.add(b4);

       b5 = new JButton("5");
       b5.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e){
             try{
                startFifth();
             } 
             catch(InterruptedException exception){}
          }
       });
       pan.add(b5);

       frame.setSize(600, 600);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void paint(Graphics g) {
          super.paint(g);

          System.out.println(""+y);

          Graphics2D g2d = (Graphics2D) g;
         g2d.setColor(Color.RED);   
         g2d.fillOval((int)x, (int)y, 30, 30);
    }

     public void startFirst() throws InterruptedException{
        x = 300;

        for(int t = 1;;t++){
           //xPos= 0*t*t + 0*t + 300 this is constant at 300
           if(y>=615) break; //stops the loop when the ball is off the screen

           y = .1*t*t + 0*t + 80; //parametric equation for y
           repaint();
           Thread.sleep(10);
        }
     }

     public void startSecond() throws InterruptedException{
        x = 300;

        for(int t = 1;;t++){
           //xPos= 0*t*t + 0*t + 300 this is constant at 300
           if(y>=615) break; //stops the loop when the ball is off the screen

           y = .1*t*t - 10*t + 550; //parametric equation for y
           repaint();
           Thread.sleep(10);
        }
     }

     public void startThird() throws InterruptedException{      
        for(int t = 1;;t++){
           if(y>=615||x>=615) break; //stops the loop when the ball is off the screen

           y = .1*t*t - 10*t + 550; //parametric equation for y
           x = 0*t*t + 5*t + 50; //parametric equation for x
           repaint();
           Thread.sleep(10);
        }
     }

     public void startFourth() throws InterruptedException{      
        for(int t = 1;;t++){
           //xPos= 0*t*t + 0*t + 300 this is constant at 300
           if(y>=615||x>=615) break; //stops the loop when the ball is off the screen

           y = t*t*t + 50; //given parametric equation for y
           x = t - 4; //given parametric equation for x
           repaint();
           Thread.sleep(10);
        }
     }

     public void startFifth() throws InterruptedException{      
        for(int t = 1;t<500 /* goes for 5 seconds */;t++){        
           y = 200*Math.sin(t) + 300; //given parametric equation for y
           x = 200*Math.cos(t) + 300; //given parametric equation for x
           repaint();
           Thread.sleep(10);
        }
     }
  }

【问题讨论】:

    标签: java swing graphics


    【解决方案1】:

    基本问题是您要覆盖 PhysicsEngine 类的 paint() 方法。但是您永远不会将此类的实例添加到框架中。

    但是,更大的问题是班级的结构。您的主类不应该只是为了创建 JFrame 而扩展 JPanel。创建框架的逻辑应该在 main() 方法中,然后你的 PysicsEngine 面板应该包含你想要为你的框架构建的所有组件。此外,自定义绘画应该通过覆盖 paintComponent(...) 方法而不是 paint() 方法来完成。

    阅读 Custom Painting 上的 Swing 教程部分,了解基本绘画信息和演示。

    然后,您可以查看教程中的其他部分,以更好地构建代码。例如,How to Use Buttons 教程中的 ButtonDemo 代码将向您展示如何扩展 JPanel 并向其添加按钮。

    【讨论】:

    • 另外,repaint 只是安排一个绘制事件。由于方法直到最后一个“帧”才返回,因此除了最后一个之外,它们不会被处理。
    【解决方案2】:

    repaint() 存储刷新组件的请求,该请求将在稍后由 GUI 线程执行。 但是一个组件只有在屏幕上可见时才会被刷新,可惜您的 PhysicsEngine 类似乎没有在某些可见的 GUI 层次结构中用作可见组件。

    【讨论】:

      【解决方案3】:

      你的paint方法应该是paintComponent,并且应该调用超类的paintComponent方法。

      而且你没有将 PhysicsEngine JPanel 添加到 JFrame 中,在 JPanel 子类的构造函数中创建 JFrame 是非常糟糕的方法。我在 ma​​in 方法 中创建了一个 JFrame,并创建了一个 PhysicsEngine 对象(JPanel 子类)并将其添加到 JFrame 中。我使用 this 添加按钮,它引用 PhysicsEngine(JPanel 子类)对象。

      这是工作代码:

      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.event.*;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import javax.swing.JButton;
      
      public class PhysicsEngine extends JPanel {
      
      double x, y;
      
      JPanel pan;
      JButton b1;
      JButton b2;
      JButton b3;
      JButton b4;
      JButton b5;
      
      public static void main(String[] args) {
      
          JFrame frame = new JFrame("Ball Engine");
          PhysicsEngine gui = new PhysicsEngine();
          frame.add(gui, BorderLayout.CENTER);
          frame.setSize(600, 600);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      }
      
      public PhysicsEngine() {
      
          b1 = new JButton("1");
          b1.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent event) {
                  try {
                      startFirst();
                  } catch (InterruptedException exception) {
                  }
              }
          });
          this.add(b1);
      
          b2 = new JButton("2");
          b2.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      startSecond();
                  } catch (InterruptedException exception) {
                  }
              }
          });
          this.add(b2);
      
          b3 = new JButton("3");
          b3.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      startThird();
                  } catch (InterruptedException exception) {
                  }
              }
          });
          this.add(b3);
      
          b4 = new JButton("4");
          b4.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      startFourth();
                  } catch (InterruptedException exception) {
                  }
              }
          });
          this.add(b4);
      
          b5 = new JButton("5");
          b5.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      startFifth();
                  } catch (InterruptedException exception) {
                  }
              }
          });
          this.add(b5);
      
      }
      
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
      
          System.out.println("" + y);
      
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.RED);
          g2d.fillOval((int) x, (int) y, 30, 30);
      }
      
      public void startFirst() throws InterruptedException {
          x = 300;
      
          for (int t = 1;; t++) {
              // xPos= 0*t*t + 0*t + 300 this is constant at 300
              if (y >= 615)
                  break; // stops the loop when the ball is off the screen
      
              y = .1 * t * t + 0 * t + 80; // parametric equation for y
              repaint();
              Thread.sleep(10);
          }
      }
      
      public void startSecond() throws InterruptedException {
          x = 300;
      
          for (int t = 1;; t++) {
              // xPos= 0*t*t + 0*t + 300 this is constant at 300
              if (y >= 615)
                  break; // stops the loop when the ball is off the screen
      
              y = .1 * t * t - 10 * t + 550; // parametric equation for y
              repaint();
              Thread.sleep(10);
          }
      }
      
      public void startThird() throws InterruptedException {
          for (int t = 1;; t++) {
              if (y >= 615 || x >= 615)
                  break; // stops the loop when the ball is off the screen
      
              y = .1 * t * t - 10 * t + 550; // parametric equation for y
              x = 0 * t * t + 5 * t + 50; // parametric equation for x
              repaint();
              Thread.sleep(10);
          }
      }
      
      public void startFourth() throws InterruptedException {
          for (int t = 1;; t++) {
              // xPos= 0*t*t + 0*t + 300 this is constant at 300
              if (y >= 615 || x >= 615)
                  break; // stops the loop when the ball is off the screen
      
              y = t * t * t + 50; // given parametric equation for y
              x = t - 4; // given parametric equation for x
              repaint();
              Thread.sleep(10);
          }
      }
      
      public void startFifth() throws InterruptedException {
          for (int t = 1; t < 500 /* goes for 5 seconds */; t++) {
              y = 200 * Math.sin(t) + 300; // given parametric equation for y
              x = 200 * Math.cos(t) + 300; // given parametric equation for x
              repaint();
              Thread.sleep(10);
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多