Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

assigning code to my buttons  RSS feed

 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, i've just started java recently and it feels like i've been thrown into quite a deep end. I'm making buttons to draw a line, oval, rectangle, arc and showing an image. So far all i've managed is make the buttons and put in the code for drawing a line and basically it's just making all the buttons draw lines. I have no idea what to do next and i just want to implement the code into the buttons, i think i have most of the code for each feature. I've done some research and i found a guide that's given me most of the code but im not sure where i'm going next.
I apologise if my code is abit messy and this is my first time on a programming forum.
Thank you
Here is the project


Here is what it looks like


and here is the code
 
Andrea Binello
Ranch Hand
Posts: 47
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one note: what is JCanvas? What is JEventQueue? (ok, I can imagine ...) And why should you loop on a waitEvent? Are these things part of BlueJ? (I don't known and I never saw these classes).

This is not the standard/common way to develop Swing applications!
 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh i'm sorry. Yes i was told to use BlueJ so i had a look into it. The JEventQueue is not part of the standard Swing library but i had to put it in so it listens to the events from the components. And the JCanvas was needed i believe, so that i could draw shapes and images onto the area.
Thank you very much for your reply
 
Randall Twede
Ranch Hand
Posts: 4683
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no JCanvas it should be JPanel
Bluejay is a very simple easy to use IDE. i had to use it in college. i really liked it.
 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the guide i looked at there was the code already for the JCanvas class. And inputting that class allowed me to draw in the windows without designing new classes. Thank you for your reply i will look into JPanel
 
Randall Twede
Ranch Hand
Posts: 4683
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JCanvas must be a custom class the author wrote and you may or may not have that source code.
you can use JPanel or Canvas
JPanel is better. Canvas is from Java1
 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that's it. Yes i have the source code for it. And it's working perfectly fine i just want to know how i implement my next bits of code into my buttons.
which are
for my oval

for my rectangle

for my Arc

Thank you once again for the replies
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Alvin,

normally you would adjust the state of your program in such a way, that a call to repaint the canvas/panel/jpanel
would draw the relevant figures. You would set this state in the ActionListeners that you added to the
buttons.

But you are using some non-standard Java here; the other repliers already mentioned this. Especially strange
is this event-loop, which you normally do not have to program. So, what you actually must do, I don't know.
What documentation do you have on this non standard Java?

Greetings,
Piet
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Alvin Tsang wrote: . . . Yes i was told to use BlueJ so i had a look into it. . . .
And have you started cursing whoever told you to use BlueJ yet?

What is JEventQueue? Does that start a thread? If so, it is probably broken code; Swing is not thread‑safe, so you must only use the Event Dispatch Thread. There is an example of what goes wrong otherwise in Horstmann and Cornell; from this post it appears to be page 909. If your Event Queue runs in the main thread, that would be wrong, too.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:JCanvas must be a custom class the author wrote and you may or may not have that source code.
you can use JPanel or Canvas
JPanel is better. Canvas is from Java1
If I remember correctly, the book which advocates BlueJ (Barnes and Kölling) has a download with a class called JCanvas in. It appears to be a drawing canvas.
 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I really appreciate the replies.
And have you started cursing whoever told you to use BlueJ yet?

Yes it's really stressing me out, i think i start to understand it and then i'm back at the start again. I've found documentation of the Java here
http://akira.ruc.dk/~madsr/swing/notes.pdf
we are supposed to follow this guide to create it.
I maybe wrong but i think the event-loop is for when i press the other buttons i have created. I am sorry i am new to this.
Thank you once again for all your help
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That appears to be a non‑standard approach to graphics. Event queue does appear to be a store for events, and the app has to poll it regularly to see whether there are any events needing handling. dIn the short time I looked at the handbook, at least it did not appear to interfere with the threading. I suspect, however, it will interfere with your learning about events.

Is there a particular reason why you are using this unusual approach?
 
Alvin Tsang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my course. This was the manual given to do all the tasks. I am understanding parts of it slowly but for the actual tasks i am not understanding alot
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should post a few sentences from the manual, or that link and a page number, next time you encounter something you don't understand in it.
 
Nick Wa
Greenhorn
Posts: 5
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since BlueJ seems to contain classes that are not common to most Java programming or stored in other IDEs, I'll try to give a rough outline of how you can approach the problem of drawing shapes other than lines. I won't go into specific detail with code as it might not be applicable to your IDE.

JCanvas seems to be able to draw lines (and possibly other shapes), so I'm guessing it's like Graphics (or Graphics2D), possibly combined with a Canvas class. When you create a program, including a GUI, there are always events being fired. You have to listen to all the events you're interested in and when a particular one is caught, you should have code that does something. Right now, EventObject events listens for any events fired when the JButtons for drawing a line or oval are fired, or when the user interacts with the canvas (i.e mouse click). What you have to do next is catch the event when the "Draw Oval" JButton is pressed, then use JCanvas to draw an oval.

Since you call .isMouseEvent(EventObject object), is there any method to invoke that detects a button press or an ActionEvent? Sorry for this question but normally, the way you write event-listeners is night-and-day compared to these event-listeners.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!