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.
Printable View
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.
What you mean by INT? Is it int?
like..
int bob=200;
and i need to print that.
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?
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..
To convert int to a string use wrapper classes.
Code:
String str = Integer.toString(200);
but it will not convert 5 value in to Five....
You mean that convert into the character sequence, not for an String object. Thread starter mentioned about that, I'm not clear on that?
int Bob=200;
String PrintBob = Integer.toString(bob);
g.drawString(bob, 50, 100);
thats it. perfect! thanks alot guys!
So you just want to convert an int into a String? What you want to do with drawString()?
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);
}
}
and BTW the buttons do nothing atm
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. :)
In general, objects have a toString() method for this purpose. For better control, String.format() is usually the way to go.
Are you commenting on specific code segment?
No, I was giving a general answer to the initial question.