📄 squareapplet.java
字号:
//Download by http://www.codefans.net
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SquareApplet extends Applet implements KeyListener,Runnable,ActionListener {
private Font font=new Font("宋体",Font.PLAIN,12);//字体对象
private boolean stop=false;//设置是否停止
private Color color1=new Color(253,197,159);//设置颜色
private Color color2=new Color(252,149,80);
private Color color3=new Color(251,112,19);
private JPanel pnl=new JPanel(new BorderLayout());//
private JPanel pnlbtn=new JPanel(new BorderLayout());
private JPanel pnlScore=new JPanel(new BorderLayout());
private JLabel lblScore=new JLabel();
private JButton btnRestart=new JButton("重新开始");//按钮对象
private JButton btnStart=new JButton(" 开始 ");//
private JButton btnPause=new JButton(" 暂停 ");
private int xpos=100,ypos=0;
private SquareShape s;
private SquareShape s1;
private SquareShape s2;
private boolean fill[][]=new boolean[10][20];
private boolean next=false;//设置是否下移
private Image buffer;//画布对象
private Random r=new Random();
private int score=0;
private Thread t;//线程对象
private volatile int down=20;
public void init()
{
setBackground(Color.black);//设置背景颜色
buffer=this.createImage(200,400);//创建画布大小
addKeyListener(this);
requestFocus(true);//设置请求焦点为真
this.setLayout(new BorderLayout());//设置布局管理器
btnStart.addActionListener(this);
btnStart.setFont(font);//设置按钮的字体
btnPause.addActionListener(this);
btnPause.setFont(font);//设置按钮的字体
btnRestart.addActionListener(this);
btnRestart.setFont(font);
lblScore.setFont(font);//设置标签的字体
pnlbtn.add(btnStart,BorderLayout.SOUTH);//把按钮加到容器中
pnlbtn.add(btnRestart,BorderLayout.NORTH);
pnlbtn.add(btnPause,BorderLayout.CENTER);
pnlScore.add(lblScore,BorderLayout.SOUTH);
pnl.add(pnlbtn,BorderLayout.SOUTH);
pnl.add(pnlScore,BorderLayout.CENTER);
this.add(pnl,BorderLayout.EAST);//把容器加到窗体中
pnl.setSize(100,400);//设置容器的大小
pnl.setVisible(true);//设置容器为可见的
s1=this.getNextShape();
s=this.getNextShape();
lblScore.setText("得分: "+String.valueOf(score));
}
public void paint(Graphics g)//画方法
{ lblScore.repaint();
btnStart.repaint();
btnPause.repaint();
btnRestart.repaint();
s1.drawShape(g,250,20);
drawFrame(g);
s.drawShape();
g.drawImage(buffer,10,10,null);
}
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP)
{
if(next==true)
{
return;//***
}
s.rotate();//旋转进行计数
s.setPlace(xpos,ypos);//设置图形的基位置
while(s.getLeft()<0)//进行左调整
{
xpos+=20;
s.setPlace(xpos,ypos);
}
while(s.getRight()>200)//进行右调整
{
xpos-=20;
s.setPlace(xpos,ypos);
}
while(s.getBottom()>400)//进行底调整
{
ypos-=20;
s.setPlace(xpos,ypos);
}
if(isFilled())
{
s.contraRotate();
s.setPlace(xpos,ypos);
}
repaint();
}
else if(keyCode==KeyEvent.VK_DOWN && s.getBottom()<400)
{
ypos+=20;
s.setPlace(xpos,ypos);
int first[]=s.getFirstPlace();
int second[]=s.getSecondPlace();
int third[]=s.getThirdPlace();
int fourth[]=s.getFourthPlace();
if(isFilled())
{
ypos-=20;
s.setPlace(xpos,ypos);
setFill();
next=true;
}
else if(s.getBottom()==400)
{
fill[first[0]][first[1]]=true;
fill[second[0]][second[1]]=true;
fill[third[0]][third[1]]=true;
fill[fourth[0]][fourth[1]]=true;
next=true;
}
repaint();
}
else if(keyCode==KeyEvent.VK_LEFT && s.getLeft()>0)
{
xpos-=20;
s.setPlace(xpos,ypos);
if(isFilled())
{
xpos+=20;
s.setPlace(xpos,ypos);
}
repaint();
}
else if(keyCode==KeyEvent.VK_RIGHT && s.getRight()<200)
{
xpos+=20;
s.setPlace(xpos,ypos);
if(isFilled())
{
xpos-=20;
s.setPlace(xpos,ypos);
}
repaint();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void run()
{
while(true)
{
if(next || s.getBottom()==400)
{
setFill();
s=s1;
int t=this.getTopRow();
if(t<=0)
{
stop=true;
try
{
synchronized(this)
{
wait();
}
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}
else
{
int s;
s=this.disappear(t);
if(s==1)
{
score+=100;
}
else if(s==2)
{
score+=300;
}
else if(s==3)
{
score+=700;
}
else if(s==4)
{
score+=1500;
}
lblScore.setText("得分: "+String.valueOf(score));
}
xpos=100;
ypos=0;
next=false;
s1=this.getNextShape();
this.getGraphics().clearRect(200,0,100,100);
continue;
}
ypos+=down;
s.setPlace(xpos,ypos);
if(isFilled())
{
ypos-=down;
s.setPlace(xpos,ypos);
setFill();
next=true;
continue;
}
repaint();
try
{
if(stop)
{
synchronized(this)
{
wait();
}
}
Thread.sleep(800);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
private void setFill()
{
int first[]=s.getFirstPlace();
int second[]=s.getSecondPlace();
int third[]=s.getThirdPlace();
int fourth[]=s.getFourthPlace();
fill[first[0]][first[1]]=true;
fill[second[0]][second[1]]=true;
fill[third[0]][third[1]]=true;
fill[fourth[0]][fourth[1]]=true;
}
public void update(Graphics g)
{
for(int i=0;i<10;i++)
{
for(int j=0;j<20;j++)
{
if(!fill[i][j])
{
int x=i*20;
int y=j*20;
buffer.getGraphics().clearRect(x,y,20,20);
}
}
}
paint(g);
}
private int disappear(int r)
{
int count=0;
int topRow=r;
int currentRow=19;
Graphics g=this.getGraphics();
while(currentRow>=topRow)
{
if(isFullRow(currentRow))
{
for(int j=currentRow;j>=topRow;j--)
{
for(int k=0;k<10;k++)
{
fill[k][j]=fill[k][j-1];
}
buffer.getGraphics().clearRect(0,(j)*20,200,20);
buffer.getGraphics().copyArea(0,(j-1)*20,200,20,0,20);
}
topRow++;
count++;
currentRow=19;
}
else
{
currentRow--;
}
}
repaint();
return count;
}
private int getTopRow()
{
int topRow=21;
for(int i=0;i<20;i++)
{
for(int j=0;j<10;j++)
{
if(fill[j][i])
{
topRow=i;
i=20;
break;
}
}
}
return topRow;
}
private boolean isFullRow(int r)
{
for(int i=0;i<10;i++)
{
if(!fill[i][r])
{
return false;
}
}
return true;
}
private SquareShape getNextShape()
{
SquareShape s=null;
Graphics g=buffer.getGraphics();
int i=r.nextInt(7);
switch(i)
{
case 0:s=new Bar(g);
break;
case 1:s=new Ding(g);
break;
case 2:s=new LeftL(g);
break;
case 3:s=new ObverseZ(g);
break;
case 4:s=new RightL(g);
break;
case 5:s=new Square(g);
break;
case 6:s=new TurnZ(g);
}
return s;
}
private boolean isFilled()
{
int first[]=s.getFirstPlace();
int second[]=s.getSecondPlace();
int third[]=s.getThirdPlace();
int fourth[]=s.getFourthPlace();
if(fill[first[0]][first[1]] || fill[second[0]][second[1]] || fill[third[0]][third[1]] || fill[fourth[0]][fourth[1]])
{
return true;
}
else
{
return false;
}
}
public void actionPerformed(ActionEvent e)
{
Object btn=(JButton)e.getSource();
if(btn==btnStart)
{
if(t==null)
{
t=new Thread(this);
t.start();
}
else
{
stop=false;
synchronized(this)
{
notify();
}
}
}
else if(btn==btnPause)
{
stop=true;
}
else
{
if(t!=null)
{
score=0;
lblScore.setText("得分: "+String.valueOf(score));
for(int i=0;i<10;i++)
{
for(int j=0;j<20;j++)
{
fill[i][j]=false;
}
}
xpos=100;
ypos=0;
s=this.getNextShape();
s1=this.getNextShape();
buffer.getGraphics().clearRect(0,0,200,300);
this.getGraphics().clearRect(200,0,100,100);
stop=false;
synchronized(this)
{
notify();
}
repaint();
}
}
this.requestFocus(true);
}
private void drawFrame(Graphics g)
{
g.setColor(color1);
g.drawRect(10,10,200,400);
g.drawRect(9,9,202,402);
g.setColor(color2);
g.drawRect(8,8,204,404);
g.setColor(color3);
g.drawRect(7,7,206,406);
g.drawRect(6,6,208,408);
g.drawRect(5,5,210,410);
g.setColor(color2);
g.drawRect(4,4,212,412);
g.setColor(color1);
g.drawRect(3,3,214,414);
g.drawRect(2,2,216,416);
}
public void stop()
{
stop=true;
}
public void start()
{
stop=false;
synchronized(this)
{
notify();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -