Solved

Newbie Java Applet help

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 87
JUnit 4 @Before and @BeforeClass differences 3 59
Why my table column Id is not passed to java object? 4 38
CSV file parsing thru Java 13 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

786 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