package game;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
public class Game extends JPanel{
public static final int WIDTH = 400;
public static final int HEIGHT = 600;
int intervel = 3000;
public static BufferedImage background;
int ROW = WIDTH/background.getWidth();
int COL = HEIGHT/background.getHeight();
Cell[] cells1=new Cell[]{new Cell(1,3),new Cell(1,2)};
static {
try {
background = ImageIO.read(Game.class.getResource("background.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintCell(Graphics g){
for(int i=0;i<2;i++) {g.drawImage(cells1[i].getImage(),cells1[i].getCol()*background.getWidth(),cells1[i].getRow()*background.getHeight(),null);
}
}
public void paint(Graphics g){//画笔
paintCell(g);
}
public void step(){
for(int i=0;i<cells1.length;i++){
cells1[i].drop();
}
}
private Timer timer;
public void action(){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
step();
repaint();
}
},intervel,intervel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly");
Game game = new Game();
frame.add(game);//将面板添加到窗口
frame.setSize(WIDTH,HEIGHT);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);//设置窗口可见 尽快调用paint()方法
game.action();
}
}
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
public class Game extends JPanel{
public static final int WIDTH = 400;
public static final int HEIGHT = 600;
int intervel = 3000;
public static BufferedImage background;
int ROW = WIDTH/background.getWidth();
int COL = HEIGHT/background.getHeight();
Cell[] cells1=new Cell[]{new Cell(1,3),new Cell(1,2)};
static {
try {
background = ImageIO.read(Game.class.getResource("background.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintCell(Graphics g){
for(int i=0;i<2;i++) {g.drawImage(cells1[i].getImage(),cells1[i].getCol()*background.getWidth(),cells1[i].getRow()*background.getHeight(),null);
}
}
public void paint(Graphics g){//画笔
paintCell(g);
}
public void step(){
for(int i=0;i<cells1.length;i++){
cells1[i].drop();
}
}
private Timer timer;
public void action(){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
step();
repaint();
}
},intervel,intervel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly");
Game game = new Game();
frame.add(game);//将面板添加到窗口
frame.setSize(WIDTH,HEIGHT);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);//设置窗口可见 尽快调用paint()方法
game.action();
}
}
















