Results 1 to 10 of 10
  1. #1
    nikita is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default setting applet's size

    hello all,
    i'm new here so please be patient. anyhow i have an applet which is driving me nutz. it is an example given to me by my instructor. if i start it with the appletviewer, it shows the top part of the animation; i have to resize the frame and minimize the window to get it to show. all my attempts to set the size have failed. could use a little help ... thanks.

    Java Code:
    
    /*
    
    This applet will have a stick figure doing a jumping jack
    
    */
    
    
    
    import java.awt.*;
    
    import javax.swing.*;
    
    
    
    public class stickman extends JApplet
    
    {
    
    	int index = 0;
    
    	int[] leftarmx = {100,90,85,80,72,72,70,70,70,72,75,80};
    
    	int[] leftarmy = {200,190,180,170,160,150,140,130,120,110,100,90};
    
    	int[] rightarmx = {200,210,215,220,228,228,230,230,230,228,225,220};
    
    	int[] rightarmy = {200,190,180,170,160,150,140,130,120,110,100,90};
    
    	int[] leftlegx = {145,140,135,130,125,120,115,110,105,100,95,90};
    
    	int[] leftlegy = {280,280,280,280,280,280,280,280,280,280,280,280};
    
    	int[] rightlegx = {155,160,165,170,175,180,185,190,195,200,205,210};
    
    	int[] rightlegy = {280,280,280,280,280,280,280,280,280,280,280,280};
    
    	int sleep = 50;
    
    	boolean goingup = true, first = true;
    
    	Container c;
    
    	
    
    	public void init()
    
    	{
    
    		c = getContentPane();
    
    		c.setBackground(Color.cyan);
    
    		setForeground(Color.blue);
    
    
    
    		index =0;
    
    		goingup= true;
    
    
    
    	}
    
    	public void start()
    
    	{
    
    		//setLayout(null);
    
    		setPreferredSize(new Dimension(500,500));
    
    		first = true;
    
    		repaint();
    
    
    
    	}
    
    	public void update(Graphics g)
    
    	{
    
    		paint(g);
    
    
    
    	}
    
    	public void paint(Graphics g)
    
    	{
    
    		if(first)
    
    		{
    
    			super.paint(g);
    
    			first = false;
    
    			g.setColor(getForeground());
    
    			g.drawOval(120, 40, 60, 60);
    
    			g.drawLine(150, 100, 150, 190);
    
    		}
    
    		else
    
    		{
    
    			g.setColor(c.getBackground());
    
    			g.drawLine(150,130, leftarmx[index],leftarmy[index]);
    
    			g.drawLine(150, 130, rightarmx[index], rightarmy[index]);
    
    			g.drawLine(150, 190, leftlegx[index], leftlegy[index]);
    
    			g.drawLine(150, 190, rightlegx[index], rightlegy[index]);
    
    			if(goingup)
    
    			{
    
    				index++;
    
    				if(index == leftarmx.length)
    
    				{
    
    					goingup = false;
    
    					index = leftarmx.length - 1;
    
    				}
    
    			}
    
    			else
    
    			{
    
    				index--;
    
    				if(index < 0)
    
    				{
    
    					goingup = true;
    
    					index = 0;
    
    				}
    
    			}
    
    			g.setColor(c.getForeground());
    
    			g.drawLine(150, 130, leftarmx[index], leftarmy[index]);
    
    			g.drawLine(150, 130, rightarmx[index], rightarmy[index]);
    
    			g.drawLine(150, 190, leftlegx[index], leftlegy[index]);
    
    			g.drawLine(150, 190, rightlegx[index], rightlegy[index]);
    
    			try
    
    			{
    
    				Thread.sleep(sleep);
    
    			}
    
    			catch(InterruptedException e){}
    
    
    
    		}
    
    		repaint();
    
    	}
    
    }

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

    Default

    Add the following line of code in init() method.

    Java Code:
    setSize(500, 500);

  3. #3
    nikita is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    THANK YOU.

    so could you please explain why setPreferredSize() or overriding getPreferredSize() didn't work? all i did was place this in init() and bingo. i'd really "prefer" to know .... thanks again.

    n

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

    Default

    First of all, did you read Java docs about this?

    setPreferredSize() and getPreferredSize() are not overriding methods.

    setSize() sets the size of the component. setpreferedSize() set the preferred size of the component. In most layout managers looking at preferred size, and if not change the actual size of the component.

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

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

    Default

    Oh, exactly the same thread there. Fubarable, you are so clever on this, I've not hang on Suns' forum. I hope we have nothing to do in such situations.

  7. #7
    nikita is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default apologies

    i was unaware that posting in another forum was verbotten. i have re-read the forums rules and will abide to them. i did however find that the code example i was working from(d. grimshaw at ryerson university) was correct.
    i am using eclipse and the conflict was that a similar example had already defined an inner class:

    Java Code:
    /**
    * Drawapplet1.java
    * Illustrates a pitfall in using a Canvas with an applet
    * two solutions:
    * (1) overriding getPreferredSize()
    */
    
    import java.applet.*;
    import java.awt.*;
    
    public class DrawApplet1 extends Applet
    {
    	MyCanvas canvas;
    	public void init()
    	{
    		canvas = new MyCanvas(300, 300);
    		add(canvas);
    	}
    }
    class MyCanvas extends Canvas
    {
    	private Dimension size;
    	MyCanvas (int width, int height)
    	{
    		size = new Dimension(width, height);
    		setBackground(Color.blue);
    	}
    	// The preferred size of a Canvas object is 0x0 !
    	// The applet appears blank (white) and the setBackground()
    	// method appears not to have been called. Actually, there
    	// is a blue canvas with zero area.
    	// So, override its getPreferredSize() method so that
    	// when the JVM calls getPreferredSize() it will get
    	// your version with size widthxheight of the Dimenasion object.
    	// You get a blue square with this method.
    	// This is needed if you are using the default layout manager
    	// FlowLoyout which renders all components at their preferred size.
    	// If you have a GridLayout manager all preservered sizes are
    	// ignored and the components are stretched to fill the space
    	// available.
    	public Dimension getPreferredSize()
    	{
    		return size;
    	}
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.black);
    		g.fillRect(20, 20, size.width-20, size.height-20);
    		g.setColor(Color.white);
    		g.fillOval(40, 40, 50, 50);
    	}
    }
    i wish i had more time to explore this but i'll try to get back to it by thursday. again i apologize for the cross posting; it will not happen again.

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

    Default

    i was unaware that posting in another forum was verbotten.
    I'm not sure if it is forbidden, but it is poor netiquette. It will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders. If you feel you must cross-post, I recommend that you explicitly state in all cross-posts that you are doing this and provide links to all the cross-posts. YMMV.

  9. #9
    nikita is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default that's why i said ...

    IT WILL NOT HAPPEN AGAIN.

    i have few resources where i am for java help. my current instructor is okay, but ... i really need to get better and the lack of cohesive information is disheartening. i don't want to alienate myself from help. if i had waited 2 minutes i would have seen the "correct" answer was already posted here. i have to go to class. take care.

    n

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

    Default

    Quote Originally Posted by Fubarable View Post
    I'm not sure if it is forbidden, but it is poor netiquette. It will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders. If you feel you must cross-post, I recommend that you explicitly state in all cross-posts that you are doing this and provide links to all the cross-posts. YMMV.
    You are right Fubarable. It's not forbidden at the moment. But it's not a good habit. What my believe is, wait until someone replaying to your post and respect for there help. If you are urgent the solution is not the cross-posting. Idea of a general discussion forum is not like that. In my view no one wants to write codes for others in such a forum, they want/need/trying to guide someone to correct way. If you want to find some to write codes for you, there are different type of communities, where you can pay and find solutions.

    What I'm doing is, if someone cross-posted his/her question, no need to spend time on that thread. Reason is, say I replay and give the answer to a thread. Then thread starter saying in another cross-posted thread, found the solution my self. Do you think it's good to see? No, never....

Similar Threads

  1. ResultSet size
    By bugger in forum JDBC
    Replies: 18
    Last Post: 06-25-2011, 08:36 PM
  2. how to set font size for a xml value
    By bala_kj in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2008, 11:00 AM
  3. String max size
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:29 PM
  4. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 09:27 AM
  5. Object size
    By kavithas in forum New To Java
    Replies: 0
    Last Post: 11-30-2007, 12:00 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
  •