Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default Changing default Applet Viewer Size?

    I'm new to Java Applet programming and I was trying to make a basic program where you bounce a ball around a screen. It works, but whenever I run it the Applet that appears is a tiny rectangle in the top corner of the screen. When I click to maximize it gets bigger, but whenever the ball goes outside the boundaries of the original applet it disappears from view. I want to know if there is a way to set the size of the applet myself. If you want to have a look at my code it's here:
    Java Code:
    // import necessary packages
    import java.applet.*;
    import java.awt.*;
    
    // Inherit the applet class from the class Applet
    public class FirstApplet extends Applet implements Runnable
    {
    	int x_pos = 10;
    	int y_pos = 100;
    	int x_speed=1;
    	int y_speed=1;
    	int radius = 20;
    	int appletsize_x = 300; 
    	int appletsize_y = 300;	
    
    	
    	// Needed for double buffering
    	private Image dbImage;
    	private Graphics dbg;
    
         
          public void init() {
        	  setBackground (Color.BLUE);
          }
    
         
          public void start() {
        	  Thread th =new Thread(this);
        	  th.start();
          }
    
    
          public void stop() { }
    
        
         public void destroy (){ }
          
          public void run(){
        	// lower ThreadPriority
        	  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
        	  // run a long while (true) this means "always"
        	  while (true)
        	  {
        		// Ball is bounced if its x - position reaches the right border of the applet
        		  if (x_pos > appletsize_x - radius)
        		  {
    
        		        // Change direction of ball movement
        		        x_speed = -1;
    
        		  }
        		  // Ball is bounced if its x - position reaches the left border of the applet
       		  else if (x_pos < radius)
       		  {
    
        		        // Change direction of ball movement
        		        x_speed = +1; 
        		  }
        		  if (y_pos>appletsize_y - radius){
        			  y_speed=-1;
        		  }
        		  else if (y_pos< radius){
        			  y_speed= +1;
        		  }
        		  	x_pos+=x_speed;
        		  	y_pos+=y_speed;
    
        	        // repaint the applet
        	        repaint();
    
        	        try
        	        {
        	              // Stop thread for 20 milliseconds
        	              Thread.sleep (20);
        	        }
        	        catch (InterruptedException ex)
        	        {
        	              // do nothing
        	        }
    
        	        // set ThreadPriority to maximum value
        	        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    
        	  } 
        	  
          }
    
        
          public void paint (Graphics g)
          {
    
                // set color
                g.setColor (Color.red);
    
                // paint a filled colored circle
                g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    
          }
          /** Update - Method, implements double buffering */
          public void update (Graphics g)
          {
    
                // initialize buffer
                if (dbImage == null)
                {
                      dbImage = createImage (this.getSize().width, this.getSize().height);
                      dbg = dbImage.getGraphics ();
                }
    
                // clear screen in background
                dbg.setColor (getBackground ());
                dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
                // draw elements in background
                dbg.setColor (getForeground());
                paint (dbg);
    
                // draw image on the screen
                g.drawImage (dbImage, 0, 0, this);
    
          }
    
    }

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

    Default

    I believe that the Applet size is set by the web page that holds it. I would suggest that you:
    1) have you paint routine get the applet size each time it runs and use the true applet size to figure out where the walls should be, and
    2) Use Swing and JApplets, not AWT and Applets for more robust and flexible programming.

    For example:

    FirstJAppletPanel.java
    Java Code:
    package dy09.m02.a;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    /**
     * create JPanel that can be used in JFrame or
     * a JApplet
     * @author Pete
     */
    public class FirstJAppletPanel extends JPanel
    {
      private static final int TIMER_DELAY = 20;
      private static final int RADIUS = 20;
      private int x = 10;
      private int y = 100;
      private int xSpeed = 1;
      private int ySpeed = 1;
      private Timer timer = new Timer(TIMER_DELAY, new TimerAction());
      
      public FirstJAppletPanel()
      {
        setBackground(Color.blue);
        timer.start();
      }
      
      @Override
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.red);
        g.fillOval(x, y, 2*RADIUS, 2*RADIUS);
      }
      
      public void move()
      {
        Dimension d = getSize();
        if (x < 0)
        {
          xSpeed = Math.abs(xSpeed);
        }
        else if (x > d.width - 2*RADIUS)
        {
          xSpeed = -Math.abs(xSpeed); 
        }
        if (y < 0)
        {
          ySpeed = Math.abs(ySpeed);
        }
        else if (y > d.height - 2*RADIUS)
        {
          ySpeed = -Math.abs(ySpeed); 
        }
        x += xSpeed;
        y += ySpeed;
        repaint();
      }
      
      private class TimerAction implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          move();
        }
      }
    }
    FirstJApplet.java
    Java Code:
    package dy09.m02.a;
    
    import javax.swing.JApplet;
    
    public class FirstJApplet extends JApplet
    {
      public void init()
      {
        try
        {
          javax.swing.SwingUtilities.invokeAndWait(new Runnable()
          {
            public void run()
            {
              createGUI();
            }
          });
        }
        catch (Exception e)
        {
          System.err.println("createGUI didn't successfully complete");
        }
      }
    
      private void createGUI()
      {
        getContentPane().add(new FirstJAppletPanel());
      }
    }
    Last edited by Fubarable; 02-11-2009 at 03:37 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Thread starter, if you define the size of the applet in the HTML code, it'll work. Did you try that?

  4. #4
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default thanks

    Alright thanks alot guys, I'll try it.

  5. #5
    bobthebob is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    I have a similar question but it wasn't answered above - basically I just want to know if there is a way to set the default size of the appletViewer window so I don't have to always manually resize it.

    I know how to size the applet window in HTML but I am doing quick debugging in Eclipse so I just launch into appletViewer without the HTML wrapper.

    so - how to?

    cheers,
    b

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Can you pass the appletviewer program a commandline arg?
    Put in the html file.
    Or add a few lines of HTML in a comment in your program with the <APPLET & </APPET tag s and pass the program source to Appletviewer.

  7. #7
    bobthebob is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks - I'll try those.

    I did figure out that if you simply use the 'setSize' method you can make the window any size you want.

    I'd still appreciate a way to set it as a default, that way I wouldn't have to maybe re-edit the code when it came time to embed in a web page.
    but then perhaps your advice re: the <applet> tag will work for both.

    cheers,
    bennett

Similar Threads

  1. Changing size of Array
    By ravian in forum New To Java
    Replies: 3
    Last Post: 06-05-2012, 08:17 PM
  2. changing font size
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 12-25-2008, 07:48 AM
  3. java applet viewer dimensions
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-08-2008, 10:44 PM
  4. Eclipse Applet Viewer not showing graphics?
    By sjchase in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:16 PM
  5. Swing - Changing component default font
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 10:52 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
  •