aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Applet that lets you draw shapes, custom component and action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Applet that lets you draw shapes, custom component and action" Watch "Applet that lets you draw shapes, custom component and action" New topic
Author

Applet that lets you draw shapes, custom component and action

Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
This is a pretty big lab and I am going along fine with the code so far, but I am not sure if I'm creating these classes the best way.
The lab says to create a panel that when you click in it the click creates a shape and a panel of buttons let you choose the shape whether it's filled or not and the color of the shape.
My big confusion is with event listeners I am not exactly sure how to use them the lab instructions are

Your custom component must also implement a custom action that is triggered whenever the shape type, color, or filled/unfilled mode is altered by the user. This action should generate a custom event that contains a Shape object of the selected type and with the currently selected characteristics, and with a width of 30.



For the events I wrote this code so far to outline what I want to do, but I am running into two problems, how do I get access to my shape classes(ie public class a extends b...implements b?)? And how do I call it up in the code that I have below? . I have a shape class which is the parent class to each shape(Square, Circle, etc). and a class for the button panel and this code I posted is in it's own class




Thanks in advance for any help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
I know you see that sort of thing in some books, but it is rubbish. Don’t try those big if-elses. I suggest you create a Paintable interface with one method telling the objects to paint themselves. All your shapes can implement that interface. They might inherit from one another; that is for you to work out.
Alternatively, you might get your shapes to implement the Icon interface.
Find some old threads, eg this one, which discuss similar problems. Read them. Get individual listeners for each button. It would be rather like my ColourListener class, which you will find more details of here and here.

I am moving this thread because we usually discuss GUI-related questions elsewhere.
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
My professor doesn't use a book so I just have his examples to go by. His notes and the lecture aren't really helping me much and everything on the internet is getting more confusing than it usually does.
Thanks for the links I'll check those out and see if that can help me make sense of this.

Sorry about posting in the wrong forum, I didn't realize there was a separate one for GUI, and even if I had the beginner area is usually my fail-safe since I am pretty java illiterate
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
Well I've been working on this for two weeks now and I've been stuck on these action event listeners for about a week now.
I've gone through a lot of examples and I still must be missing something because I don't think this should be so hard since this is the first assignment we've had using the custome components and it's only the second GUI assignment.

Anyway can someone maybe explain in simple terms how I should go about solving this problem.

I have three types of buttons. One set chooses if the shape is filled or unfilled. Another set chooses the shape. And I have a button that brings up a color chooser.
There is a drawing panel and the idea is that I set the shape, color, and if it's filled or not and then I can click anywhere in the drawing panel and it will create my shapes

This is what I have for my event(right now I'm just focusing on getting a square to work and to change color)



and then I have this in with my buttons



The big questions I have right now are do I need both these codes? All the examples I see use just the second, but I can't find any examples where there are this many different things going on. It seems the examples are either too simple for this or they are way to complicated for me to understand enough to apply them to what I need.

This is my EventListener...Is it wrong for me to try doing all the shapes in one set of these? Should I have a square listener, a square event, a circle listener, a circle event...etc? My idea is that I should be able to group them together by what the button is affecting(the shape, the fill, and the color)




Also, as much as I would like to do this some other way this is what's required in the assignment, so I can't really deviate
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
I would advise you to get your hands on a book. The best book might be Horstmann and Cornell, which you will probably find here; I don’t think they have got the Java7 edition out yet. But you don’t need Java7 features. Find out about listeners.

What I can tell you is that your middle quote with the poorly indented code looks a mess. Even if you sort out the indentation, you end up with a load of else ifs which make the method hard to maintain and also violate the design principle that a method does one thing (acknowledgement: Fred Rosenberger). Your code looks as if you had been desperately writing something, anything, rather than taking the time to think about what to write and to design it.
Consider how you intend to draw one shape. Remember, you need to specify a size, colour and location as well as the type of shape. Start by drawing a square in a fixed location, and a fixed size. Once you have got that working, consider how to set its location. You obviously have some idea how to do that already. Then enhance it for colour and size.
You would do well to pass some sort of Square via whichever Listener you use. There are several ways you can do it. Then you can draw and display it.

Search for my posts about how to write Listeners. They aren’t very recent, because I tend to refer to old posts rather than writing new ones. The instructions there are general, and not specifically related to your current problem.
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
Thank you for your reply again. I think this one is just too advanced for me with the information I have from the class and the things I'm reading I'm not really understanding.
I think I have a general understanding of how it's supposed to work, but the coding part just isn't working out. I can make it work if I want to choose the color of the color button or if I want to display something.
Sorry about the messy code. The ifs are the only way he taught us to do it and I've found that the examples from class are the only ones I'm really grasping, the other ways I've read about are confusing and beyond the scope of what I've learned.
Anyway, thanks again, I'll keep reading and maybe I'll be able to find something that helps it click for me.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Anyway can someone maybe explain in simple terms how I should go about solving this problem.

here's a (very) simple demo of what you'd expect to see (if I'm reading the specs right)
note: this would be no good for you to submit - very little is in the specs

compile/run then select/unselect any or all of the buttons

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
I don’t like all those else-ifs in the paintComponent() method.
Christina Bremmerman
Ranch Hand

Joined: Feb 02, 2012
Posts: 33
Thanks Michael.
I had to turn it in yesterday and never really got it working.
I was wanting to do it like you did, but one requirement was having a hierarchy of classes for the shapes and creating my own draw method so I was having to create objects and that's what was tripping me up I think.
Anyway my brain is mush now...this was not a fun thing to be doing during dead week and finals...buuut if I don't pull a B in this class I'll see you guys later probably when I retake it!

Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet that lets you draw shapes, custom component and action
 
Similar Threads
Problem linking button to mouse event - Please help!
Problem with simple drawing tool
Calling methods using dynamic binding
painting custom components without extending jcomponent
Inheritance problem