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 閱讀(301) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 东丽区| 喀什市| 肇源县| 固原市| 双江| 三穗县| 新沂市| 怀柔区| 利津县| 宽城| 临沂市| 清新县| 札达县| 邵阳县| 叙永县| 油尖旺区| 新乐市| 东乡县| 连城县| 嘉祥县| 方山县| 军事| 布尔津县| 鲜城| 汝阳县| 赣榆县| 玉树县| 徐水县| 石林| 阳曲县| 白河县| 伊金霍洛旗| 讷河市| 茂名市| 阿荣旗| 焦作市| 金阳县| 册亨县| 万荣县| 萝北县| 义乌市|