Results 1 to 16 of 16
  1. #1
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default help with type casting.

    i need to print out a number in an applet.

    i have the number saved to an INT but i need to make it in to a string to print int a

    g.printString(string, int, int);

    wtf do i do! help plz and thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    like..

    int bob=200;

    and i need to print that.

  4. #4
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    how can i convert an int to a string.

    g.drawString(String, int, int);


    i have an int saved to a variable. and i need to make that variable into a string to be printed. what do i do?

  5. #5
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Red face

    you can use switch case for each digit ...
    like in num>2 digit then u have to print 100 like that ways...
    so so many condition checking r there..
    Rakesh Mehta

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    To convert int to a string use wrapper classes.

    Java Code:
    String str = Integer.toString(200);

  7. #7
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Red face

    but it will not convert 5 value in to Five....
    Rakesh Mehta

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You mean that convert into the character sequence, not for an String object. Thread starter mentioned about that, I'm not clear on that?

  9. #9
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    int Bob=200;

    String PrintBob = Integer.toString(bob);

    g.drawString(bob, 50, 100);



    thats it. perfect! thanks alot guys!

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    well.. i used the code just o add up a score in a simple game i created.

    im in a programming class and we can do w/e we want and i decided to make a crap version of guitar hero for my 6 week project.

    ive only been programming java since the beggining of december so its not too great but im quite happy with my work.

    if you wana see all the code its right here...

    to start the game press S and to pause it press P



    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;

    public class GUITAR_HERO extends Applet implements ActionListener, MouseListener, KeyListener
    {
    // instance variables:
    Timer timer;
    private int difficulty;
    private int money;

    private boolean leftbool=true;
    private boolean upbool=true;
    private boolean downbool=true;
    private boolean rightbool=true;

    private int key;

    private Button easybutton;
    private Button mediumbutton;
    private Button hardbutton;

    private int lefty=75;
    private int upy=75;
    private int downy=75;
    private int righty=75;

    private int leftspeed=1;
    private int upspeed=1;
    private int downspeed=1;
    private int rightspeed=1;

    private int scorex=50;
    private int scorey=400;

    private boolean start=true;

    public void init()
    {
    easybutton = new Button("Easy");
    add(easybutton);
    easybutton.addActionListener(this);

    mediumbutton = new Button("Medium");
    add(mediumbutton);
    mediumbutton.addActionListener(this);

    hardbutton = new Button("Hard");
    add(hardbutton);
    hardbutton.addActionListener(this);


    timer = new Timer(15, this);
    //timer.start(); //timers

    addMouseListener(this);
    addKeyListener(this);
    setFocusable(true);





    }

    public void mouseEntered(MouseEvent me){}
    public void mouseExited(MouseEvent me){}
    public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    public void mouseClicked(MouseEvent me)
    {

    }

    public void keyPressed(KeyEvent ke)
    {
    key = ke.getKeyCode();//returns the interger coe of the key taht was pressed

    if (key==KeyEvent.VK_LEFT)//left key listener
    {
    if ( ((500-lefty) <= 110) && ((500-lefty) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-lefty) <= 130) && ((500-lefty) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_UP)//left key listener
    {
    if ( ((500-upy) <= 110) && ((500-upy) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-upy) <= 130) && ((500-upy) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_DOWN)//left key listener
    {
    if ( ((500-downy) <= 110) && ((500-downy) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-downy) <= 130) && ((500-downy) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_RIGHT)//left key listener
    {
    if ( ((500-righty) <= 110) && ((500-righty) >= 90))//awards 100 points if 20 pixals near perfect
    {
    money+=100;
    repaint();
    }

    else if ( ((500-righty) <= 130) && ((500-righty) >= 70))//awards 50 points if 60 pixals near
    {
    money+=50;
    repaint();
    }
    }

    if (key==KeyEvent.VK_P)//left key listener
    {
    timer.stop();

    }

    if (key==KeyEvent.VK_S)//left key listener
    {
    timer.start();

    }



    }
    public void keyReleased(KeyEvent ke){}
    public void keyTyped(KeyEvent ke){}



    public void actionPerformed(ActionEvent ae)
    {
    Object source = ae.getSource(); //allows buttons

    if (source.equals(easybutton))
    {
    difficulty=1000;
    start=true;


    }

    if (source.equals(mediumbutton))
    {
    difficulty=50;
    start=true;

    }

    if (source.equals(hardbutton))
    {
    difficulty=10;
    start=true;
    }
    setFocusable(true);
    repaint();

    }

    //public void update(Graphics g)
    //{
    //super.update(g);

    // paint(g);
    //money(g);

    //}

    public void paint(Graphics g)
    {


    if (start==true)
    {
    left(g);
    up(g);
    down(g);
    right(g);
    }
    money(g);
    target(g);

    }

    public void left(Graphics g)
    {
    g.fillRect(50, lefty, 50, 50);
    if (lefty==75)
    {
    leftspeed = (int) (Math.random()*4);
    }

    lefty+=leftspeed;

    if (lefty>=450)
    {
    lefty=75;
    leftbool=true;
    }


    }

    public void up(Graphics g)
    {
    g.fillRect(150, upy, 50, 50);
    if (upy==75)
    {
    upspeed = (int) (Math.random()*4);
    }
    upy+=upspeed;

    if (upy>=450)
    {
    upy=75;
    upbool=true;
    }

    }

    public void down(Graphics g)
    {
    g.fillRect(250, downy, 50, 50);
    if (downy==75)
    {
    downspeed = (int) (Math.random()*4);
    }
    downy+=downspeed;

    if (downy>=450)
    {
    downy=75;
    downbool=true;
    }
    }

    public void right(Graphics g)
    {
    g.fillRect(350, righty, 50, 50);
    if (righty==75)
    {
    rightspeed = (int) (Math.random()*4);
    }
    righty+=rightspeed;

    if (righty>=450)
    {
    righty=75;
    rightbool=false;
    }

    }

    public void target(Graphics g)
    {
    scorex=50;
    for (int target=1; target<=4; target++)
    {
    g.setColor(Color.GREEN);
    g.drawRect(scorex, scorey, 50, 50);
    scorex+=100;
    }


    g.setColor(Color.CYAN);
    g.drawString("left", 70, 425);

    g.setColor(Color.CYAN);
    g.drawString("up", 170, 425);

    g.setColor(Color.CYAN);
    g.drawString("down", 261, 425);

    g.setColor(Color.CYAN);
    g.drawString("right", 365, 425);
    }

    public void money(Graphics g)
    {
    g.setColor(Color.BLACK);
    String pmoney = Integer.toString(money);

    g.drawString(pmoney, 405, 50);

    }



    }

  12. #12
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    and BTW the buttons do nothing atm

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    ramsrocker, please use code tags to post your code segments posting again here. Un-formatted codes are hard to read lol.

    By the way, I'll check your code let you know about my thoughts on that. :)

  14. #14
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    In general, objects have a toString() method for this purpose. For better control, String.format() is usually the way to go.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  16. #16
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    No, I was giving a general answer to the initial question.

Similar Threads

  1. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  2. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. type casting array?
    By willemjav in forum Advanced Java
    Replies: 5
    Last Post: 08-27-2008, 10:25 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •