Results 1 to 2 of 2
  1. #1
    tomjrmkay is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default Problem with Applet

    hi i have written a java applet just for messing around with because its my first one and i have a question. this is my code:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Keyboard1 extends Applet
       implements KeyListener, MouseListener {
    
       int width, height;
       int x, y, ex, ey;
       String s = "";
       String enter = "Mouse is NOT in the applet.";
    // Color changing buttons
    
       CheckboxGroup radioGroup;
       Checkbox radio1;
       Checkbox radio2;
       Checkbox radio3;
    
    // End Color changing buttons
    
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.gray );
    
          x = width/2;
          y = height/2;
    
          addKeyListener( this );
          addMouseListener( this );
    
    // Color changing buttons
    
    setLayout(new FlowLayout());
    radioGroup = new CheckboxGroup();
    radio1 = new Checkbox("Red",radioGroup,false);
    radio2 = new Checkbox("Green",radioGroup,true);
    radio3 = new Checkbox("Blue",radioGroup,false);
    add(radio1);
    add(radio2);
    add(radio3);
    
    //End Color chaning buttons
       }
    
       public void keyPressed( KeyEvent e ) { }
       public void keyReleased( KeyEvent e ) { }
       public void keyTyped( KeyEvent e ) {
          char c = e.getKeyChar();
          if ( c != KeyEvent.CHAR_UNDEFINED ) {
             s = s + c;
             repaint();
             e.consume();
          }
       }
    
       public void mouseEntered( MouseEvent e ) {
          enter = "Mouse IS in the applet.";
          repaint();
          e.consume();
       }
       public void mouseExited( MouseEvent e ) {
          enter = "Mouse is NOT in the applet.";
          repaint();
          e.consume();
       }
       public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
          ex = e.getX();
          ey = e.getY();
          repaint();
          e.consume();
       }
       public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
       }
       public void mousePressed( MouseEvent e ) { }
       public void mouseReleased( MouseEvent e ) { }
       public void mouseClicked( MouseEvent e ) {
          x = e.getX();
          y = e.getY();
          s = "";
          repaint();
          e.consume();
       }
    
       public void paint( Graphics g ) {
          g.setColor( Color.magenta );
          g.drawLine( x, y, x, y-10 );
          g.drawLine( x, y, x+10, y );
    if (radio1.getState()) {
    g.setColor( Color.red );
    } else if (radio2.getState()) {
    g.setColor( Color.green ); 
    } else {
    g.setColor( Color.blue );
    }
          g.drawString( s, x+10, y-3 );
          g.setColor( Color.cyan );
          g.drawString( enter, 10, 25 );
          g.setColor( Color.red );
          g.drawString( "Click where you want to type then type something.", 10, 10 );
          g.setColor( Color.yellow );
          g.drawString( "Your Coords: ("+x+", "+y+")", 10, 40 );
       }
    }
    When i try to change the color it doesnt work. nothing shows up. it worked fine before i added the changing colors so if anyone knows please reply. thanks.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

  1. applet problem
    By shwein in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 12:17 AM
  2. Serious applet problem
    By willemjav in forum Java Applets
    Replies: 1
    Last Post: 03-25-2008, 06:01 PM
  3. applet problem plz HELP
    By shibajisanyal in forum Java Applets
    Replies: 1
    Last Post: 01-09-2008, 02:47 AM
  4. problem with jscrollbar in my applet
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 12:39 AM
  5. Applet problem
    By Ed in forum Java Applets
    Replies: 2
    Last Post: 07-02-2007, 04:35 PM

Posting Permissions

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