Solved

Newbie Java Applet help

Posted on 2011-09-05
3
313 Views
Last Modified: 2013-11-23
Good day Experts.
So I just started taking a Java class (no pun intended).

Instructor gave us an assignment to write an applet based on Hello World program (see attached code).  Need to output information like Name, college, age - each on their own line.

Question is, if g.drawString puts everything on one line, what's the command to go to the next line?
/* FILE: Applet1.java */
/* A Java applet. */
import java.awt.Graphics;
import java.applet.Applet;
public class Applet1 extends Applet {
public void paint( Graphics g )
{
g.drawString("Hello World!", 25, 25);
}
}
<!-- FILE: Applet1.html -->
<html>
<applet code="Applet1.class" width=275 height=125>
</applet>
</html>

Open in new window

0
Comment
Question by:Geisrud
  • 3
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 36485616
g.drawString(String s, int x, int y) - has arguments x and y which pint to exact location
where the string starts - so you need to calcyulate new values x and y so that new string starte below the previous string.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36485628


value x is number of pixels counted horizontally form the left border of your
component where you are drawing
value y is counted vertically from the top borde

so ig you have

g.drawString("Name", 25, 50)
g.darwString("Address",25, 70)

you'll see Address start just below the plcae  where "Name" starts

so next string

g.drawSring("Phone", 25, 90);

will be one line lowere

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36485632
In comparison to say
System.out.println(String) command
with darwString you have much more flexibility - you can even print
one on top of another or with minimal itervals, etc, etc

On the other hand you should take care of all formatting in your code
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now