Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2008, 05:48 PM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Couple of Problems
Having a couple of problems in my code below. The first one is this line in RushFrame class is highlighted as wrong:

Code:
RushPanel panel_for_game =new RushPanel(board_for_game);
I know this is wrong because in my RushPanel class I just have RushPanel() but i'm not sure what I need to put in my brackets - any ideas - it could just be me missing something.

The second error is in my RushPanel in my paint method at the bottom. I am trying to draw a 6x6 grid using lines.

The for loop which is not commented out seems to be causing problems. When you look at the for loop below that one which is currently commented out - it will happily draw my vertical lines but when I change that to same format as for loop above it doesn't draw anymore.

Can anyone give me some advice on how to cure this?

I have only posted my Board, RushPanel, and RushFrame classes. I also have a Car and Exit class but I think they are ok.

Board Class:

Code:
package rush; import java.util.TreeSet; import rush.Car; public class Board { public static final int NOOFROWS = 6; public static final int NOOFCOLS = 6; public static final boolean EMPTY = false; // The cell is empty. public static final boolean OCCUPIED = true; // The cell is occupied private int width; private int height; private TreeSet<Car> car; Exit exit; /** Creates a new instance of Board */ public Board(int width, int height, Car[] cars, Exit exit) { this.width = width; this.height = height; this.exit = exit; car = new TreeSet<Car>(); for(int i = 0; i < cars.length; i++) { car.add(cars[i]); } } public void setWidth(int width) { this.width = width; } public int getWidth() { return width; } public void setHeight(int height) { this.height = height; } public int getHeight() { return height; } }//end Board class
RushPanel Class:

Code:
package rush; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JPanel; public class RushPanel extends JPanel { public Board myBoard; Car[] car ; private boolean isDraggable = false; /** Creates a new instance of RushPanel */ public RushPanel() { addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e){ // ******Code Here****** Point p = e.getPoint(); for(int i = 0; i < car.length; i++) { if(car[i].contains(p)) { e.getX(); e.getY(); isDraggable = true; } } } @Override public void mouseReleased(MouseEvent e){ // ******Code Here****** isDraggable = false; } }); addMouseMotionListener(new MouseMotionAdapter(){ @Override public void mouseDragged(MouseEvent e){ // ******Code Here****** if(isDraggable) { Point p = e.getPoint(); } } }); } public void paint(Graphics g) { for (int y = 0; y <= Board.NOOFROWS; y = y + myBoard.getHeight()/Board.NOOFROWS) { g.drawLine(0, myBoard.getHeight()/Board.NOOFROWS, myBoard.getWidth(), myBoard.getHeight()/Board.NOOFROWS); } /*for (int x = 0; x <= 300; x = x + 50) { g.drawLine(x, 0, x, 300); }*/ } }
RushFrame Class:

Code:
package rush; import javax.swing.JFrame; public class RushFrame { public static void main(String[] args) { Car[] car_set_1 ={ new Car(1, 1, 2), new Car(4, 4, 1), }; Exit exit_1=new Exit(0, 3, 3); 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"); //RushPanel rushPanel = new RushPanel(); // create paint panel f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(panel_for_game); //f.add(new RushPanel()); f.setSize(350,350); f.setVisible(true); f.setResizable(false); } }

Last edited by joz_12345 : 03-05-2008 at 12:06 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-06-2008, 01:13 AM
Member
 
Join Date: Dec 2007
Posts: 17
joz_12345 is on a distinguished road
Ok I now have lines appearing on my panel but only one vertical one and one horizontal line. How do I get more lines to appear so it creates a 6x6 grid. I'm guessing its to do with my for loops in the paint method in my RushPanel.

What its doing so far:



Code:
package rush; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JPanel; public class RushPanel extends JPanel { public Board myBoard; Car[] car ; private boolean isDraggable = false; /** Creates a new instance of RushPanel */ public RushPanel(Board board) { myBoard = board; addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e){ // ******Code Here****** Point p = e.getPoint(); for(int i = 0; i < car.length; i++) { if(car[i].contains(p)) { e.getX(); e.getY(); isDraggable = true; } } } @Override public void mouseReleased(MouseEvent e){ // ******Code Here****** isDraggable = false; } }); addMouseMotionListener(new MouseMotionAdapter(){ @Override public void mouseDragged(MouseEvent e){ // ******Code Here****** if(isDraggable) { Point p = e.getPoint(); } } }); } @Override public void paint(Graphics g) { super.paint(g); for (int y = 0; y <= Board.NOOFROWS; y = y + myBoard.getHeight()/Board.NOOFROWS) { g.drawLine(0, myBoard.getHeight()/Board.NOOFROWS, myBoard.getWidth(), myBoard.getHeight()/Board.NOOFROWS); } for (int x = 0; x <= Board.NOOFROWS; x = x + myBoard.getWidth()/Board.NOOFCOLS) { g.drawLine(myBoard.getWidth()/Board.NOOFCOLS, 0, myBoard.getWidth()/Board.NOOFCOLS, myBoard.getHeight()); } } }

Last edited by joz_12345 : 03-06-2008 at 01:15 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-06-2008, 05:13 PM
Senior Member
 
Join Date: Jul 2007
Posts: 933
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java assignment - couple methods don't know how to figure out Snowboardmylife New To Java 1 04-16-2008 11:52 AM
Problems in JSP : Need help raj4u JavaServer Pages (JSP) and JSTL 1 02-07-2008 11:06 AM
GUI problems. saytri New To Java 1 12-16-2007 11:27 PM
a few problems gary AWT / Swing 0 07-11-2007 05:57 PM
problems with JPA Ed New To Java 2 07-04-2007 06:34 AM


All times are GMT +3. The time now is 02:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org