- //FrameGame.java文件
- package ui;
- import java.awt.Dimension;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- public class FrameGame extends JFrame{
-
- private static final long serialVersionUID = -6194695820531427582L;
- public FrameGame(){
- //设置标题
- this.setTitle("JAVA俄罗斯方块 By Pinger");
- //设置默认关闭属性(关闭程序)
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //设置窗口大小
- this.setSize(1200,700);
- //不允许用户改变窗口大小
- this.setResizable(false);
- //居中
- Toolkit toolkit = Toolkit.getDefaultToolkit();
- Dimension screen = toolkit.getScreenSize();
- int x = (screen.width - this.getWidth()) / 2;
- int y = (screen.height - this.getHeight()) / 2 - 32;
- this.setLocation(x,y);
- //设置默认Panel
- this.setContentPane(new PanelGame());
- }
- }
- //PanelGame.java文件
- package ui;
- import java.awt.Graphics;
- import javax.swing.JPanel;
- public class PanelGame extends JPanel{
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private Lay[] lays = null;
-
- public PanelGame(){
- lays = new Lay[]{
- new LayDataBase(40,32,334,279),
- new LayDisk(40,343,334,279),
- new LayGame(414,32,334,590),
- new LayButton(788,32,334,124),
- new LayNext(788,188,176,148),
- new LayLevel(964,188,158,148),
- new LayPoint(788,368,334,200),
- };
- }
-
- @Override
- public void paintComponent(Graphics g){
- //循环游戏层窗口
- for(int i = 0;i<lays.length;i++){
- //刷新层窗口
- lays[i].paint(g);
- }
- }
- }
- //Lay.java文件
- package ui;
- import java.awt.Graphics;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- /**
- * 绘制窗口
- * @author Pinger
- *
- */
- abstract class Lay{
- private static final int SIZE = 7;
- private static Image WINDOW_IMG = new ImageIcon("graphics/game/0.jpg").getImage();
- private static int WINDOW_W = WINDOW_IMG.getWidth(null);
- private static int WINDOW_H = WINDOW_IMG.getHeight(null);
-
- /**
- * 窗口左上角x坐标
- */
- protected int x;
-
- /**
- * 窗口左上角y坐标
- */
- protected int y;
-
- /**
- *窗口宽度
- */
- protected int w;
-
- /**
- * 窗口高度
- */
- protected int h;
-
- protected Lay(int x,int y,int w,int h){
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- }
-
- protected void creatWindow(Graphics g){
- //左上
- g.drawImage(WINDOW_IMG, x, y, x + SIZE, y + SIZE, 0, 0, SIZE, SIZE, null);
- //中上
- g.drawImage(WINDOW_IMG, x + SIZE, y, x + w - SIZE, y + SIZE, SIZE, 0, WINDOW_W - SIZE, SIZE, null);
- //右上
- g.drawImage(WINDOW_IMG, x + w - SIZE, y, x + w, y + SIZE, WINDOW_W - SIZE, 0, WINDOW_W, SIZE, null);
- //左中
- g.drawImage(WINDOW_IMG, x, y + SIZE, x + SIZE, y + h - SIZE, 0, SIZE, SIZE, WINDOW_H - SIZE, null);
- //中
- g.drawImage(WINDOW_IMG, x + SIZE, y + SIZE, x + w - SIZE, y + h - SIZE, SIZE, SIZE, WINDOW_W - SIZE, WINDOW_H - SIZE, null);
- //右中
- g.drawImage(WINDOW_IMG, x + w - SIZE, y + SIZE, x + w, y + h - SIZE, WINDOW_W - SIZE, SIZE, WINDOW_W, WINDOW_H - SIZE, null);
- //左下
- g.drawImage(WINDOW_IMG, x, y + h - SIZE, x + SIZE, y + h, 0, WINDOW_H - SIZE, SIZE, WINDOW_H, null);
- //中下
- g.drawImage(WINDOW_IMG, x + SIZE, y + h - SIZE, x + w - SIZE, y + h, SIZE, WINDOW_H - SIZE, WINDOW_W - SIZE, WINDOW_H, null);
- //右下
- g.drawImage(WINDOW_IMG, x + w - SIZE , y + h - SIZE, x + w, y + h, WINDOW_W - SIZE, WINDOW_H - SIZE, WINDOW_W, WINDOW_H, null);
- }
-
- abstract public void paint(Graphics g);
-
- }
- //Main.java文件
- package main;
- import ui.FrameGame;
- public class Main {
- public static void main(String[] args){
- new FrameGame() .setVisible(true);
- }
- }
复制代码
|
|