• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Writing an Etch A Sketch Program

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again!

So, we've finally hit writing small applets/little graphics and this week we're given the following prompt:

TA will help you copy/paste download the EtchASketch starter program from our course website to your PC. You are to put several JButtons on the JFrame's content pane such that he buttons are of varying colors. The effect of any button being clicked must be to cuase all subsequent mouse drawing to draw its line in that color.
Hints:

declare a global/member variable at the top to represent the current drawing Color
declare an ARRAY of JButtons and an array of Color codes to assign to them.
get rid of the echoing of the mouse coordinate before handing in.



Here is a link to the starter file: Etch_A_Sketch.java

Now, here's my code:


However, nothing happens, and frankly I'm stumped. I feel like I'm doing this completely wrong. I added a ButtonListener/ActionListener and registered it with the corresponding buttons. I even added the following line:
in the mouseDragged method, but nothing happens...

Any advice or nudge in the right direction would be much appreciated. I feel like I'm not doing this assignment how I should be.

Thank you for any help,
Joe
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Nothing happens" doesn't seem accurate to me. I have no problems getting lines to be drawn along the path of the mouse. (You're are keeping the mouse button pressed while dragging, right? Just checking :-) ) The only thing that doesn't work right is the color selection, but a small change in the mouseDragged method will fix that.

Oh, and after releasing the mouse button you need to do a bit of housekeeping - otherwise the next line segment starts at the end of the previous segment instead of at the position where the mouse button was pressed again.
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,
Should of been more specific. I meant nothing happens in the color changer. Do you have any tips you can give me?

Thanks,
Joe
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have all the code you need in the mouseDragged method - but you need to reorder it just a little bit
 
Bartender
Posts: 5530
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Joseph,

every time you create a new Graphics environment (see the mouseDraaged method)), you get one in its
complete default state, Therefore, in your mouseDragged method, you always
draw first with the default color, black. It is only after you have drawn that you change
the drawing color, Therefore you do not see any impacr whatsoever.

Two other remarks:

the assignment indicates that your buttons should have corresponding colors. Currently,
you are only supplying names on them.

The direct drwing which you do in the mouseDragged method works, but is considered
dubious. Normally, you would do all the painting in a paintComponent() method (you have
systematically outcommented the repainf() command).
Well, this technique works, but it has some drawbacks. First, it lets you draw over your buttons,
although the effect is actually pleasing. If you then click on an overdrawn button, you will see
your lines erased at that button.
Which brings me to the second remark: since you do not paint anything in your paintComponent
method, you should expect some problems when your frame changes. Now, covering your
frame by some other frame and then uncovering, you won't notice anything. That's because
of the double buffering that Java uses. However, resize your window and see what happens.

But then, storing all the drawing information such that you can restore everything in the paintComponent,
may be covered in a lesson to come.

Greetz,
Piet
 
Joseph Ibrahim
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Joseph,

every time you create a new Graphics environment (see the mouseDraaged method)), you get one in its
complete default state, Therefore, in your mouseDragged method, you always
draw first with the default color, black. It is only after you have drawn that you change
the drawing color, Therefore you do not see any impacr whatsoever.

Two other remarks:

the assignment indicates that your buttons should have corresponding colors. Currently,
you are only supplying names on them.

The direct drwing which you do in the mouseDragged method works, but is considered
dubious. Normally, you would do all the painting in a paintComponent() method (you have
systematically outcommented the repainf() command).
Well, this technique works, but it has some drawbacks. First, it lets you draw over your buttons,
although the effect is actually pleasing. If you then click on an overdrawn button, you will see
your lines erased at that button.
Which brings me to the second remark: since you do not paint anything in your paintComponent
method, you should expect some problems when your frame changes. Now, covering your
frame by some other frame and then uncovering, you won't notice anything. That's because
of the double buffering that Java uses. However, resize your window and see what happens.

But then, storing all the drawing information such that you can restore everything in the paintComponent,
may be covered in a lesson to come.

Greetz,
Piet



Thank you for your response.
The repaint() method was commented out in the starter file he gave us, and the current draw method was already done. To be honest, we haven't really learned much in regards to drawing/gui. We're kinda rushing this in at the very end..
Now, for the buttons, I plan on sticking them on their own little pane and pretting it up, I just wanted to make sure they work first.
Any advice in how the paint method would look or work?
Thanks,
Joe
 
Piet Souris
Bartender
Posts: 5530
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
putting the buttons in a separate panel is certainly a good idea. You would need a separate
panel for your line drawings, then.

There are many ways in which you can do all the drawing in the paintComponent method.
One is to store all the points and the colors in some way, and do all the drawings later on.
Another way would be to draw to a BufferedImage instead of directly to a panel,
and paint that bufferedimage to your panel.

But I suggest to leave that to when you get the lessons that will cover this subject.

For now, I would advice to follow Ulfs remarks: get the colors working, and make sure
that a mousePressed event starts a new curve, instead of connecting the new point to
the former point.

Greetings,
Piet
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic