JLabel的动画的JPanel [英] JLabel animation in JPanel

查看:191
本文介绍了JLabel的动画的JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搔抓后四周,我发现这是最好的通过扩展一个JLabel实现自定义图像组件。到目前为止,已经很好工作,因为我可以添加多个图像(不带的JLabel布局断裂。我只是有一个,我希望有人能回答我一个问题。


  • 我注意到,为了动画跨越我需要 setLayout的(空)屏幕的JLabel; 的setBounds 组件,然后,以最终动画的setLocation(X,Y); 。这是一个最好的做法,或者以动画组件一个可怕的方式?我计划最终使得动画类,但我不想这样做,最终不得不辞职不干了。

我已经包含了相关的code的快速回顾检查。

 进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.Graphics2D中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口javax.swing.JPanel中;
进口javax.swing.Timer中;公共类GraphicsPanel继承JPanel {
    私人定时器定时器;
    私人长期STARTTIME = 0;
    私人INT numFrames = 0;
    私人浮动FPS = 0.0;
    私人INT X = 0;    GraphicsPanel(){
        最终实体ENT1 =新实体();
        ent1.setBounds(X,0,ent1.getWidth(),ent1.getHeight());
        加(ENT1);        //必要
        setLayout的(NULL);        // GAMELOOP
        定时器=新定时器(30,新的ActionListener(){
            公共无效的actionPerformed(ActionEvent的五){
                getFPS();
                INCX();
                ent1.setLocation(X,0);
                重绘();
            }
        });
        timer.start();
    }    公共无效INCX(){X ++; }    @覆盖
    公共无效的paintComponent(图形G)
    {
        super.paintComponent方法(G);
        Graphics2D的G2 =(Graphics2D的)g.create();
        g2.setClip(0,0,的getWidth(),的getHeight());        g2.setColor(Color.BLACK);
        g2.drawString(FPS+ FPS,1,15);
    }    公共无效getFPS()
    {
        ++ numFrames;
        如果(startTime时== 0){
            STARTTIME = System.currentTimeMillis的();
        }其他{
            长currentTime的= System.currentTimeMillis的();
            长三角洲=(currentTime的 - 的startTime);
            如果(增量> 1000){
                FPS =(numFrames * 1000)/三角形;
                numFrames = 0;
                STARTTIME = currentTime的;
            }
        }
    }
}

感谢您!


解决方案

  

我注意到,为了动画
  跨我需要到屏幕上的JLabel
  setLayout的(NULL);和定义的setBounds
  组件,然后进行动画
  最终的setLocation(X,Y);.这是
  最好的做法还是一种可怕的方式
  动画组件?


那么,这是不完全正确。你可以只用位置开始播放和标签会在屏幕上移动。但是,如果你调整框架或任何那么布局管理器将被调用,标签将被重新定位和布局管理器中定义的位置,这在FlowLayout中的情况下,将顶部/左侧面板。那么动画会从该位置继续。因此,在现实中,是的,这正是你需要做什么。

我觉得做动画,因​​为秋千会自动重绘组件的最后位置的最简单的方法(恢复背景)以及油漆组件的新位置。这是所有由单个的setLocation()方法来实现。

有些人喜欢直接在面板上绘制图像做风俗画,但你是负责清理图像的老位置,这样背景重绘,然后绘制图像中的新位置。我觉得这太辛苦了。

After scratching around I found that it's best to implement a custom image component by extending a JLabel. So far that has worked great as I can add multiple "images" (jlabels without the layout breaking. I just have a question that I hope someone can answer for me.

  • I noticed that in order to animate JLabels across the screen I need to setlayout(null); and setbounds of the component and then to animate eventually setlocation(x,y);. Is this a best practice or a terrible way to animate a component? I plan on eventually making an animation class but I don't want to do so and end up having to chuck it.

I have included relevant code for a quick review check.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GraphicsPanel extends JPanel  {
    private Timer timer;
    private long startTime = 0;
    private int numFrames = 0;
    private float fps = 0.0f;
    private int x = 0;

    GraphicsPanel() {
        final Entity ent1 = new Entity();
        ent1.setBounds(x, 0, ent1.getWidth(), ent1.getHeight());
        add(ent1);

        //ESSENTIAL
        setLayout(null);

        //GAMELOOP
        timer = new Timer(30, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getFPS();
                incX();
                ent1.setLocation(x, 0);
                repaint();
            }
        });
        timer.start();
    }

    public void incX() { x++; }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setClip(0, 0, getWidth(), getHeight());

        g2.setColor(Color.BLACK);
        g2.drawString("FPS: " + fps, 1, 15);
    }

    public void getFPS()
    {
        ++numFrames;
        if (startTime == 0) {
            startTime = System.currentTimeMillis();
        } else {
            long currentTime = System.currentTimeMillis();
            long delta = (currentTime - startTime);
            if (delta > 1000) {
                fps = (numFrames * 1000) / delta;
                numFrames = 0;
                startTime = currentTime;              
            }
        }
    }
}

Thank you!

解决方案

I noticed that in order to animate JLabels across the screen I need to setlayout(null); and setbounds of the component and then to animate eventually setlocation(x,y);. Is this a best practice or a terrible way to animate a component?

Well, this is not entirely true. You can just play with the location and the label will move around the screen. However, if you ever resize the frame or anything then the layout manager will be invoked and the label will be repositioned and the location defined by the layout manager which in the case of a FlowLayout will be the top/left of the panel. Animation will then continue from that location. So in reality, yes, this is exactly what you need to do.

I find it the easiest way to do animation since Swing will automatically repaint the "last" location of the component (to restore the background) as well as paint the new location of the component. This is all achieved by a single setLocation() method.

Some people like to do custom painting by drawing the image directly on the panel, but then you are responsible for clearing the old location of the image so the background is redrawn and then draw the image in the new location. I find this too much work.

这篇关于JLabel的动画的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆