Solved

Newbie Java Applet help

Posted on 2011-09-05
3
312 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

15 Experts available now in Live!

Get 1:1 Help Now