Applets

Tech topics & all that sweet sweet jazz

Moderator: nobile

User avatar
nobile
Kakaist
Kakaist
Posts: 522
Joined: Wed Dec 12, 2001 00:32
MySpace: www.myspace.com/nobile01
Location: Honduras
Contact:

Applets

Postby nobile » 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...
Beauty is in the eyes of the beholder <3

vrap
Kakaist
Kakaist
Posts: 89
Joined: Sun Nov 11, 2001 15:52
Location: Manipal, India
Contact:

Postby vrap » Wed Mar 08, 2006 16:33

I should actually know how to help you on this, I just haven't bothered to start studying this part of the course yet. Sorry Embarassed
Image
i'm having the most perfect hallucination

User avatar
Smeagol
Kakaist
Kakaist
Posts: 1422
Joined: Wed Nov 07, 2001 14:32
Location: Vänersborg, Sweden
Contact:

Postby Smeagol » Wed Mar 08, 2006 19:23

about it not breaking lines, try \r\n, no clue if it changes anything but it did make it work for me when a simple \n didn't do it Razz

Perhaps you need to call paint from the init ? Something like redraw or something.. /me knows nothing much of applets Razz

update(Graphics g)?
He's not heavy, he's my brother.

User avatar
nobile
Kakaist
Kakaist
Posts: 522
Joined: Wed Dec 12, 2001 00:32
MySpace: www.myspace.com/nobile01
Location: Honduras
Contact:

Postby nobile » Thu Mar 09, 2006 02:36

I'll try that at home, while I was reading in the book, noticed that the way they did it was by putting different g.drawString(); like
g.drawString("bleh",25,25);
g.drawString("blah",25,40);
so it would put it 15 pixels below (like a different line..) But that sounds kind of annoying to do if I want it to sort of put a for and keep putting different lines in the same message... I'll definitely try that \r\n thing...
Oh and vrap, if you have any suggestions, please!
Beauty is in the eyes of the beholder <3

vrap
Kakaist
Kakaist
Posts: 89
Joined: Sun Nov 11, 2001 15:52
Location: Manipal, India
Contact:

Postby vrap » Fri Mar 10, 2006 09:21

What about updating the string itself with '\n' ?
Eg. txt += "\n" or something like, everytime you are going to pass a string into drawString() . Maybe the string just keeps starting from that same cursor point so you need to move it from there everytime, or something like that :S .
You can also do that with the position integers in that same function call - instead of passing actual numbers pass int variables that you control from outside accordingly.

You can keep calling the paint() routine btw, by using repaint() . You don't necessarily need to do all the working in paint() , you can just use that to display txt whenever you've done something to it and want to update the display. As it is, paint() keeps getting called again anyway when you minimise and then restore the applet window.

Am I talking total nonsense? Confused I can't test this code because I can't install JDK on this POS comp at the moment.
Image
i'm having the most perfect hallucination


Return to “Geek Juice”

Who is online

Users browsing this forum: No registered users and 6 guests