Package com.test.pm

Source Code of com.test.pm.PMTestFrame03

package com.test.pm;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.rosiminc.pm.game.PipesManiaGame;
import com.rosiminc.pm.game.Tile;


public class PMTestFrame03 extends JFrame {

  private static final long serialVersionUID = 1L;
  private final int WIDTH = 10;
  private final int HEIGHT = 5;
 
  private Tile[][] board;


  public PMTestFrame03(){
    super();
   
    initializeComponents();
    setVisible(true);
    pack();
   
   
    setSize(new Dimension(400,400));
    setExtendedState(MAXIMIZED_BOTH);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
 
  private void initializeComponents(){
   
    PipesManiaGame game = new PipesManiaGame(HEIGHT, WIDTH);
    board = game.getBoard();
   
    this.add(getScrollPane(), BorderLayout.CENTER);
    /*try {
      //this.add(getImagePane(), BorderLayout.CENTER);
    } catch (MalformedURLException e) {
      // T ODO Auto-generated catch block
      e.printStackTrace();
    }*/
  }
 
 
  private JPanel getImagePane() throws MalformedURLException {
    JPanel pane = new JPanel();
    JLabel label = new JLabel(new ImageIcon("img/p0.bmp"));
    pane.add(label);
    return pane;
  }

  private JPanel getScrollPane() {
    JPanel scrollPanel = new JPanel(new BorderLayout());
   
    JScrollPane scroll = new JScrollPane(getMainGridPanel(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPanel.add(scroll, BorderLayout.CENTER);
   
    return scrollPanel;
  }
  //X XX sid = 2009130904

  private JPanel getMainGridPanel() {
    JPanel panel = new JPanel(new GridLayout(HEIGHT,WIDTH,0,0));
   
    for(int row=0; row < HEIGHT; row++)
      for(int col=0; col < WIDTH; col++)
      {
        panel.add(getTile(row, col));
      }
   
    JPanel flowPanel = new JPanel();//FlowLayout
    flowPanel.add(panel);
   
    /*System.out.println(MediaTracker.ABORTED);
    System.out.println(MediaTracker.COMPLETE);
    System.out.println(MediaTracker.ERRORED);
    System.out.println(MediaTracker.LOADING);
    System.out.println();*/
   
    return flowPanel;
  }

  private Component getTile(int row, int col) {
   
    JPanel panel = new JPanel();
   
    Tile t = board[row][col];
   
   
    /*panel.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(
                0,0,0,0),
       
       
        BorderFactory.createMatteBorder(
            t.isUp()?0:3, t.isLeft()?0:3,
            t.isDown()?0:3, t.isRight()?0:3,
            (row+col)%2==0?Color.BLUE:Color.RED)));*/
   
    /*int tileNum = (int)(Math.random()*16);
    String address = "img/p" + tileNum + ".bmp";
    ImageIcon image = new ImageIcon(address);
    System.out.println(address);
    JLabel button = new JLabel(image);
   
    //button.setIcon(image);
   
    return button;*/
    //panel.add(new JButton("X"));
    Image image;
    try {
      image = ImageIO.read(new File (String.format("images/p%d.bmp", t.getTileNumber())));
      JButton btn = new JButton(new ImageIcon(image));
      btn.setVisible(true);
      btn.setContentAreaFilled(false);
      panel.add(btn);
    } catch (MalformedURLException e) {
      //  Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      //  Auto-generated catch block
      e.printStackTrace();
    }
   
    return panel;
  }
 
  /** Returns an ImageIcon, or null if the path was invalid. */
  protected ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = this.getClass().getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.out.println("Couldn't find file: " + path);
      return null;
    }
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
      public void run()
      {
        new PMTestFrame03();
      }
    });
  }

}
TOP

Related Classes of com.test.pm.PMTestFrame03

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.