Solved

Newbie Java Applet help

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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