View Single Post
  #3 (permalink)  
Old 03-06-2008, 05:13 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
Board board_for_game = new Board(300, 300, car_set_1, exit_1); RushPanel panel_for_game = new RushPanel(board_for_game); JFrame f = new JFrame("The Rush Hour Game"); // "panel_for_game" is a reference to this new RushHour // instance you created with the new operator. // Since RushHour extends JPanel then this instance // ("panel_for_game") _is_ a JPanel and can be added to // your frames contentPane as a component. There is no // need to make another one. This instance of RushHour // comes with a paint method override and is ready to go. //RushPanel rushPanel = new RushPanel(); // create paint panel f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(panel_for_game);
Code:
import java.awt.*; import javax.swing.*; public class RushPanelRx extends JPanel { Board myBoard; RushPanelRx(Board board) { myBoard = board; } // Overriding this method is easier for custom drawing. protected void paintComponent(Graphics g) { super.paintComponent(g); double yInc = (double)(myBoard.getHeight()-1)/Board.NOOFROWS; for (int row = 0; row <= Board.NOOFROWS; row ++) { int y = (int)(row * yInc); g.drawLine(0, y, myBoard.getWidth(), y); } double xInc = (double)(myBoard.getWidth()-1)/Board.NOOFCOLS; for (int col = 0; col <= Board.NOOFROWS; col++) { int x = (int)(col * xInc); g.drawLine(x, 0, x, myBoard.getHeight()); } } public static void main(String[] args) { Board board = new Board(300, 300); RushPanelRx rushPanel = new RushPanelRx(board); JFrame f = new JFrame("The Rush Hour Game"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(rushPanel); f.setSize(350, 350); f.setVisible(true); } } class Board { public static final int NOOFROWS = 6; public static final int NOOFCOLS = 6; int width; int height; Board(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } }
Reply With Quote