我想动画放置在JLabel
中的图片,就像您在下面看到的那样。
我的动画有问题。代码不会产生任何错误,但它只能无限期地在void
中运行。
阶级障碍:
package imageTest;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Obstacle extends JPanel {
//Position
int posX,posY;
public int getPosX(){
return this.posX;
}
public void setPosX(int posX){
this.posX=posX;
}
public int getPosY(){
return this.posY;
}
public void setPosY(int posY){
this.posY=posY;
}
public int fctHasard(int borneInf,int borneSup){
int random = (int)(Math.random() * (borneSup-borneInf)) + borneInf;
return random;
}
//Image Obstacle
Image imgObstacle =new Image("C:\\Users\\antoine\\Desktop\\imgProjetObstacle.JPG");
JLabel labImgObstacle =null;
public void fctDessinerObstacle(JFrame fenPrincipale, JPanel panPrincipale) throws IOException{
int posXDepart=fctHasard(0,fenPrincipale.getWidth()),posYDepart=ImageUtil.getImageHeight(imgObstacle.adresseImg);
posX=posXDepart;
posY=posYDepart;
labImgObstacle=imgObstacle.fctAfficherImg(panPrincipale, posX,posY,ImageUtil.getImageWidth(imgObstacle.adresseImg), ImageUtil.getImageHeight(imgObstacle.adresseImg));
panPrincipale.add(labImgObstacle);
//fctMouvement(fenPrincipale, labImgObstacle ,panPrincipale,posX,posY);
}
public void fctMouvement(JFrame fenPrincipale, JLabel labImgObstacle , JPanel panPrincipale,int posX ,int posY) throws IOException{
boolean continuer=true;
while(continuer){
posY++;
labImgObstacle=imgObstacle.fctAfficherImg(panPrincipale, posX,posY,ImageUtil.getImageWidth(imgObstacle.adresseImg), ImageUtil.getImageHeight(imgObstacle.adresseImg));
panPrincipale.add(labImgObstacle);
panPrincipale.repaint();//remetlefondvierge
if(posY==fenPrincipale.getHeight()){
posX=fctHasard(0,fenPrincipale.getWidth());
posY=-ImageUtil.getImageHeight(imgObstacle.adresseImg);
//remet le fond vierge
labImgObstacle=imgObstacle.fctAfficherImg(panPrincipale, posX,posY,ImageUtil.getImageWidth(imgObstacle.adresseImg), ImageUtil.getImageHeight(imgObstacle.adresseImg));
panPrincipale.add(labImgObstacle);
}
}
}
}
EDIT1:
答案 0 :(得分:0)
我试着理解你的问题,将图像移动到JLabel中吗? 在这种情况下,我尝试将图像作为图标放入Jlabel并在点击JPanel时移动,如下所示:
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
public class Main extends JPanel {
public JLabel label1 ;
private int x = 100;
private int y = 5;
public Main() {
setLayout(null);
setPreferredSize( new Dimension(640, 480 ) );
addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
x = x + 10;
y = y + 10;
label1.setBounds(x + getInsets().left, y + getInsets().top, label1.getPreferredSize().width, label1.getPreferredSize().height);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
ImageIcon icon = createImageIcon("middle.gif", "a pretty but meaningless splat");
label1 = new JLabel("Image and Text",icon, JLabel.CENTER);
add(label1);
label1.setBounds(x + getInsets().left, y + getInsets().top, label1.getPreferredSize().width, label1.getPreferredSize().height);
}
protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = Main.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Main());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
可能是开始制作动画的开始吗?