-
Applet question: Changing integers to special string
code:
import javax.swing.*;
import java.awt.Graphics;
public class L5a1 extends JApplet{
int num1,
count;
public void init()
{
String one;
one = JOptionPane.showInputDialog ( "Enter first number, 1-30 please:" );
//converts string to integer
num1 = Integer.parseInt(one);
}
public void paint ( Graphics g )
{
String output = "";
if (num1 > 30 || num1 < 0 )
{
g.drawString("The number does not meet specs!", 10, 10);
}
for (int count = 0; count < num1; count++)
{
g.drawString("*", count, 10);
output += "*";
}
}
}
I need to write an applet that reads five numbers (each between 1 and 30). For each number read, I need todraw a line containing that number of adjacent asterisks. If it reads the number seven, it should print *******.
this is what i have now, but im not sure why the spacing of the astericks are so small, they are bunching up...
please help thanks
-
You are only increasing your count variable by 1 each time, which effectively draws each new asterick one pixel further to the right. Try passing count*10 to the function instead of just count.
-
once again thanks
Hi thanks for the reply once again. I didn't know it would space like that and not auto space to a readable length. O well thanks for the help, I appreciate it once again
-
Re: once again thanks
Originally posted by firewire
Hi thanks for the reply once again. I didn't know it would space like that and not auto space to a readable length. O well thanks for the help, I appreciate it once again
erm.. a graphics context is a drawing area for anything, lines, shapes etc.. a character is a special shape, but a shape none the less.. it will be drawn with its top left corner at the point that you specify.. it will never intelligently autospace the characters, because it doesnt know what youre drawing is a character.. it's just a bunch of pixels (words dont mean very much to any computer)
for autospacing, as you call it.. you should make the entire string first, then paint it.. ive given you an example in the other forum.. ps; please dont crosspost
-
ALright thanks for the explanation. Sorry, but i just wasn't getting a reply, thanks.
-
it's frustrating, i know.. but these are quiet forums.. www.codeguru.com has more activity (and a guy called cjard regularly pwns the java forum there too. too much time on his hands, that dude)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks