Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couple of Problems

 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for getting back to me and answering questions/giving advice - very much appreciated
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm now i'm lost
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven Gibbs:
Hmm now i'm lost


So which part don't you understand ?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement the Comparable interface
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know much about Graphics objects I'm afraid. Did you try what Rob suggested in his post ?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm that draws my horizontal lines but on top of each other and at the very top of the panel
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm no Joanne that didn't cure it either. My computer is about to go out the window I think haha
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much everyone got it working
 
Steven Gibbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic