📄 tetrix.java
字号:
import java.util.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.table.AbstractTableModel;
import java.net.*;
grant codeBase "http://localhost:8080/hw3/" {
permission java.net.SocketPermission "*", "connect";
};
/** the tetrixframe class generate the whole GUI. It is responsible for every change on the GUI */
public class tetrix extends JApplet
{
Boardpanel board;
public int c1;
public int pausecount=0;
public int countdown=0;
public boolean end = false;
public boolean newgame = false;
public boolean newrestart = false;
public boolean stop_automovedown = false;
public String showscore = "0";
JLabel Label3;
JButton pause_button;
previewpanel preview;
public void init( )
{
int c;
boolean flag =true;
block b = new line();
for(int i=0; i<block.bheight; i++)
{
for (int j=0; j<block.bwidth; j++)
{
b.tile[i][j]=' ';
}
}
Random r = new Random();
c = r.nextInt(4);
if(c==0)
{ b = new square();}
else if(c==1)
{ b = new line(); }
else if(c==2)
{ b = new tblock(); }
else if(c==3)
{ b = new lblock(); }
c1 = c;
//setSize(WIDTH, HEIGHT);
//setTitle("Tetrix");
Box panel = Box.createHorizontalBox();
board = new Boardpanel(b);
board.requestFocus();
panel.add(board,BorderLayout.SOUTH);
Container contentPane = getContentPane();
Box vbox1 = Box.createVerticalBox();
vbox1.add(Box.createVerticalStrut(10));
JLabel Label1 = new JLabel(" SCORE");
Label3 = new JLabel(" " + showscore);
vbox1.add(Label1);
vbox1.add(Label3);
vbox1.add(Box.createVerticalStrut(100));
JLabel Label2 = new JLabel(" NEXT BLOCK");
preview = new previewpanel(c1);
vbox1.add(Label2);
vbox1.add(Box.createVerticalStrut(30));
vbox1.add(preview,BorderLayout.CENTER);
Box vbox2 = Box.createVerticalBox();
JButton new_button = new JButton("New Game");
vbox2.add(new_button);
NewgameListener myListener1 = new NewgameListener();
new_button.addActionListener(myListener1);
pause_button = new JButton("Pause");
vbox2.add(pause_button);
PausegameListener myListener2 = new PausegameListener();
pause_button.addActionListener(myListener2);
vbox1.add(vbox2);
vbox1.setSize(140,480);
contentPane.add(panel,BorderLayout.WEST);
contentPane.add(vbox1,BorderLayout.EAST);
d = b;
}
public block d;
/** the NewgameListener class is in charge of the react of the Newgame button*/
class NewgameListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
newgame = true;
end = false;
board.requestFocus();
}
}
/** the PauseListener class is in charge of the react of the Pausegame button*/
class PausegameListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
pausecount++;
if(pausecount%2==0)
{
pause_button.setText("Pause");
}
else
{pause_button.setText("Continue");}
board.requestFocus();
}
}
/** class Boardpanel is responsible for the gameboard displaying */
class Boardpanel extends JPanel implements Serializable
{
ActionListener listener1=new TimeMove();
Timer t1 = new Timer(10,listener1);
// Store top = new Store();
public Boardpanel(){}
public Boardpanel(block c)
{
setBackground(Color.WHITE);
Dimension d1 = new Dimension(300,540);
setPreferredSize(d1);
KeyHandler listener = new KeyHandler();
addKeyListener(listener);
this.requestFocus();
newgame=true;
}
public boolean isFocusable() { return true; }
public void paintComponent(Graphics g)
{
super.paintComponent(g); // draw background
for(int i=0; i<d.bheight; i++)
{
for(int j=0; j<d.bwidth; j++)
{
if(d.tile[i][j]=='*')
{
g.drawRect(j*30,i*30,30,30);
g.setColor(Color.green);
g.fillRect(j*30,i*30,30,30);
}
if(d.tile[i][j]==' ')
{
g.drawRect(j*30,i*30,30,30);
g.setColor(Color.white);
g.fillRect(j*30,i*30,30,30);
}
}
}
t1.start();
}
public void continuegame()
{
repaint();
}
/** class TimeMove is making the block automatically move down every second */
class TimeMove implements ActionListener
{
public Players[] newdata = new Players[10];
public String inputValue;
/** make the block move down every second */
public void actionPerformed(ActionEvent event)
{
d.check();
repaint();
if(pausecount%2==0)
{
if(end == false)
{
if(newgame == false)
{
if((d.down == true))
{
if(countdown==100)
{d.Move_Downward();
showscore = String.valueOf(d.score);
Label3.setText(" " +showscore);
repaint();
countdown=0;
}
else
{
countdown++;
}
newrestart = false;
}//down
else{
if(newrestart == false)
{
d.erase_lines();
c1 = preview.restart(c1);
showscore = String.valueOf(d.score);
Label3.setText(" " +showscore);
repaint();
}
else
{
/**when the game is over. Show the "Game is over" message and pop the inputname dialogue. */
JOptionPane.showMessageDialog(null, "Game is over", "Message", JOptionPane.ERROR_MESSAGE);
inputValue = JOptionPane.showInputDialog("Congratulations! You are in the top 10! Please input your name :");
Players person = new Players();
String newName=inputValue;
String newScore= String.valueOf(d.score);
try{
String str = "http://localhost:8080/hw3/servlet/MyServlet?newName="+newName+"&newScore="+newScore;
URL url = new URL(str);
URLConnection con = url.openConnection();
//con.connect();
con.setDoOutput(true);
con.setDoInput(true);
//BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader( con.getInputStream() ));
String line;
int prime=0;
int m=0;
for(int n=0;n<10;n++)
{
newdata[n]=new Players();
}
while ((line = in.readLine()) != null)
{
if(prime%2==0)
{
newdata[m].setName(line);
}
else
{
newdata[m].setScore(Integer.parseInt(line));
m++;
}
;
prime++;
System.out.println();
}
in.close();
}
catch (IOException exception)
{
exception.printStackTrace();
}
scoretable mytable = new scoretable(newdata);
/** store the players' information into the top10.dat. */
mytable.pack();
mytable.setVisible(true);
mytable.show();
mytable.requestFocus();
end = true;
}
}//down
}//newgame
else
{
for(int m=0; m<block.bheight; m++)
{
for(int n=0; n<block.bwidth; n++)
{
d.tile[m][n]=' ';
}
}
d.score = 0;
showscore = String.valueOf(d.score);
c1 = preview.restart(c1);
board.requestFocus();
newgame = false;
end = false;
repaint();
}//newgame
}//end
}//pause
}
//record
}
/** class KeyHandler handle the Key reaction in this game */
class KeyHandler implements KeyListener
{
public void keyTyped(KeyEvent ke)
{
if(pausecount%2==0)
{
char keyChar = ke.getKeyChar();
if(keyChar == 'h')
{
if(d.left == true)
{
d.Move_Left();d.right=true;d.down=true;
}
}
else if(keyChar == 'j')
{
if(d.down == true)
{
d.Move_Downward();
}
else
{
d.erase_lines();
showscore = String.valueOf(d.score);
Label3.setText(showscore);
c1 = preview.restart(c1);
}
}
else if(keyChar == 'k')
{
d.rotate();
}
else if(keyChar == 'l')
{
if(d.right == true)
{
d.Move_Right();d.left=true;d.down=true;
}
else {
}
}
}
}
public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
}
}
/** class previewpanel handles the display of the next showing block */
class previewpanel extends JPanel
{
public int next;
ActionListener listener2=new refreshpreview();
Timer t1 = new Timer(1000,listener2);
public previewpanel(int choose)
{
Dimension d1 = new Dimension(60,120);
setPreferredSize(d1);
next = choose;
}
public void paintComponent(Graphics g1)
{
super.paintComponent(g1); // draw background
if(next==0)
{
g1.drawRect(10,10,60,60);
g1.setColor(Color.blue);
g1.fillRect(10,10,60,60);
}
else if(next==1)
{
g1.drawRect(10,10,90,30);
g1.setColor(Color.blue);
g1.fillRect(10,10,90,30);
}
else if(next==2)
{
g1.drawRect(10,10,90,30);
g1.setColor(Color.blue);
g1.fillRect(10,10,90,30);
g1.drawRect(40,40,30,30);
g1.setColor(Color.blue);
g1.fillRect(40,40,30,30);
}
else if(next==3)
{
g1.drawRect(10,10,30,90);
g1.setColor(Color.blue);
g1.fillRect(10,10,30,90);
g1.drawRect(40,70,30,30);
g1.setColor(Color.blue);
g1.fillRect(40,70,30,30);
}
t1.start();
}
/** the restart funciton randomly generate a new block */
public int restart(int c)
{
if(c==0)
{ d = new square();}
else if(c==1)
{ d = new line(); }
else if(c==2)
{ d = new tblock(); }
else if(c==3)
{ d = new lblock(); }
Random r = new Random();
next= r.nextInt(4);
newrestart= true;
return next;
}
class refreshpreview implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
repaint();
}
}
}
public static final int WIDTH = 480;
public static final int HEIGHT = 574;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -