Results 1 to 7 of 7

Thread: Drawing Applet

  1. #1
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    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:

    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;
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    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.

  3. #3
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

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

  5. #5
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    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

    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;
      }
      
    }

  6. #6
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    I figured it out, I just put a couple simple if else statements in there.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm glad. Now tell us what happens to all the drawings when you minimize your applet and then restore it?

Similar Threads

  1. Using Piccolo for Drawing
    By rstepler in forum Java 2D
    Replies: 2
    Last Post: 08-20-2013, 11:03 AM
  2. Shape drawing applet not working...
    By evapisces in forum Java Applets
    Replies: 0
    Last Post: 11-18-2008, 12:46 AM
  3. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 01:26 PM
  4. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 AM
  5. Drawing on aJPanel
    By Djangolo in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 02:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •