Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Applet

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applet

    I am writing a rectangle applet. Everything works fine, runs and compiles fine. What i am supposed to do as well is restrict the maximum and minimum size of the applet such that the user cannot enter a negative number or zero for the min, and limit the max so that the rectangle will be properly displayed in the applet. I am just not sure how to do it. I was thinking to use an if statement, but i'm not completely sure. here is my code

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import java.awt.Graphics;
     
    public class rectangle extends Applet implements ActionListener
    {
    	//declare variables
    	Image logo; //declare an Image object
    	int height, width;
     
    	//construct components
     
    	Label heightLabel = new Label("Enter the height of your rectangle: ");
    	TextField heightField = new TextField(10);
    	Label widthLabel = new Label ("Enter the width of your recntagle: ");
    	TextField widthField = new TextField(10);
    	Button drawButton = new Button("Draw Rectangle");
    	Label outputLabel = new Label("Click the Draw Rectangle button to see your rectangle.");
    	public void init()
    	{
    		setMaximumSize(new Dimension(700,700));
    		setMinimumSize(new Dimension(500,500));
    		setForeground(Color.green);
    		add(companyLabel);
    		add(heightLabel);
    		add(heightField);
    		add(widthLabel);
    		add(widthField);
    		add(drawButton);
    		drawButton.addActionListener(this);
    		add(outputLabel);
    		setBackground(Color.red);
     
     
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		height = Integer.parseInt(heightField.getText());
    		width = Integer.parseInt(widthField.getText());
    		repaint();
    	}
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.green);
    		g.drawRect(125,100,width,height);
    		g.fillRect(125,100,width,height);
     
    	}
    }
    Last edited by JavaPF; November 7th, 2011 at 12:52 PM. Reason: Highlight tags!!


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Applet

    Well, to point you in the right direction;
    Yes, you are going to need an if statement in your actionPerformed method.

    However, realize that unless called inside HTML, you're applet could be re-sized. And, inside HTML, I believe it can be re-sized up to a point, so you will have to dynamically calculate the allowed maximum size of the rectangle. Also note that because you are not using some kind of layout manager (except the default FlowLayout), when your applet is resized, it ruins the organization. If you ignore that, I suppose you could use some testing and decide what the max size of the rectangle could be.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    Ok, but I'm really not sure how I would set up the if statement. The only thing I can think of is if(height > MaximumSize || width < MinimumSize){ <code> } but afterr that, I'm not sure. I'm not even sure if my IF statement is correct.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    If the sizes entered by the user are out of bounds, display an error message and ignore the invalid values.

    Are you trying to control the size of the applet in the browser's window or the size of the shape that the applet draws in the paint method?

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    Thats what i'm trying to figure out. in my first post, i put exactly what i needed to do that the teacher was asking. here it is again.


    Write an applet that draws a rectangle according to the height and width the user enters; i.e. you must get the height and width from the user and have a draw button that will draw the rectangle on the applet. Make sure you restrict the maximum and minimum size of the applet such that the user cannot enter a negative number or zero for the min, and limit the max so that the rectangle will be properly displayed in the applet. Make the default size of the applet no smaller than 400 x 400.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    There seems to be the sizes of two things to worry about. The applet and the rectangle.
    It used to be that the applet's size was determined by the width and height attributes in the applet tag.
    Can you assume that the size of the applet is fixed and only worry about the size of the rectangle that you are to draw?

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    I determined the size of the actual applet itself. But now all i have to do is control the size of the rectangle itself. But now what i am trying to do, is to have an error message appear if the user enters a size that is over the applet size. for example, in the applet tag, i declared that the height and width of the applet is 400x500. if the user were to enter in a number above 400 for the height, i would like to have an error message pop up saying that you have gone over the maximum size of the applet. I am to get the message to pop up, but the rectangle is still being drawn.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    but the rectangle is still being drawn.
    Use separate variables for getting the values and for drawing the rectangle.
    Validate the given values first. If ok, set the values used for drawing.
    Otherwise leave the old values as is.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    if i use the variables "height" and "width" as the variables to draw the rectangle, i'm not sure how to implement the separate variables for getting the values

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    how to implement the separate variables for getting the values
                    height = Integer.parseInt(heightField.getText());
    		width = Integer.parseInt(widthField.getText());
    Don't use height and width here. Use another variable that you can test to be in range. Then copy them.

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    ok, so you're saying that I should have to more lines of code that look like that, but with different variable names?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    I said: Use separate variables for getting the values and for drawing the rectangle.
    Read the user's values into local variables, test the values are in range, then copy those variable's values into the ones used to draw the rectangle.

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    can u show me an example? I've got a lot of things going through my head that I can't even think clearly.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Applet

    pseudo code:
    int val = get a value from user into a local variable
    if(test if val is good) {
       savedVal = val;  // save the good value 
    }else {
       tell user input was bad. leave savedVal as it was
    }

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    ok, so lets say i used UHgt for the local variable. so I would have

     
    int UHgt = 700;
     
    if(UHgt < 700)
    {
    Height = UHgt;
    }
    else
    {
    JOptionPane.showMessageDialog("Please enter a new height");
    }

    I can get the error message to come up, but the rectangle still draws. How do I fix that?

  16. #16
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    actually, it just came to me. I figured it out. I had my repaint function in the wrong place. by changing the place of the repaint function to inside the else statement, i was able to achieve what i wanted. thank you very much for all of your help. i really appreciate all of your help. and for the height, i actually did it a different way than u had suggested. for my applet size, i have the height set at 700 for right now. so i created an if statement using height and did it that way. so my new if statement is

     
    if ( height <= 0 || height > 700)
    {
    JOptionPane.showMessageDialog("Enter new height");
    }

    so everything works out just fine.

  17. #17
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet

    actually, I have one last question. I've been trying to set an image as my background, but have not had any success at all. I've only been able to position an image on the screen, but not able to set it as my background. How would I go abouts in doing that?

Similar Threads

  1. Replies: 0
    Last Post: October 13th, 2011, 07:42 PM
  2. Need help to run my first applet
    By piulitza in forum Java Applets
    Replies: 6
    Last Post: September 2nd, 2011, 11:52 AM
  3. Help with this applet.
    By jasonxman in forum Java Applets
    Replies: 1
    Last Post: August 24th, 2011, 12:52 AM
  4. Can't Run my Applet! Please help!
    By JJoelTheMan in forum Java Applets
    Replies: 2
    Last Post: June 10th, 2011, 07:11 AM
  5. applet img
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: April 7th, 2010, 09:14 PM