Results 1 to 5 of 5
  1. #1
    furry is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Lightbulb getSize() issue - Displaying object in 3 locations in Applet

    Hello,

    I have been going nuts trying to figure this out but have had absolutely no luck so I figured I'd come here and ask for help. If anyone could point me in the right direction that would be INCREDIBLE.

    I am trying to draw 3 peace signs inside of a Java Applet with one being in the top left corner, one in the center, and one in the bottom right corner. The location of the signs are supposed to remain in the same location even if the Applet is resized, so Ive been trying to base them on getSize(), but do not think I am doing it right. I have yet to figure out how to get the lines inside of the circle, but am going to worry about that after I get the circles in the correct locations. Again, any help would be GREATLY appreciated!

    Thanks in advance!

    Java Code:
    // This applet displays 3 peace signs, one in the top left corner,
    // one in the bottom right corner, and one in the center.  All
    // are based on getSize().  A motto is also displayed in the
    // bottom left hand side of the applet.
    
    import java.awt.*;
    import java.applet.*;
    
    public class test1 extends Applet
    {
       public void init()
       {
       Color myColor;
       myColor = new Color(0, 0, 0);
       //Sets color to black
       setBackground(myColor);
       Color myColorA;
       myColorA = new Color(155, 155, 0);
       //Sets color to yellow
       setForeground(myColorA);
    }
    
       public void paint(Graphics g)
    {
       //Sets the width and height of the applet
       Dimension d = getSize();
       int centerX = d.width/2;
       int centerY = d.height/2;
       int widthOfPicture = 200;
       int heightOfPicture = 200;
       int x = centerX - widthOfPicture/2;
       int y = centerY - heightOfPicture/2;
    
       //Declares myColor as a new color (yellow)
       Color myColor1;
       myColor1 = new Color(155, 155, 0);
       //Sets color to yellow
       g.setColor(myColor1.brighter());
    
       //Draws a peace sign in upper left corner
       Dimension e = getSize();
       int ulX = e.width +50;
       int ulY = e.height +50;
       int x1 = centerX;
       int y1 = centerY;
       //int widthOfPicture1 = 200;
       //int heightOfPicture1 = 200;
       g.drawArc(x1,y1,50,50,-360,360);
    
       //Draws a peace sign in the center
       Dimension f = getSize();
       int centerX2 = f.width/45;
       int centerY2 = f.height/150;
       g.drawArc(centerX2, centerY2,50,50,-360,360);
       //g.drawLine(100,150,117,142);
    
       //Draws a peace sign in the lower right corner
       Dimension h = getSize();
       int centerX3 = h.width-50;
       int centerY3 = h.height-50;
       g.drawArc(centerX3, centerY3,50,50,-360,360);
       //g.drawLine(100,150,117,142);
    
    
       //Sets Lucida as the new font in 18pt bold
       g.setFont(new Font("Lucida", Font.BOLD, 22));
    
       //Declares myColor1 as a new color (white)
       Color myColor2;
       myColor2 = new Color(255, 255, 255);
       //Sets color to white
       g.setColor(myColor2.brighter());
       //Displays a text string message
       g.drawString("Make Love, Not War!", 10, 580);
       }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    if the Applet is resized
    I don't think applets can be resized. They are assigned an area to display in by the browser and it doesn't change.

    Try to debug your code by adding some println statements to show values.
    Last edited by Norm; 10-07-2008 at 08:45 PM.

  3. #3
    furry is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    my apologies if i was unclear by that statement. what i meant by "if it is resized" is for example, if you click the corner and drag the applet to make it larger/smaller. hope this makes more sense. when i do this the circle in the bottom right stays in the bottom right no matter what, but the other ones do move a bit.

    i would try to debug, but have absolutely no idea what i am doing when debugging. i am in a 1st year java programming class and they have not shown us anything about debugging as of yet. sorry if this makes it more difficult.

    thanks for your help, i really appreciate it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    You're not going to make ANY progress programming without knowing how to debug your program. NONE! 99.9% of programs have bugs.
    Some ideas:
    First read the API doc for Graphics to see which draw methods to use. drawOval vs drawArc for example.
    Use different colors for each drawing to see where each is being drawn. Your comments misplace where the draws occur.
    Add println() of the different int variables you are using to see if their values make sense.
    Figure out how to draw one design at a time, then make it a method and pass it the x,y location where you where want it to draw, instead of writing the same code 3 times.

    Add comments to describe why the values of 45 and 150 are used.
    It'd be better if those values were defined as variables with descriptive names:
    final int DivFactor = 45; then use DivFactor in your code to doc what you are doing.

    Add the <APPLET tag to be used to execute the program as a comment at the front of your program to show how to use it.
    BTW with the <APPLET tag in your code you can do:
    appletviewer test1.java
    You wouldn't need a separate HTML page.
    Last edited by Norm; 10-07-2008 at 11:35 PM.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You have some misleading comments in your code. What they say the code should do is different from what the code actually does

    e.g.
    Java Code:
       //Sets Lucida as the new font in 18pt bold
       g.setFont(new Font("Lucida", Font.BOLD, 22));
    this comment says it should be 18pt font, the code says it will be 22pt font. You should check what font you want and make sure this is set correctly

    Java Code:
       //Declares myColor1 as a new color (white)
       Color myColor2;
       myColor2 = new Color(255, 255, 255);
    For someone just reading comments this would be very confusing. The comment says you would be resetting myColor1, when in fact you are creating a new Color. This one is not very important, but it is also very simple to fix.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  2. Replies: 0
    Last Post: 03-10-2008, 02:36 PM
  3. Replies: 0
    Last Post: 01-22-2008, 11:05 AM
  4. Object locations via grid coordinates HELP.
    By deadman_uk in forum New To Java
    Replies: 4
    Last Post: 11-18-2007, 08:32 PM
  5. Applet tabbing issue
    By pegitha in forum Java Applets
    Replies: 2
    Last Post: 04-28-2007, 10:41 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
  •