Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-07-2008, 09:50 PM
Member
 
Join Date: Oct 2008
Posts: 3
furry is on a distinguished road
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!

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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-07-2008, 10:42 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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 10:45 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-07-2008, 10:52 PM
Member
 
Join Date: Oct 2008
Posts: 3
furry is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-08-2008, 01:32 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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-08-2008 at 01:35 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a superclass object to subclass object dynamicly Andrefs Advanced Java 1 07-22-2008 06:27 PM
Applet - Displaying an HTML page with a selected resolution Java Tip Java Tips 0 03-10-2008 04:36 PM
New project VFSJFileChooser(browse local and remote locations) mrcheeks AWT / Swing 0 01-22-2008 01:05 PM
Object locations via grid coordinates HELP. deadman_uk New To Java 4 11-18-2007 10:32 PM
Applet tabbing issue pegitha Java Applets 2 04-29-2007 12:41 AM


All times are GMT +3. The time now is 10:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org