当前位置: 代码迷 >> J2ME >> 你能看懂吗?解决方法
  详细解决方案

你能看懂吗?解决方法

热度:2874   发布时间:2013-02-25 21:38:27.0
你能看懂吗?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class GameFrame extends JFrame implements KeyListener {
private final int DEFAULT_WIDTH=500;
private final int DEFAULT_HEIGHT=700;
Random ran=new Random();
//方块形状
private int Fstate=ran.nextInt(7);
//方块状态
private int Sstate=ran.nextInt(4);
private int X=130;
private int left=130;
private int top=0;
private int[][][] shapes;
private int[]s;
private int[][]sh;
Shapes shape=new Shapes();
private JButton btnStart ,btnPause;
private boolean flag=false;
private GamePanel panel;
private int[][]map=new int[60][30];
private int tx=13;
private int ty=3;
public GameFrame()
{

shapes=shape.getShapes();
s=shapes[Fstate][Sstate];
sh=getS(s);
panel=new GamePanel();
panel.setLayout(null);
setTitle("Tetris game");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
btnStart=new JButton("开始游戏");

btnStart.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
flag=true;
btnStart.requestFocus(false);
panel.requestFocus();
}
 
}
});
btnStart.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
flag=true;
btnStart.requestFocus(false);
panel.requestFocus();
}
});
btnStart.setBounds(350, 350, 100, 30);

btnPause=new JButton("暂停");
  btnPause.addActionListener(new ActionListener()
  {
 

@Override
public void actionPerformed(ActionEvent arg0) {
flag=false;
panel.requestFocus();
}
});
  btnPause.setBounds(350, 400, 100, 30);
  panel.add(btnPause);
this.setLocationRelativeTo(null);
this.add(panel);

this.addKeyListener(this);
panel.addKeyListener(GameFrame.this);
GameThread gth=new GameThread();
Thread th=new Thread(gth);
th.start();

panel.add(btnStart);
}
public int[][] getS(int[]a)
{

int[][]sa=new int[4][4];
int n=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
sa[i][j]=a[n];
n++;
}

}

return sa;
}
private class GamePanel extends JPanel
{
  public void paintComponent(Graphics g)
  {
  Graphics2D g2d=(Graphics2D)g;
  g2d.setColor(Color.black);
  g2d.fill3DRect(20, 20, 300, 600, true);
  g2d.drawRect(350, 40, 80, 80);
 
  for(int j=0;j<sh.length;j++)
  {
for(int i=0;i<sh.length;i++)
{


if(sh[j][i]==1)
{
 
g2d.setColor(Color.RED);
g2d.fill3DRect(left+20+10*i, top+20+10*j, 10, 10, true);

}

}
  }
  for(int i=0;i<60;i++)
  {
  for(int j=0;j<30;j++)
  {
  if(map[i][j]==1)
  {
  g2d.setColor(Color.RED);
g2d.fill3DRect(left+20+10*i, top+20+10*j, 10, 10, true);
  }
 
 
  }
  }
 
  }
}
//判断是否出界
public int isOut(int x,int y)
{
int[] aa=shapes[x][y];
int[][]b=getS(aa);
for(int j=0;j<b.length;j++)
{
  for(int i=0;i<b.length;i++)
  {
 

  if(b[j][i]==1)
  {

 
  if((left+20+10*i)<20||(left+30+10*i)>300)
  相关解决方案