Results 1 to 7 of 7
- 02-11-2009, 04:12 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
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); } }
-
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(); } } }
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 04:37 AM.
- 02-11-2009, 08:47 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Thread starter, if you define the size of the applet in the HTML code, it'll work. Did you try that?
- 02-13-2009, 03:19 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
thanks
Alright thanks alot guys, I'll try it.
- 08-27-2010, 06:59 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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
- 08-27-2010, 07:28 PM #6
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.
- 08-27-2010, 07:42 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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
-
Changing size of Array
By ravian in forum New To JavaReplies: 3Last Post: 06-05-2012, 09:17 PM -
changing font size
By diggitydoggz in forum New To JavaReplies: 1Last Post: 12-25-2008, 08:48 AM -
java applet viewer dimensions
By yuriythebest in forum Java AppletsReplies: 1Last Post: 11-08-2008, 11:44 PM -
Eclipse Applet Viewer not showing graphics?
By sjchase in forum Java AppletsReplies: 1Last Post: 04-17-2008, 05:16 PM -
Swing - Changing component default font
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:52 PM
Bookmarks