i cant change the size.Pls help.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: i cant change the size.Pls help.

Hybrid View

  1. #1
    Join Date
    Dec 2006
    Posts
    10

    i cant change the size.Pls help.

    please help me again. i cant change the size of the square. pls help me.
    Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class snakes extends JApplet 
                      implements KeyListener, FocusListener, MouseListener {
                          // (Note:  MouseListener is implemented only so that
                          //         the applet can request the input focus when
                          //         the user clicks on it.)
    
       static final int SQUARE_SIZE = 20;  // Length of a side of the square.
       
       Color squareColor;  // The color of the square.
       
       int squareTop, squareLeft;  // Coordinates of top-left corner of square.
       
       boolean focussed = false;   // True when this applet has input focus.
       
       DisplayPanel canvas;  // The drawing surface on which the applet draws,
                             // belonging to a nested class DisplayPanel, which
                             // is defined below.
    
       public void init() {
            // Initialize the applet; set it up to receive keyboard
            // and focus events.  Place the square in the middle of
            // the applet, and make the initial color of the square red.
            // Then, set up the drawing surface.
            
          squareTop = getSize().height / 20;// - SQUARE_SIZE / 2;
          squareLeft = getSize().width / 20;// - SQUARE_SIZE / 2;
          squareColor = Color.red;
          
          canvas = new DisplayPanel();  // Create drawing surface and 
          setContentPane(canvas);       //    install it as the applet's content pane.
    
          canvas.setBackground(Color.white);  // Set the background color of the canvas.
    
          canvas.addFocusListener(this);   // Set up the applet to listen for events
          canvas.addKeyListener(this);     //                          from the canvas.
          canvas.addMouseListener(this);
          
       } // end init();
       
       
       class DisplayPanel extends JPanel {
              // An object belonging to this nested class is used as
              // the content pane of the applet.  It displays the
              // moving square on a white background with a border
              // that changes color depending on whether this 
              // component has the input focus or not.
       
          public void paintComponent(Graphics g) {
          
             super.paintComponent(g);  // Fills the panel with its
                                       // background color, which is white.
    
             /* Draw a 3-pixel border, colored cyan if the applet has the
                keyboard focus, or in light gray if it does not. */
    
             if (focussed) 
                g.setColor(Color.cyan);
             else
                g.setColor(Color.lightGray);
    
             int width = getSize().width;  // Width of the applet.
             int height = getSize().height; // Height of the applet.
             g.drawRect(0,0,width-1,height-1);
             g.drawRect(1,1,width-3,height-3);
             g.drawRect(2,2,width-5,height-5);
    
             /* Draw the square. */
    
             g.setColor(squareColor);
             g.fillRect(squareLeft = 3, squareTop = 3, SQUARE_SIZE, SQUARE_SIZE);
    
             /* If the applet does not have input focus, print a message. */
    
             if (!focussed) {
                g.setColor(Color.magenta);
                g.drawString("Click to activate",7,20);
             }
    
          }  // end paintComponent()
        
        } // end nested class DisplayPanel 
        
        
    
       // ------------------- Event handling methods ----------------------
       
       public void focusGained(FocusEvent evt) {
             // The applet now has the input focus.
          focussed = true;
          canvas.repaint();  // redraw with cyan border
       }
       
    
       public void focusLost(FocusEvent evt) {
             // The applet has now lost the input focus.
          focussed = false;
          canvas.repaint();  // redraw without cyan border
       }
       
       
       public void keyTyped(KeyEvent evt) {
              // The user has typed a character, while the
              // applet has the input focus.  If it is one
              // of the keys that represents a color, change
              // the color of the square and redraw the applet.
              
          char ch = evt.getKeyChar();  // The character typed.
    
          if (ch == 'B' || ch == 'b') {
             squareColor = Color.blue;
             canvas.repaint();
          }
          else if (ch == 'G' || ch == 'g') {
             squareColor = Color.green;
             canvas.repaint();
          }
          else if (ch == 'R' || ch == 'r') {
             squareColor = Color.red;
             canvas.repaint();
          }
          else if (ch == 'K' || ch == 'k') {
             squareColor = Color.black;
             canvas.repaint();
          }
    
       }  // end keyTyped()
       
       
       public void keyPressed(KeyEvent evt) { 
              // Called when the user has pressed a key, which can be
              // a special key such as an arrow key.  If the key pressed
              // was one of the arrow keys, move the square (but make sure
              // that it doesn't move off the edge, allowing for a 
              // 3-pixel border all around the applet).
              
          int key = evt.getKeyCode();  // keyboard code for the key that was pressed
          
          if (key == KeyEvent.VK_LEFT) {
             squareLeft -= 8;
             if (squareLeft < 3)
                squareLeft = 3;
             canvas.repaint();
          }
          else if (key == KeyEvent.VK_RIGHT) {
             squareLeft += 8;
             if (squareLeft > getSize().width - 3 - SQUARE_SIZE)
                squareLeft = getSize().width - 3 - SQUARE_SIZE;
             canvas.repaint();
          }
          else if (key == KeyEvent.VK_UP) {
             squareTop -= 8;
             if (squareTop < 3)
                squareTop = 3;
             canvas.repaint();
          }
          else if (key == KeyEvent.VK_DOWN) {
             squareTop += 8;
             if (squareTop > getSize().height - 3 - SQUARE_SIZE)
                squareTop = getSize().height - 3 - SQUARE_SIZE;
             canvas.repaint();
          }
    
       }  // end keyPressed()
    
    
       public void keyReleased(KeyEvent evt) { 
          // empty method, required by the KeyListener Interface
       }
       
    
       public void mousePressed(MouseEvent evt) {
            // Request that the input focus be given to the
            // canvas when the user clicks on the applet.
          canvas.requestFocus();
       }   
       
       
       public void mouseEntered(MouseEvent evt) { }  // Required by the
       public void mouseExited(MouseEvent evt) { }   //    MouseListener
       public void mouseReleased(MouseEvent evt) { } //       interface.
       public void mouseClicked(MouseEvent evt) { }
    
    
    } // end class KeyboardAndFocusDemo

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I cannot find a getSize method which you call in the init() method of the applet. Might that be your problem with the code?

    Later ... Oh well ... if found the getSize method which returns a Dimension object which has a height and a width field ... so it's not that ...
    Last edited by nspils; 01-31-2007 at 12:39 AM.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This works ...

    At least the square appears in the center and responds to arrowkeys.
    As for the getSize() method, it will return a 0,0 dimension if called before the applet is actually displayed. I'm not sure what the problem w. sizing of the square was....

    Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Snakes
        extends JApplet
        implements KeyListener, FocusListener, MouseListener {
      // (Note:  MouseListener is implemented only so that
      //         the applet can request the input focus when
      //         the user clicks on it.)
    
      static final int SQUARE_SIZE = 20; // Length of a side of the square.
    
      Color squareColor; // The color of the square.
    
      Point topLeft=null; // Coordinates of top-left corner of square.
      Rectangle square; // the moveable square
    
      boolean focussed = false; // True when this applet has input focus.
    
      DisplayPanel canvas; // The drawing surface on which the applet draws,
      // belonging to a nested class DisplayPanel, which
      // is defined below.
    
      public void init() {
        // Initialize the applet; set it up to receive keyboard
        // and focus events.  Place the square in the middle of
        // the applet, and make the initial color of the square red.
        // Then, set up the drawing surface.
    
        squareColor = Color.red;
    
        canvas = new DisplayPanel(); // Create drawing surface and
        setContentPane(canvas); //    install it as the applet's content pane.
    
        canvas.setBackground(Color.white); // Set the background color of the canvas.
    
        canvas.addFocusListener(this); // Set up the applet to listen for events
        canvas.addKeyListener(this); //                          from the canvas.
        canvas.addMouseListener(this);
    
      } // end init();
      /**
       * small main for testing, treats applet as a panel
       * @param args
       */
      public static void main(String[] args) {
        JFrame f=new JFrame();
        f.getContentPane().setLayout(new GridLayout());
        Snakes s=new Snakes();
        f.getContentPane().add(s);
        f.setBounds(10,10,400,400);
        s.init();
        f.setVisible(true);
      }
    
      class DisplayPanel
          extends JPanel {
        // An object belonging to this nested class is used as
        // the content pane of the applet.  It displays the
        // moving square on a white background with a border
        // that changes color depending on whether this
        // component has the input focus or not.
    
        public void paintComponent(Graphics g) {
    
          super.paintComponent(g); // Fills the panel with its
          // background color, which is white.
    
          /* Draw a 3-pixel border, colored cyan if the applet has the
             keyboard focus, or in light gray if it does not. */
    
          if (focussed)
            g.setColor(Color.cyan);
          else
            g.setColor(Color.lightGray);
    
          int width = getSize().width; // Width of the applet.
          int height = getSize().height; // Height of the applet.
          g.drawRect(0, 0, width - 1, height - 1);
          g.drawRect(1, 1, width - 3, height - 3);
          g.drawRect(2, 2, width - 5, height - 5);
    
          /* Draw the square. */
          if (topLeft==null) {
            /**
             * first time for paint job, the applet is now displayed and
             * has a real dimension
             */
            topLeft=new Point();
            topLeft.x=getSize().width/2 - SQUARE_SIZE/2;
            topLeft.y=getSize().height/2 - SQUARE_SIZE/2;
            square=new Rectangle(topLeft,new Dimension(SQUARE_SIZE,SQUARE_SIZE));
          }
    
          square.setLocation(topLeft);
    
          g.setColor(squareColor);
          g.fillRect(square.x,square.y,square.width,square.height);
    
          /* If the applet does not have input focus, print a message. */
    
          if (!focussed) {
            g.setColor(Color.magenta);
            g.drawString("Click to activate", 7, 20);
          }
    
        } // end paintComponent()
    
      } // end nested class DisplayPanel
    
      // ------------------- Event handling methods ----------------------
    
      public void focusGained(FocusEvent evt) {
        // The applet now has the input focus.
        focussed = true;
        canvas.repaint(); // redraw with cyan border
      }
    
      public void focusLost(FocusEvent evt) {
        // The applet has now lost the input focus.
        focussed = false;
        canvas.repaint(); // redraw without cyan border
      }
    
      public void keyTyped(KeyEvent evt) {
        // The user has typed a character, while the
        // applet has the input focus.  If it is one
        // of the keys that represents a color, change
        // the color of the square and redraw the applet.
    
        char ch = evt.getKeyChar(); // The character typed.
    
        if (ch == 'B' || ch == 'b') {
          squareColor = Color.blue;
          canvas.repaint();
        }
        else if (ch == 'G' || ch == 'g') {
          squareColor = Color.green;
          canvas.repaint();
        }
        else if (ch == 'R' || ch == 'r') {
          squareColor = Color.red;
          canvas.repaint();
        }
        else if (ch == 'K' || ch == 'k') {
          squareColor = Color.black;
          canvas.repaint();
        }
    
      } // end keyTyped()
    
      public void keyPressed(KeyEvent evt) {
        // Called when the user has pressed a key, which can be
        // a special key such as an arrow key.  If the key pressed
        // was one of the arrow keys, move the square (but make sure
        // that it doesn't move off the edge, allowing for a
        // 3-pixel border all around the applet).
    
        int key = evt.getKeyCode(); // keyboard code for the key that was pressed
    
        if (key == KeyEvent.VK_LEFT) {
          topLeft.x -= 8;
          if (topLeft.x  < 3)
            topLeft.x  = 3;
          canvas.repaint();
        }
        else if (key == KeyEvent.VK_RIGHT) {
          topLeft.x  += 8;
          if (topLeft.x  > getSize().width - 3 - SQUARE_SIZE)
            topLeft.x  = getSize().width - 3 - SQUARE_SIZE;
          canvas.repaint();
        }
        else if (key == KeyEvent.VK_UP) {
          topLeft.y -= 8;
          if (topLeft.y < 3)
            topLeft.y = 3;
          canvas.repaint();
        }
        else if (key == KeyEvent.VK_DOWN) {
          topLeft.y += 8;
          if (topLeft.y > getSize().height - 3 - SQUARE_SIZE)
            topLeft.y = getSize().height - 3 - SQUARE_SIZE;
          canvas.repaint();
        }
    
      } // end keyPressed()
    
      public void keyReleased(KeyEvent evt) {
        // empty method, required by the KeyListener Interface
      }
    
      public void mousePressed(MouseEvent evt) {
        // Request that the input focus be given to the
        // canvas when the user clicks on the applet.
        canvas.requestFocus();
      }
    
      public void mouseEntered(MouseEvent evt) {} // Required by the
    
      public void mouseExited(MouseEvent evt) {} //    MouseListener
    
      public void mouseReleased(MouseEvent evt) {} //       interface.
    
      public void mouseClicked(MouseEvent evt) {}
    
    } // end class KeyboardAndFocusDemo
    eschew obfuscation

Similar Threads

  1. change the date in the file.
    By vino in forum VB Classic
    Replies: 0
    Last Post: 03-01-2006, 07:48 PM
  2. How to change COM port number
    By quantum1976 in forum VB Classic
    Replies: 1
    Last Post: 02-17-2006, 10:29 AM
  3. How to generate Make Files?
    By aMax in forum Java
    Replies: 2
    Last Post: 08-04-2000, 05:55 PM
  4. Replies: 6
    Last Post: 03-25-2000, 12:29 PM
  5. Replies: 0
    Last Post: 03-23-2000, 01:05 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center