Swing 应用程序无法正确显示 gif 背景
Swing application can't display a gif background properly
所以我试图在我的 java 应用程序中显示 GIF 背景,但 GIF 似乎无法正确显示
That's the GIF 但是当我在我的应用程序中显示它时,像素到处乱飞,看起来一点也不像原始 GIF
代码:
public class PlayerPanel extends JPanel
{
Player player ;
Image img;
boolean moveFlag=false;
private HashMap<Character,Integer> keyMap=new HashMap<>();
public PlayerPanel()
{
img = (new ImageIcon("assets\space.gif")).getImage(); //background
player = new Player(this);
keyMap.put('w',0);
keyMap.put('s',0);
keyMap.put('a',0);
keyMap.put('d',0);
addKeyListener(new KA());
setFocusable(true);
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,getWidth(),getHeight(),null);
}
主要:
public class Main {
public static void main(String[] args) throws InterruptedException {
JFrame f=new JFrame("Asteroids Game");
PlayerPanel playerPanel=new PlayerPanel();
f.add(playerPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,700);
f.setResizable(true);
f.setVisible(true);
f.setFocusable(false);
}
显然是尺寸问题,图片尺寸有点小
无论如何,我设法通过将图像调整为更大的分辨率来修复错误
所以我试图在我的 java 应用程序中显示 GIF 背景,但 GIF 似乎无法正确显示 That's the GIF 但是当我在我的应用程序中显示它时,像素到处乱飞,看起来一点也不像原始 GIF
代码:
public class PlayerPanel extends JPanel
{
Player player ;
Image img;
boolean moveFlag=false;
private HashMap<Character,Integer> keyMap=new HashMap<>();
public PlayerPanel()
{
img = (new ImageIcon("assets\space.gif")).getImage(); //background
player = new Player(this);
keyMap.put('w',0);
keyMap.put('s',0);
keyMap.put('a',0);
keyMap.put('d',0);
addKeyListener(new KA());
setFocusable(true);
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img,0,0,getWidth(),getHeight(),null);
}
主要:
public class Main {
public static void main(String[] args) throws InterruptedException {
JFrame f=new JFrame("Asteroids Game");
PlayerPanel playerPanel=new PlayerPanel();
f.add(playerPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024,700);
f.setResizable(true);
f.setVisible(true);
f.setFocusable(false);
}
显然是尺寸问题,图片尺寸有点小 无论如何,我设法通过将图像调整为更大的分辨率来修复错误