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
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.
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
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
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.
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.
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
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!