📄 gamepanel.java
字号:
package gameFrame;
import java.awt.Color;
import java.awt.event.*;
import java.awt.*;
import java.awt.Image.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.awt.event.KeyEvent;
// import img.*;
public class GamePanel extends JPanel implements Runnable
{
private int MESSAGE_X =200;
private int MESSAGE_Y =200;
JButton btn1 = new JButton("start");
JButton btn2 = new JButton("pause");
JButton btn3 = new JButton("exit");
Thread t = null;
String greet;
String inputTxt;
Keyboard keyID = new Keyboard();
private Image image;
private JTextArea getTxt;
File sound = new File("ingame.mid");
Sequence seq;
Sequencer midi;
public GamePanel()
{
btn1.setBounds(30, 40 , 15, 12);
btn2.setBounds(80, 40 , 15, 12);
add(btn1);
add(btn2);
add(btn3);
StartAction startAction = new StartAction();
PauseAction pauseAction = new PauseAction();
ExitAction exitAction = new ExitAction();
btn1.addActionListener(startAction);
btn2.addActionListener(pauseAction);
btn3.addActionListener(exitAction);
greet = getString();
getTxt = new JTextArea(1, 10);
add(getTxt);
Action actlisen = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
inputTxt = getTxt.getText();
getTxt.setText("");
}
};
getTxt.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "send");
getTxt.getActionMap().put("send", actlisen);
t = new Thread(this);
// 增加图片
try
{
image = ImageIO.read(new File("game.gif"));
}
catch(IOException e)
{
e.printStackTrace();
}
try {
seq = MidiSystem.getSequence(sound);
midi = MidiSystem.getSequencer();
midi.open();
midi.setSequence(seq);
}
catch (Exception ex) {
}
}
private class StartAction implements ActionListener
{
int statue = 0;
public void actionPerformed(ActionEvent event)
{
if(statue == 0){
t.start();
statue = 1;
}
//setBackground(backgroundColor);
else
t.resume();
midi.start();
}
}
private class PauseAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//setBackground(backgroundColor);
t.suspend();
midi.stop();
}
}
private class ExitAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(1);
}
}
private String getString()
{
// System.out.println(ii);
int i = (int)(Math.random()*8);
String greeting[] = {"pig", "hello", "dog","green","table","苹果","树","房子"};// new String[4];
return greeting[i];
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Font sansbold50=new Font("SabsSerif",Font.BOLD,50);
g.setFont(sansbold50);
//g.setColor(Color.magenta);
g.drawImage(image,0,0,null);
// g.fillRect(0,0,600,600);
// g.setColor(Color.black);
g.drawString(greet, MESSAGE_X, MESSAGE_Y);
//g.Update(this.getGraphics());
}
public void run()
{
while(MESSAGE_Y <= 500)
{
try{
Thread.sleep(80);
this.repaint();
}
catch(InterruptedException e){
e.printStackTrace();
//Thread.currentThread().interrupt();
}
MESSAGE_X += 5;
if(greet.equals(inputTxt))
{
MESSAGE_X = 80;
greet = getString();
}
if(MESSAGE_X > 500){
System.out.println("output:" + greet);
System.out.println("input:" + inputTxt);
MESSAGE_X = 80;
greet = getString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -