Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2008, 06:51 AM
Member
 
Join Date: Oct 2008
Posts: 30
Rep Power: 0
Urgle is on a distinguished road
Default 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:

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;
  }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-19-2008, 06:58 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,478
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-19-2008, 07:01 AM
Member
 
Join Date: Oct 2008
Posts: 30
Rep Power: 0
Urgle is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-19-2008, 07:04 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,478
Rep Power: 8
Fubarable is on a distinguished road
Default
Originally Posted by Urgle View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-19-2008, 07:48 AM
Member
 
Join Date: Oct 2008
Posts: 30
Rep Power: 0
Urgle is on a distinguished road
Default
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

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;
  }
  
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-19-2008, 08:37 AM
Member
 
Join Date: Oct 2008
Posts: 30
Rep Power: 0
Urgle is on a distinguished road
Default
I figured it out, I just put a couple simple if else statements in there.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-19-2008, 09:38 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,478
Rep Power: 8
Fubarable is on a distinguished road
Default
I'm glad. Now tell us what happens to all the drawings when you minimize your applet and then restore it?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shape drawing applet not working... evapisces Java Applets 0 11-18-2008 12:46 AM
Drawing a map Karp AWT / Swing 4 11-07-2008 01:26 PM
Using Piccolo for Drawing rstepler Java 2D 0 07-01-2008 01:51 AM
Help with 2-D Drawing Deathmonger New To Java 4 06-18-2008 03:23 AM
Drawing on aJPanel Djangolo AWT / Swing 1 02-17-2008 02:01 AM


All times are GMT +2. The time now is 10:07 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org