Applets
Posted: Tue Mar 07, 2006 07:21
Ok so, sorry for using this, it's the easiest way I could think of... =P
[syntax="java"]
import java.awt.*;
import javax.swing.*;
public class TablaDiez extends JApplet
{
public void paint(Graphics g)
{
String txt;
super.paint(g);
txt="\nx x² x³\n";
for(int i=0;i<=10;i++)
{
txt+="\n"+i+" "+(i*i)+" "+(i*i*i);
}
g.drawString(txt,25,25);
}
}
[/syntax]
Ok so this thing works on regular programs, but in the applet it puts it all in one line, how do I fix that?
Also, it seems that the g.drawString() thing isn't like JOptionPane.showMessagedialog() or the System.out.println() things, do I have to keep saving everything in one variable to show it? or can I use g.drawString() many times? How do I make that I don't have to specify the exact place where I cant it to show? (g.drawString(txt,25,25);)
Ok, so far so good, next code:
[syntax="java"]
import java.awt.*;
import javax.swing.*;
public class Multiplos extends JApplet
{
String txt;
int in,m[]=new int[100];
public void init()
{
String sn;
int c=1;
sn=JOptionPane.showInputDialog("number: ");
in=Integer.parseInt(sn);
for(int i=0;i<in;i++)
{
for(int j=0;j<=i;j++)
{
if(in%i==0)
{
m[c]=i;
c++;
}
}
}
for(int i=0;i<c;i++)
{
txt+=m[i]+" ";
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(15,10,270,40);
g.drawString(txt,25,25);
}
}
[/syntax]
So on this one I noticed I had to make everything on a different method, I couldn't use the same paint one I was using before... If I left it in the same method it kept asking me for the number over and over again and do nothing else but drive me insane asking for the number, even though I just put it once and it wasn't on a loop. It asked for the number just once when I put it outside the method, so I left it there. I used as a base some other program I found on the book, not sure if the init() name is a must like the pain() name or it can change, but since it was that way I didn't change it.
So this one asks me for the number once, but does nothing else, it shows nothing and, yea, nothing is pretty much everything. It doesn't even show the rectangle there...
[syntax="java"]
import java.awt.*;
import javax.swing.*;
public class TablaDiez extends JApplet
{
public void paint(Graphics g)
{
String txt;
super.paint(g);
txt="\nx x² x³\n";
for(int i=0;i<=10;i++)
{
txt+="\n"+i+" "+(i*i)+" "+(i*i*i);
}
g.drawString(txt,25,25);
}
}
[/syntax]
Ok so this thing works on regular programs, but in the applet it puts it all in one line, how do I fix that?
Also, it seems that the g.drawString() thing isn't like JOptionPane.showMessagedialog() or the System.out.println() things, do I have to keep saving everything in one variable to show it? or can I use g.drawString() many times? How do I make that I don't have to specify the exact place where I cant it to show? (g.drawString(txt,25,25);)
Ok, so far so good, next code:
[syntax="java"]
import java.awt.*;
import javax.swing.*;
public class Multiplos extends JApplet
{
String txt;
int in,m[]=new int[100];
public void init()
{
String sn;
int c=1;
sn=JOptionPane.showInputDialog("number: ");
in=Integer.parseInt(sn);
for(int i=0;i<in;i++)
{
for(int j=0;j<=i;j++)
{
if(in%i==0)
{
m[c]=i;
c++;
}
}
}
for(int i=0;i<c;i++)
{
txt+=m[i]+" ";
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawRect(15,10,270,40);
g.drawString(txt,25,25);
}
}
[/syntax]
So on this one I noticed I had to make everything on a different method, I couldn't use the same paint one I was using before... If I left it in the same method it kept asking me for the number over and over again and do nothing else but drive me insane asking for the number, even though I just put it once and it wasn't on a loop. It asked for the number just once when I put it outside the method, so I left it there. I used as a base some other program I found on the book, not sure if the init() name is a must like the pain() name or it can change, but since it was that way I didn't change it.
So this one asks me for the number once, but does nothing else, it shows nothing and, yea, nothing is pretty much everything. It doesn't even show the rectangle there...