Results 1 to 7 of 7
Thread: Drawing Applet
- 11-19-2008, 06:51 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
Drawing Applet
Hi, I wrote a simple applet that lets the user draw. I want to be able to change the color of the pen (g.filloval)'s using radio buttons. Ive looked up how to use radio buttons but nothing I found really helped with my situation. Heres what I have so far:
Java Code:import java.applet.Applet; import java.awt.Event; import java.awt.Graphics; public class Scribble extends Applet { int xpoint; int ypoint; public boolean mouseDown(Event e, int x, int y) { xpoint = x; ypoint = y; return true; } public boolean mouseDrag(Event e, int x, int y) { Graphics g = getGraphics(); g.fillOval(xpoint, ypoint, 5, 5); xpoint = x; ypoint = y; return false; } }
-
1) I recommend that you use Swing and JApplets unless you have a very compelling reason to do otherwise.
2) You should then draw in a JPanel and display the drawing JPanel in your JApplet.
3) The painting goes on in the JPanel's paintComponent method.
4) There are many ways to have JRadioButtons change the color of the pen, but how you do it will depend a bit on your program structure. I favor having the drawing done in a JPanel that's in its own class with a public setPenColor(Color c) method. Then this method can be called on the drawPane object from within an ActionListener attached to your radio buttons.
- 11-19-2008, 07:01 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
I dont like swing because it requires like 2X more code to accomplish the same thing. Also Im not asking how to set it up, im more concerned with how to use radio buttons.
-
Sorry, but that's kind of like saying "I don't like a car because it's too complicated. I'd like to ride my bike from here to Peoria. How do I do this?"
Seriously, AWT will hold you back. Learn Swing.
Regardless my concepts are still true. The radio buttons have ActionListeners and in the listeners you change the Color variable either directly or indirectly.
- 11-19-2008, 07:48 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
Okay, I have my radio buttons set up correctly, what I cant figure out is how to make it so it changes the pen color depending on which one is selected
Java Code:import java.applet.*; import java.awt.*; import java.awt.Graphics; public class Scribble extends Applet { int xpoint; int ypoint; Checkbox red, blue, green, yellow; CheckboxGroup cbg; public void init(){ cbg = new CheckboxGroup(); red = new Checkbox("Red", cbg, true); blue = new Checkbox("Blue", cbg, false); green = new Checkbox("Green", cbg, false); yellow = new Checkbox("Yellow", cbg, false); add(red); add(blue); add(green); add(yellow); } public boolean mouseDown(Event e, int x, int y) { xpoint = x; ypoint = y; return true; } public boolean mouseDrag(Event e, int x, int y) { Graphics g = getGraphics(); g.fillOval(xpoint, ypoint, 5, 5); xpoint = x; ypoint = y; return false; } }
- 11-19-2008, 08:37 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
I figured it out, I just put a couple simple if else statements in there.
-
Similar Threads
-
Using Piccolo for Drawing
By rstepler in forum Java 2DReplies: 2Last Post: 08-20-2013, 11:03 AM -
Shape drawing applet not working...
By evapisces in forum Java AppletsReplies: 0Last Post: 11-18-2008, 12:46 AM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 01:26 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 03:23 AM -
Drawing on aJPanel
By Djangolo in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 02:01 AM
Bookmarks