📄 newpanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.io.Serializable;
import java.util.*;
public class NewPanel extends JPanel implements ActionListener ,Runnable,KeyListener{
Block block;//方块主体类
int speed;
Sound sound; //声音类
Image icon1,icon2,icon3,icon4,icon5,bg;
ImageIcon image0,image1,image2,image3,image4,image5,image6,over;
JFrame frame;
JLabel imageLbl,scoreLbl,levelLbl;
Thread thread;
JPanel control;
JMenuBar menuBar;
JMenu game,help;
String name;
String levelType;
boolean isFirstThread;
StringBuffer helpText;
JMenuItem start,record,exit,setting,author,introduction;
Color color[];
int level;
boolean isStop;
public NewPanel()
{
initImage();
initOther();
initFrame();
}
public void initFrame()
{
frame= new JFrame("俄罗斯方块");
this.setFocusable(true);
menuBar = new JMenuBar();
game = new JMenu("游戏");
help = new JMenu("帮助");
start = new JMenuItem("Start");
record = new JMenuItem("Record");
record.addActionListener(this);
setting=new JMenuItem("Control");
setting.addActionListener(this);
exit=new JMenuItem("Exit");
author = new JMenuItem("作者");
introduction = new JMenuItem("说明");
author.addActionListener(this);
start.addActionListener(this);
introduction.addActionListener(this);
introduction.addActionListener(this);
exit.addActionListener(this);
game.add(start);
game.add(record);
game.add(setting);
game.add(exit);
help.add(author);
help.add(introduction);
frame.setJMenuBar(menuBar);
menuBar.add(game);
menuBar.add(help);
frame.setBounds(100,100,548,580);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
imageLbl = new JLabel(" ",JLabel.CENTER);
scoreLbl = new JLabel("",JLabel.CENTER);
levelLbl = new JLabel("",JLabel.CENTER);
this.setLayout(null);
imageLbl.setBounds(351,117,90,90);
imageLbl.setBackground(new Color(253,230,136));
imageLbl.setOpaque(true);
levelLbl.setBounds(350,241,52,33);
levelLbl.setBackground(new Color(209,176,131));
levelLbl.setOpaque(true);
levelLbl.setForeground(Color.red);
scoreLbl.setBounds(409,243,77,30);
scoreLbl.setBackground(new Color(209,176,131));
scoreLbl.setOpaque(true);
scoreLbl.setForeground(Color.red);
add(imageLbl);
add(scoreLbl);
add(levelLbl);
helpText=new StringBuffer();
helpText.append("使用说明\n");
helpText.append("通过上下左右控制方块的变换\n");
helpText.append("空格为加速,回车是暂停,具体的操作请见控制面板\n");
this.addKeyListener(this);
frame.add(this,"Center");
frame.setVisible(true);
}
public String setLevel()//设置特定的等级
{
switch(block.score/100)
{
case 0 : levelLbl.setText("工兵");break;
case 1 : levelLbl.setText("排长"); break;
case 2 :
case 3 : levelLbl.setText("连长");break;
case 4 :
case 5 : levelLbl.setText("营长");break;
case 6 :
case 7 : levelLbl.setText("团长");break;
case 8 : levelLbl.setText("军长");break;
case 9 : levelLbl.setText("司令");break;
}
levelType=levelLbl.getText();
return levelType;
}
public void setNext(int random)//creat next image
{
switch (random)
{
case 0: this.imageLbl.setIcon(image0); break;
case 1: this.imageLbl.setIcon(image1); break;
case 2: this.imageLbl.setIcon(image2); break;
case 3: this.imageLbl.setIcon(image3); break;
case 4: this.imageLbl.setIcon(image4); break;
case 5: this.imageLbl.setIcon(image5); break;
case 6: this.imageLbl.setIcon(image6); break;
default : System.out.println("错误"); break;
}
}
public void initOther()
{
block = new Block(this);
block.initGame();
level=1;
name="gg";
levelType="工兵";
isStop=true;
// this.isPlayMusic=true;\
isFirstThread = true;
sound = new Sound();
speed=500;
this.setDefalultRecord();
}
public void initImage()
{
Toolkit tk= Toolkit.getDefaultToolkit();
bg=tk.getImage("image/bg.jpg");
image0=new ImageIcon("image/0.gif");
image1=new ImageIcon("image/1.gif");
image2=new ImageIcon("image/2.gif");
image3=new ImageIcon("image/3.gif");
image4=new ImageIcon("image/4.gif");
image5=new ImageIcon("image/5.gif");
image6=new ImageIcon("image/6.gif");
icon1 = tk.getImage("image/10.jpg");
over=new ImageIcon("image/over.gif");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==start)
{
if(isFirstThread)
{
Thread thread = new Thread(this);
thread.start();
isFirstThread=false;
}
block.initGame();
block.creatBlock();
block.go=true;
sound.play1();
}
if(e.getSource()==exit)
{
System.exit(0);
}
if(e.getSource()==this.setting)
{
new ControlPanel(this);
block.go=false;
}
if(e.getSource()==this.record)
{
new RecordPanel(getRecord());
}
if(e.getSource()==author)
{
JOptionPane.showMessageDialog(null, "俄罗斯方块 V2.0 \n开发者:");
}
if(e.getSource()==this.introduction)
{
JOptionPane.showMessageDialog(null, helpText.toString());
}
}
public void run()
{
int i=0;
boolean set=false;
while(true )
{
if(block.go)
{
set=false;
try
{
Thread.sleep(speed);
}catch(Exception e){}
block.moveDown();
this.setNext(block.random);
this.setLevel();
this.scoreLbl.setText(String.valueOf(block.score));
i++;
if(this.block.dead && !set)
{
this.setRecord();
set=true;
}
}
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(bg,0,0,this);
if(block.dead)
{
this.imageLbl.setIcon(over);
}
for(int i=0;i<18;i++)
{
for(int j=0;j<11;j++)
{
if(block.game[i][j]==1 || block.game[i][j]==2)
{
g.drawImage(icon1,j*27+34,i*27+20,this);
}
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_RIGHT)
{
block.moveRight();
}
if(e.getKeyCode()==e.VK_LEFT)
{
block.moveLeft();
}
if(e.getKeyCode()==e.VK_UP)
{
block.XuanZhuan();
}
if(e.getKeyCode()==e.VK_SPACE)
{
block.speedGo();
}
if(e.getKeyCode()==e.VK_DOWN)
{
block.moveDown();
}
if(e.getKeyCode()==e.VK_ENTER)
{
if(block.go)
{
block.go=false;
}
else
{
block.go=true;
}
}
repaint();
}
public void setDefalultRecord()
{
Person[] person = new Person[10];
LinkedList link = new LinkedList();
try
{
ObjectOutputStream objectOut= new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("hero.txt")));
for(int i=0;i<10;i++)
{
person[i]=new Person();
link.add(person[i]);
}
objectOut.writeObject(link);
objectOut.flush();
objectOut.close();
}catch(Exception e){}
}
public void setRecord()
{
LinkedList link = getRecord();
gg: for(int i=0;i<10;i++)
{
if(this.block.score>((Person)(link.get(i))).score)
{
name= (String)JOptionPane.showInputDialog(null,"恭喜您,成功进入前5名,请输入您的名字");
Person p = new Person(this.name,this.levelType,this.block.score);
link.add(i,p);
link.removeLast();
break gg ;
}
}
try
{
ObjectOutputStream objectOut= new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("hero.txt")));
objectOut.writeObject(link);
objectOut.flush();
objectOut.close();
}catch(Exception e){};
}
public LinkedList getRecord()
{
File file=new File("hero.txt");
LinkedList link=null;
boolean ishero=false;
if(file.length()==0)
{
try
{
file.createNewFile();
setDefalultRecord();
System.out.println("ggg");
} catch(Exception e){System.out.println("the file is not found ");}
}
try
{
ObjectInputStream objectIn = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("hero.txt")));
link=(LinkedList)objectIn.readObject();
objectIn.close();
}catch(Exception e){}
return link;
}
public static void main(String[] arg){
NewPanel panel=new NewPanel();
}
}
class ControlPanel extends JPanel implements ActionListener{
JRadioButton speed[];
JRadioButton diff[];
JRadioButton on,off;
JRadioButton right,left;
JPanel speedPanel,diffPanel,radioPanel,rotPanel;
ButtonGroup speedGroup,diffGroup,radioGroup,rotDir;
NewPanel panel;
public ControlPanel(NewPanel panel)
{
this.panel=panel;
initFrame();
}
public void initFrame()
{
speedPanel = new JPanel();
speedPanel.setBorder(new TitledBorder("速度"));
speedPanel.setLayout(new GridLayout(3,3,10,10));
speed = new JRadioButton[9];
speedGroup = new ButtonGroup();
for(int i=0;i<9;i++)
{
speed[i]=new JRadioButton(String.valueOf(i+1));
speedPanel.add(speed[i]);
speed[i].addActionListener(this);
speedGroup.add(speed[i]);
}
diffPanel = new JPanel(new GridLayout(2,4,10,10));
diffPanel.setBorder(new TitledBorder("难度"));
diff = new JRadioButton[8];
diffGroup= new ButtonGroup();
for(int i=0;i<8;i++)
{
diff[i]= new JRadioButton(String.valueOf(i+1));
diffPanel.add(diff[i]);
diff[i].addActionListener(this);
diffGroup.add(diff[i]);
}
radioPanel = new JPanel(new GridLayout(1,2,10,10));
radioPanel.setBorder(new TitledBorder("音乐"));
on = new JRadioButton("音乐开");
off = new JRadioButton("音乐关");
radioPanel.add(on);
radioPanel.add(off);
radioGroup = new ButtonGroup();
radioGroup.add(on);
radioGroup.add(off);
on.addActionListener(this);
off.addActionListener(this);
this.rotPanel = new JPanel(new GridLayout(1,2,10,10));
this.rotPanel.setBorder(new TitledBorder("旋转方向"));
right = new JRadioButton("顺时针");
right.addActionListener(this);
left = new JRadioButton("逆时针");
left.addActionListener(this);
this.rotDir = new ButtonGroup();
this.rotDir.add(right);
this.rotDir.add(left);
this.rotPanel.add(right);
this.rotPanel.add(left);
this.setLayout(new GridLayout(4,1,10,10));
this.add(speedPanel);
this.add(diffPanel);
this.add(radioPanel);
this.add(this.rotPanel);
frame = new JFrame("控制面板");
frame.setSize(300,350);
frame.add(this,"Center");
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
for(int i=0;i<9;i++)
{
if(e.getSource()==speed[i])
{
panel.speed=(i+1)*100;
}
}
for(int i=0;i<8;i++)
{
if(e.getSource()==diff[i])
{
panel.block.diff=i+1;
panel.block.initGame();
}
}
if(e.getSource()==on)
{
panel.sound.play1();
}
if(e.getSource()==off)
{
panel.sound.stop();;
}
if(e.getSource()==right)
{
panel.block.rotDirection=1;
}
if(e.getSource()==left)
{
panel.block.rotDirection=2;
}
}
JFrame frame;
}
class RecordPanel extends JPanel {
JTable table;
private String[] columnNames = { "姓名", "级别", "得分"};
Object object[][];
Block block;
LinkedList link;
public RecordPanel(LinkedList link)
{
frame = new JFrame();
this.link=link;
//table = new JTable(cells,columnNames);
object=new Object[10][3];
showRecord();
// table.setSize(100,100);
add(new JScrollPane(table), BorderLayout.CENTER);
frame.add(this);
frame.setBounds(200,200,200,220);
// frame.setResizable(false);
frame.setVisible(true);
}
public void showRecord()
{
for(int i=0;i<10;i++)
{
object[i][0]=((Person)link.get(i)).name;
// System.out.println(((Person)link.get(i)).name);
object[i][1]=((Person)link.get(i)).level;
object[i][2]=((Person)link.get(i)).score;
}
table = new JTable(object,columnNames);
table.setPreferredScrollableViewportSize(new Dimension(150,170));
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// table.setPreferredSize(new Dimension(50,50));
}
JFrame frame;
}
class Person implements Serializable{
String name="gg";
String level;
int score;
public Person(String name,String level,int score)
{
this.name=name;
this.level=level;
this.score=score;
}
public Person()
{
this.name="gg";
this.level="工兵";
this.score=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -