march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0

          圖片放在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 閱讀(302) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 疏附县| 临朐县| 东安县| 景宁| 广水市| 军事| 商河县| 轮台县| 敦化市| 隆昌县| 镇康县| 威宁| 长武县| 澄城县| 桃园县| 牡丹江市| 四平市| 司法| 疏勒县| 天峨县| 仙游县| 马鞍山市| 张家川| 勐海县| 洞头县| 若羌县| 十堰市| 卓尼县| 仙桃市| 会宁县| 华容县| 叶城县| 安阳县| 清镇市| 裕民县| 东乌| 津南区| 准格尔旗| 商城县| 灵宝市| 双流县|