简体   繁体   English

对于Jpanel和Jframe,重绘方法有何不同?

[英]How repaint method differs for a Jpanel and Jframe?

i have the following code for animating a ball from top left corner towards the bottom right corner. 我有以下代码可为从左上角到右下角的球设置动画。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MainFrame{ 
    int i=0,j=0;
    JFrame frame = new JFrame();
    public void go(){   
         Animation anim = new Animation();
         anim.setBackground(Color.red);//Why color is not changing to red for the panel.
         frame.getContentPane().add(anim);
         frame.setVisible(true);
         frame.setSize(475,475);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         for(i=0,j=0;i<frame.getHeight()&&j<frame.getWidth();++i,++j){
               anim.repaint();//Main problem is here,described below.
               try{
                   Thread.sleep(50);
               }
               catch(Exception ex){}
         }
    }
    public static void main(String[] args) {
         MainFrame mf = new MainFrame();
         mf.go();
    }

    class Animation extends JPanel{
         public void paintComponent(Graphics g){
             Graphics2D g2d = (Graphics2D)g;
             g.fillOval(i,j,25,25);
         }
    }
}

Questions 问题

  1. When i do anim.repaint() inside the method go i don't get the ball animating from top left corner to bottom right corner but it gets smeared down the path.But if i replace it with frame.repaint() i get the desired result that is a moving ball.So what is the difference between these two calls to repaint ? 当我做anim.repaint()方法中go ,我不把球从左上角到动画右下角,但它被涂下来path.But如果我更换frame.repaint()我得到的预期的结果是移动的球。那么这两次repaint调用之间有什么区别?
  2. Why the color of panel is not changing after anim.setBackground(Color.red); 为什么anim.setBackground(Color.red);之后面板的颜色没有改变anim.setBackground(Color.red); in go method? go方法?
  3. If you run this programe you will find that the ball is not exactly going at the bottom edge,so how can i acheive that? 如果运行此程序,您会发现球不完全在底部边缘,那么我该如何做到?

);//Why color is not changing to red for the panel ); //为什么面板的颜色没有变为红色

You should always invoke super.paintComponent(g) when you override the paintComponent(...) method. 覆盖paintComponent(...)方法时,应始终调用super.paintComponent(g) The default code is responsible for painting the background. 默认代码负责绘制背景。

but it gets smeared down the path 但它被涂抹在小路

Same answer as above. 与上述相同的答案。 You need the background to be painted so that all the old paintings are removed. 您需要绘制背景,以便删除所有旧画。

the ball is not exactly going at the bottom edge 球不完全在底部边缘

If you mean the ball is not on an exact diagonal on the panel, that is because you set the size of the frame manually and you did not account for the size of the titlebar and borders. 如果您的意思是球不在面板上的对角线上,那是因为您手动设置了框架的大小,而没有考虑标题栏和边框的大小。 If you want the panel to be (475, 475) then override the getPreferredSize() method of the panel to return that dimension. 如果要使面板为(475,475),则重写面板的getPreferredSize()方法以返回该尺寸。 Then in your code you replace the frame.setSize() with frame.pack() . 然后在代码中,将frame.setSize()替换为frame.pack()

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

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