Applet question: Changing integers to special string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Applet question: Changing integers to special string

  1. #1
    Join Date
    Jun 2004
    Posts
    24

    Unhappy 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

  2. #2
    Join Date
    May 2004
    Posts
    219
    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.

  3. #3
    Join Date
    Jun 2004
    Posts
    24

    Talking 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

  4. #4
    Join Date
    Feb 2004
    Posts
    808

    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Jun 2004
    Posts
    24
    ALright thanks for the explanation. Sorry, but i just wasn't getting a reply, thanks.

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    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)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center