使用Swing计时器,
class GamePanel extends JPanel implements ActionListener{ Timer timer=new Timer(1000, this); public GamePanel() { timer.start();// Start the timer here. } public void actionPerformed(ActionEvent ev){ if(ev.getSource()==timer){ repaint();// this will call at every 1 second } }
@H_404_5@解决方法
我想知道如何每x秒重绘和更新JPanel的背景…这是我的代码:
package view; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private final JLabel score; private final static String[] BACKGROUND_COLORS = {"black","blue","darkpurple","purple"}; private int i = 0; public GamePanel() { this.score = new JLabel("Score: "); this.score.setBounds(0,40,20); this.score.setOpaque(false); this.score.setForeground(Color.GREEN); this.add(score); } @Override protected void paintComponent(final Graphics g) { super.paintComponent(g); //Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[i] + "Background.png")).getImage(); //g.drawImage(background,null); Timer timer = new Timer(1000,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[i] + "Background.png")).getImage(); g.drawImage(background,null); revalidate(); repaint(); System.out.println("trying my timer"); i++; if (i == 4) { i = 0; } } }); timer.start(); } }
我的代码有2个问题:1-JPanel根本没有被绘制。2-第一张可以打印,然后每次打印数量增加一倍。有什么建议吗?先感谢您
更新:我以这种方式解决了这个问题:
package view; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private final JLabel score; private int i = 0; private final static String[] BACKGROUND_COLORS = {"black","purple"}; private final static int DELAY = 10000; public GamePanel() { this.score = new JLabel("Score: "); this.score.setBounds(0,20); this.score.setOpaque(false); this.score.setForeground(Color.GREEN); this.add(score); Timer timer = new Timer(DELAY,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { i++; if (i == 4) { i = 0; } } }); timer.start(); } @Override protected void paintComponent(final Graphics g) { super.paintComponent(g); Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[this.i] + "Background.png")).getImage(); g.drawImage(background,null); revalidate(); repaint(); }
更新2:
package view; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private final JLabel score; private int currentImage; private final List<Image> backgrounds = new ArrayList<>(); private static final String[] BACKGROUND_COLORS = {"black","purple"}; private static final int DELAY = 1000; public GamePanel() { super(); this.score = new JLabel("Score: "); this.score.setBounds(0,20); this.score.setOpaque(false); this.score.setForeground(Color.GREEN); this.add(score); for (final String s : BACKGROUND_COLORS) { backgrounds.add(new ImageIcon(this.getClass().getResource("/images/" + s + "Background.png")).getImage()); } final Timer timer = new Timer(DELAY,new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { repaint(); currentImage++; if (currentImage == BACKGROUND_COLORS.length) { currentImage = 0; } } }); timer.start(); } @Override protected void paintComponent(final Graphics g) { super.paintComponent(g); g.drawImage(backgrounds.get(this.currentImage),null); }
package view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel score;
private final static String[] BACKGROUND_COLORS = {"black","blue","darkpurple","purple"};
private int i = 0;
public GamePanel() {
this.score = new JLabel("Score: ");
this.score.setBounds(0,40,20);
this.score.setOpaque(false);
this.score.setForeground(Color.GREEN);
this.add(score);
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
//Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[i] + "Background.png")).getImage();
//g.drawImage(background,null);
Timer timer = new Timer(1000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[i] + "Background.png")).getImage();
g.drawImage(background,null);
revalidate();
repaint();
System.out.println("trying my timer");
i++;
if (i == 4) {
i = 0;
}
}
});
timer.start();
}
}
package view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel score;
private int i = 0;
private final static String[] BACKGROUND_COLORS = {"black","purple"};
private final static int DELAY = 10000;
public GamePanel() {
this.score = new JLabel("Score: ");
this.score.setBounds(0,20);
this.score.setOpaque(false);
this.score.setForeground(Color.GREEN);
this.add(score);
Timer timer = new Timer(DELAY,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
i++;
if (i == 4) {
i = 0;
}
}
});
timer.start();
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
Image background = new ImageIcon(this.getClass().getResource("/images/" + BACKGROUND_COLORS[this.i] + "Background.png")).getImage();
g.drawImage(background,null);
revalidate();
repaint();
}
package view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel score;
private int currentImage;
private final List<Image> backgrounds = new ArrayList<>();
private static final String[] BACKGROUND_COLORS = {"black","purple"};
private static final int DELAY = 1000;
public GamePanel() {
super();
this.score = new JLabel("Score: ");
this.score.setBounds(0,20);
this.score.setOpaque(false);
this.score.setForeground(Color.GREEN);
this.add(score);
for (final String s : BACKGROUND_COLORS) {
backgrounds.add(new ImageIcon(this.getClass().getResource("/images/" + s + "Background.png")).getImage());
}
final Timer timer = new Timer(DELAY,new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
repaint();
currentImage++;
if (currentImage == BACKGROUND_COLORS.length) {
currentImage = 0;
}
}
});
timer.start();
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.drawImage(backgrounds.get(this.currentImage),null);
}
我想知道如何每x秒重绘和更新JPanel的背景…这是我的代码:
我的代码有2个问题:1-JPanel根本没有被绘制。2-第一张可以打印,然后每次打印数量增加一倍。有什么建议吗?先感谢您
更新:我以这种方式解决了这个问题:
更新2: