User Defined Classes [Java]

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

User Defined Classes [Java]

Post by Wesley »

I'm finally beginning to learn some programming! I am in an introductory course and have been understanding everything pretty well, until we reached user-defined classes.
My assignment is this:

Code: Select all

Design and implement an application that draws a traffic light and uses a push button to change the state of the light. Derive the drawing surface from the JPanel class and use another panel to organize the drawing surface and the button.
We have not learned error handling yet or if statements, so I'm supposed to use an ActionListener that removes the old light and adds the new light.
I'm also supposed to have 3 classes to do this: TrafficLightMain (the driver), TrafficLightPanel (what organizes the information to go into the panel), and TrafficLight (what creates the light itself). Where both TrafficLightPanel and TrafficLight extend JPanel. And only TrafficLight needs a paintComponent method, at the end of which I'll need to invoke repaint.

I'm not really sure where to even start. The book doesn't explain the whole thing very well. Does anyone have any suggestions on where to start for each of the classes I'm supposed to create? I think all I need for TrafficLightMain is this:
EDIT: I'm sorry, but I have to wipe out my code.
Last edited by Wesley on Fri 09 Oct, 2009 1:32 pm, edited 1 time in total.
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

Where to start:
Just start doing it, make those classes and have them extend whatever they have to extend, then you will see.
Beware of the "events through interfaces" hack.
Error handling is for C and assembly, in Java you have exceptions, and unfortunately you must catch every exception that could possibly arise or else it will refuse to compile.
Makes me wonder why they don't just teach us C# nowadays.
I asked why not C++, apparently it's too hard - the reason they didn't do C# was "because we still have the old material" which isn't a good enough reason to pollute people with Java IMO.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

Oh man, I wish it was as easy as you said. Like I mentioned, we aren't very far into the course yet. We have a GUI section at the end of each chapter in our book and we're only on Chapter 4. Basically, all I can come up with is this for my TrafficLight

and TrafficLightPanel classes:

My professor gave this hint for the listeners, but I don't know how to use it in the program or where to put it.
Last edited by Wesley on Fri 09 Oct, 2009 1:33 pm, edited 1 time in total.
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

That would be the event hack. I think he's suggesting you make the TrafficLight class implement a listener so it can respond to "events" so it can change its state. Or that the panel implements it and then tells the traffic light to change its state. That's pretty much equivalent.

< history >
At the University of Utrecht the mandatory Java course is so dead simple that I never learned for it (and I mean at all) and still passed with a grade roughly translatable to an A. Perhaps because it was also for the "IT management" guys.. I had never done Java before.. the event hack is what bothered me most. Lack of unsigned ints and the widespread use of doubles instead of floats annoyed me as well.
They took 2 hours to explain 'if' and 'else' - I left and came back to take the exam 2 months later.
< /history >

So I don't know what to hope for you, an easy course for which you will get an A without learning or a slightly harder one from which you will learn something useful? what do you like? :)
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

Well, I always like the hard class where I can learn something useful (although, you can learn many useful things in easy courses). I am getting closer to tackling the assignment, but I am still running into some bumpy roads on how to draw the lights whenever the button is pressed.
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: User Defined Classes [Java]

Post by benryves »

King Harold wrote:I asked why not C++, apparently it's too hard - the reason they didn't do C# was "because we still have the old material" which isn't a good enough reason to pollute people with Java IMO.
C++ is a fairly ugly language with all manner of pitfalls and gotchas; you'd spend more time learning C++ than learning how to program. Languages such as Java or C# still have pitfalls and gotchas but on a less destructive scale and are generally easier to get started with, allowing you to focus more on learning to program rather than learning a particular language. (Hence the recommendation for beginners to learn Python or C#).
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

Yes, so they maybe they have a good reason not to teach C++ (though it would get rid of the fake programmers). But I was making a case for C#, which is undoubtedly superior to Java
Not that it really helps if I do so, the courses won't suddenly change because of posts made on this forum..

As to the drawing of the lights.. you shouldn't draw them only when the state changes (but this is easy as well), but rather at any time they should be drawn (as indicated by the draw "event"(hack!) which you should be getting somewhere)
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

I got it! I can't believe it took me so long. Suddenly I understood what was going on and I finished and optimized the program :rofl: . Here is my code for TrafficLight, TrafficLightPanel, and TrafficLightMain:
TrafficLight:

Feel free to let me know if anything could be changed or edited to optimize the code. Thanks!
Last edited by Wesley on Fri 09 Oct, 2009 1:35 pm, edited 1 time in total.
ImageImage
ImageImage
Image
cjgone
Regular Member
Posts: 83
Joined: Tue 18 Apr, 2006 5:33 am
Location: Washington->UC Berkeley '15

Re: User Defined Classes [Java]

Post by cjgone »

Man, object-oriented languages scare me. :'(


I never knew clicking a button to change afake light could be so freakin' difficult. Can ya quickly summzarize the code?

I would have expected:
Create a new class for the traffic light. Draw it using some draw function. If some button is clicked to make light red, then you call some function to change the color (red, yellow, green).

But I look at teh code and I don't see anything that means anything, like wtf is redlistener.

And how does the drawing work? Only need to change one variable and it auto updates or something? =X Is there like some function like a main statement that calls the initializations and stuff? And why is like everything in a class.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

cjgone wrote:I never knew clicking a button to change afake light could be so freakin' difficult. Can ya quickly summzarize the code?
Well, the TrafficLightMain class is simply the driver for the program, to make the whole thing appear, basically. It doesn't do too much.
The TrafficLightPanel class organizes the TrafficLight class and a JButton component.
The TrafficLight class simply creates methods to be used to create the traffic light itself (with the paintComponent method).
cjgone wrote:I would have expected:
Create a new class for the traffic light. Draw it using some draw function. If some button is clicked to make light red, then you call some function to change the color (red, yellow, green).
I could very well have done that. I also could have made a program that did the exact same thing, but with only one class. The objective of my assignment; however, is to create classes that have their own methods that can be called by other classes.
As far as the "if" idea goes, my class hasn't gone over loops yet, so I can't use that route to complete the program, so I used the "hint" my professor gave, which was to create a new ActionListener for each new light and delete the old ActionListener; therefore, creating my own loop, in a way.
cjgone wrote:But I look at teh code and I don't see anything that means anything, like wtf is redlistener.
RedListener, like the other listeners, is a tool (instance of an ActionListener) used in Java to detect user interaction. I created my own listeners so I could declare a time for each light to come on and when, then to destroy the previous light (to make it turn off).
cjgone wrote:And how does the drawing work? Only need to change one variable and it auto updates or something? =X Is there like some function like a main statement that calls the initializations and stuff? And why is like everything in a class.
I'm not sure I understand your questions correctly. However, classes are what are used in object-oriented programming. They are basically what create the methods and functions that can be used when creating programs (or other classes).

I also added a neat little gimmick to give the light a more "real" touch. I made so when the light has a specific light "on" the other lights look like they're "off" by drawing darker circles before drawing the light that is "on". Take a look at the .gif file I attached and notice how my green light is brighter than the rest. Once the "Change" button is pressed, the green light turns "off" (it simply is destroyed and the darker colored circle is drawn), and the yellow light turns "on" and will be brighter than the rest. I tried to upload my .jar file so you could understand exactly what my program does, but I guess the forum doesn't accept that type of file.
Attachments
TrafficLight.gif
TrafficLight.gif (4.77 KiB) Viewed 20976 times
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

cjgone wrote:I never knew clicking a button to change afake light could be so freakin' difficult.
It isn't, this is just the difficult way of doing it. Having the main canvas remember the state and handle all the drawing and "events"(omg hack!) works just as well.

@Wesley: good, but if you upload the applet to some webhost and make a little page with random <applet> tags we could try it :)
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

King Harold wrote:@Wesley: good, but if you upload the applet to some webhost and make a little page with random <applet> tags we could try it :)
Hm... I could upload it on my brother's website, but how do you make it like an applet? I haven't even touched on applets yet. But that would be awesome if you could tell me how :D .
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

Well what is it now?
Usually it needs next to no modifications to work as applet (it must not try to create a window or handle the window "events") and then you can just use the class files in an <applet> tag
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post by Wesley »

King Harold wrote:Well what is it now?
Usually it needs next to no modifications to work as applet (it must not try to create a window or handle the window "events") and then you can just use the class files in an <applet> tag
lol. How can you expect me to know what to do when I have never learned about applets? I might be able to figure it out. If not, I'll just upload the .jar file on my brother's website.
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

Well this is teh intarwebs, right? :)
google..

Anyways, just upload the jar then?
Post Reply