aspose file tools*
The moose likes Java in General and the fly likes Couple of Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Couple of Problems" Watch "Couple of Problems" New topic
Author

Couple of Problems

Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Having a couple of problems in my code below. The first one is this line in RushFrame class is highlighted as wrong:



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:



RushPanel Class:



RushFrame Class:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Change the constructor to accept a Board, and set that board as well:

This should also solve your paint problem. Here you're accessing myBoard - but myBoard has never been given a value so it is still null.

Some more advices:
- override paintComponent instead of paint, as paint does a bit more for you.
- don't forget to call super.paintComponent(g), or in your current case super.paint(g) before doing any custom painting
- cache the result of myBoard.getHeight()/Board.NOOFROWS before your loop. Although it doesn't really improve your speed, it's nonsense to calculate the exact same value thrice.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Thank you very much for getting back to me and answering questions/giving advice - very much appreciated
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
hmm I seem to get this error when I run:

Exception in thread "main" java.lang.ClassCastException: rush.Car cannot be cast to java.lang.Comparable
at java.util.TreeMap.put(TreeMap.java:542)
at java.util.TreeSet.add(TreeSet.java:238)
at rush.Board.<init>(Board.java:39)
at rush.RushFrame.main(RushFrame.java:25)
Java Result: 1

Am I missing something?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Your Car class needs to implement the Comparable interface.
A TreeSet is a sorted set and in order to sort it's elements, all objects added to a TreeSet must implement the Comparable interface. See the TreeSet javadoc for more details.


Joanne
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Hmm now i'm lost
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by Steven Gibbs:
Hmm now i'm lost


So which part don't you understand ?
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
How to implement the Comparable interface
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Have you looked at the javadoc ? The only thing you need to decide is how to one Car object is considered less than, greater than or equal to another Car object i.e. why should one Car come before or after another Car in a sorted list.

If there is no obvious way to sort Cars then perhaps you shouldn't be using a TreeSet. Would an unsorted Set such as HashSet be better for you or maybe an ArrayList ?
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Thank you soo much Joanne - changed it to a HashSet and compiles and runs fine now

Only problem now is that its not drawing my 7 horizontal lines to make the 6x6 grid.

Any ideas what i'm doing wrong in my for loop of my paint method of my rushpanel
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Don't know much about Graphics objects I'm afraid. Did you try what Rob suggested in his post ?
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
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:



Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Steven Gibbs:
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.


Right and wrong.

What you got right - It does indeed have to do with your for loops.

What you got wrong - There are several vertical lines and several horizontal lines. Only they are drawn on top of one another.

So, have you been able to figure out what is wrong?


Originally posted by Steven Gibbs:



Are the arguments to the drawLine method changing in very iteration? Are you using your for loop indices x and y anywhere within the for loop? Do your for loops only serve to repeat the code within it a certain number of times, or do the loop indices serve to do something more?


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
No i'm not sure what I have done wrong in my code. I can now see why it would be drawing my lines on top of each other but no idea how to sort this.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Steven Gibbs:
No i'm not sure what I have done wrong in my code. I can now see why it would be drawing my lines on top of each other but no idea how to sort this.


The documentation of drawLine explains that it takes a (x,y) for the start point and a (x,y) for the end point. You pass the same start point and end point pair for all the iterations in the for loops for horizontal and vertical lines.

The following code draws the horizontal lines:


Am sure you will be able to come up with the correct code to draw the vertical lines.

- Anand
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Hmm that draws my horizontal lines but on top of each other and at the very top of the panel
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Try

Edit
Whoops. Ignore this. Should have read Anand's post a little more closely. I assumed the third part of the for loop was just y++ rather than what it actually is.
[ March 06, 2008: Message edited by: Joanne Neal ]
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
hmm no Joanne that didn't cure it either. My computer is about to go out the window I think haha
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20


Editted: you must use boardHeight and boardWidth as loop boundaries since you're not iterating over the columns but their locations.
[ March 06, 2008: Message edited by: Rob Prime ]
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
Thank you so much everyone got it working
Steven Gibbs
Greenhorn

Joined: Apr 24, 2006
Posts: 29
I'm now trying to get my car images drawn onto the grid but not sure where to start. The classes concerned are below. My cars are in a Hashset so i'm guessing I need an iterator or something to go through each car in the Hashset till they are all added - correct?

Any pointers/advice would be greatly appreciated.

Car Class:



Board Class:



RushPanel Class:



RushFrame Class:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couple of Problems