尝试使用JFrame&;在Java中设置多个图像的动画;面板
所以我尝试用动画来旋转一个图像,让它将图像更改为一个旋转了22.5度的新图像。我通过让一个类从JFrame继承,另一个类从JPanel继承来实现这一点,但它什么都不做。这是密码尝试使用JFrame&;在Java中设置多个图像的动画;面板,java,image,swing,animation,jpanel,Java,Image,Swing,Animation,Jpanel,所以我尝试用动画来旋转一个图像,让它将图像更改为一个旋转了22.5度的新图像。我通过让一个类从JFrame继承,另一个类从JPanel继承来实现这一点,但它什么都不做。这是密码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JPanel; impor
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.*;
public class LogoAnimatorJPanel extends JPanel implements ActionListener
{
protected ImageIcon[] images = new ImageIcon[16] ;
private int currentImage = 0;
private Timer animationTimer;
public LogoAnimatorJPanel()
{
for ( int count = 0; count < images.length; count++ ){
images [count] = new ImageIcon("car/yellowCar" + count + ".jpg");
}
startAnimation();
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
images[ currentImage ].paintIcon( this, g, 50 , 50 );
currentImage = ( currentImage + 1 ) % images.length;
}
public void startAnimation()
{
animationTimer = new Timer(20, this);
animationTimer.start();
}
public void actionPerformed( ActionEvent actionEvent )
{
repaint();
}
}
有什么想法吗?这是一个工作演示。我在上面讨论的一些事情,还有一些调整。仔细阅读代码中的更改,对新的/不同的方法调用进行一些研究,并询问您是否(从该研究中)不理解我为什么要包含它们。请注意,此类包括一个
main
方法
import java.awt.Image;
import java.awt.event.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LogoAnimator {
private int currentImage = 0;
private JLabel animationDisplayLabel = new JLabel();
private Timer animationTimer;
private String prefix = "http://i.stack.imgur.com/";
private String suffix = ".png";
private String[] imageNames = {"gJmeJ", "L5DGx", "in9g1", "IucNt", "yoKxT"};
protected ImageIcon[] images = new ImageIcon[imageNames.length];
public LogoAnimator() {
for (int count = 0; count < images.length; count++) {
try {
URL url = new URL(prefix + imageNames[count] + suffix);
Image image = ImageIO.read(url);
images[count] = new ImageIcon(image);
} catch (Exception ex) { // TODO! Better exception handling!
ex.printStackTrace();
}
}
startAnimation();
}
public void startAnimation() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentImage = (currentImage + 1) % images.length;
animationDisplayLabel.setIcon(images[currentImage]);
}
};
animationDisplayLabel.setIcon(images[0]);
animationTimer = new Timer(200, listener);
animationTimer.start();
}
public JComponent getAnimationComponent() {
return animationDisplayLabel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
LogoAnimator fp = new LogoAnimator();
JFrame f = new JFrame("car");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(fp.getAnimationComponent());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
导入java.awt.Image;
导入java.awt.event.*;
导入java.net。*;
导入javax.imageio.imageio;
导入javax.swing.*;
公共类动画制作者{
私有int currentImage=0;
私有JLabel animationDisplayLabel=新JLabel();
私人定时器动画定时器;
专用字符串前缀=”http://i.stack.imgur.com/";
私有字符串后缀=“.png”;
私有字符串[]imageNames={“gJmeJ”、“L5DGx”、“in9g1”、“IucNt”、“yoKxT”};
受保护的ImageIcon[]图像=新的ImageIcon[imageNames.length];
公共标识动画师(){
对于(int count=0;count
“有什么想法吗?”1)不要扩展JFrame。喜欢组合而不是继承。2) 在JLabel
(而不是JPanel
)3中显示图像。在加载第一个图像后,设置位置并调用pack()
,而不是设置帧的大小/边界。4) 在显示涉及图像的示例时,将热链接(Java可以从URL加载图像)链接到中看到的图像。5) 在EDT上启动GUI。6) 应用程序资源在部署时将成为嵌入式资源,因此现在就开始访问它们是明智的。。。。必须通过URL而不是文件访问。有关如何形成URL的信息,请参见。7) 新图像图标(“car/yellowCar”+count+”.jpg)代码>使用ImageIO
加载图像<代码>图像图标
将以静默方式失败。8) 使用@Override
符号对应该重写已定义方法的方法进行编译时检查。9) cp.setLayout(空)代码>Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的PLAF。。。。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起用于。。。还有问题吗?如果这有助于解决问题,请告诉我。
import java.awt.Image;
import java.awt.event.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LogoAnimator {
private int currentImage = 0;
private JLabel animationDisplayLabel = new JLabel();
private Timer animationTimer;
private String prefix = "http://i.stack.imgur.com/";
private String suffix = ".png";
private String[] imageNames = {"gJmeJ", "L5DGx", "in9g1", "IucNt", "yoKxT"};
protected ImageIcon[] images = new ImageIcon[imageNames.length];
public LogoAnimator() {
for (int count = 0; count < images.length; count++) {
try {
URL url = new URL(prefix + imageNames[count] + suffix);
Image image = ImageIO.read(url);
images[count] = new ImageIcon(image);
} catch (Exception ex) { // TODO! Better exception handling!
ex.printStackTrace();
}
}
startAnimation();
}
public void startAnimation() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currentImage = (currentImage + 1) % images.length;
animationDisplayLabel.setIcon(images[currentImage]);
}
};
animationDisplayLabel.setIcon(images[0]);
animationTimer = new Timer(200, listener);
animationTimer.start();
}
public JComponent getAnimationComponent() {
return animationDisplayLabel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
LogoAnimator fp = new LogoAnimator();
JFrame f = new JFrame("car");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(fp.getAnimationComponent());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}