图片放在D:/pic/的p1.png,p2.png,p3.png,台词在D盘的lines.txt
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
private static final int Width = 1000;
private static final int Height = 600;
private static JFrame frame = null;
private static String content = null;
public GameFrame() throws Exception {
frame = new JFrame("");
frame.setLocationRelativeTo(null);
frame.setSize(Width, Height);
frame.setLocation(100, 30);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
BufferedReader reader = new BufferedReader(new FileReader("D:\\lines.txt"));
while((content=reader.readLine())!=null) {
JPanel panel = new GamePanel();
panel.setBounds(0, 0, Width, Height);
frame.getContentPane().add(panel);
frame.setVisible(true);
Thread.sleep(1000);
}
}
class GamePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon("D:\\pic\\bg.jpg");
g.drawImage(icon.getImage(), 0, 0, Width, Height, this);
g.setFont(new Font("", Font.PLAIN, 50));
StringTokenizer st = new StringTokenizer(content);
String name = st.nextToken();
if(name.equals("end")) {
g.setColor(Color.WHITE);
g.drawString("end", 470, 300);;
} else {
if(name.equals("p1")) {
icon = new ImageIcon("D:\\pic\\p1.png");
g.drawImage(icon.getImage(), 100, 50, 200, 300, this);
String words = st.nextToken();
g.setColor(Color.CYAN);
g.drawString(words, 100, 450);
} else if(name.equals("p2")) {
icon = new ImageIcon("D:\\pic\\p2.png");
g.drawImage(icon.getImage(), 700, 50, 200, 300, this);
String words = st.nextToken();
g.setColor(Color.PINK);
g.drawString(words, 500, 450);
} else if(name.equals("p3")) {
icon = new ImageIcon("D:\\pic\\p3.png");
g.drawImage(icon.getImage(), 450, 50, 200, 300, this);
String words = st.nextToken();
g.setColor(Color.GREEN);
g.drawString(words, 450, 450);
}
}
}
}
public static void main(String[] args) throws Exception {
new GameFrame();
}
}
posted on 2015-06-08 15:59
marchalex 阅读(290)
评论(0) 编辑 收藏 所属分类:
java小程序