Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /var/www/minitokyo/www/includes/common.inc.php on line 360 Java applets problem :( - Minitokyo

Java applets problem :(

page 1 of 1 6 total items

I've learned just the basics(... not even those actually) of Java Applets...
So my question is...
Can the method drawString() not draw the '\n'(new line) character?
I tried to draw a stickman with 'o's and '//'s and 'I's and '\n'

But, the chars don't go to the next line >:(

I couldn't find the answer on google either...

Help >_<
I hope this isn't the wrong place to post it...

Why die only once when you can die a little everyday

UsagixKitsune

UsagixKitsune

nsɐƃıxʞıʇsnuǝ

drawString can't handle multiple lines so you have to split the string and draw each line individually. The last method does this
private void drawString(Graphics g, String text, int x, int y)

I hope this code is useful, you didn't post your own source code so I don't know how relevant the context of a JFrame and everything is but hopefully you can understand the concept and make it work ^_^V

////////////////////code/////////////////////
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class myPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(" line1 \n line2 \n unyan ^_^", 75, 100); //this is normal drawString, as you can see it doesn't make a new line at \n

int startX = 75;
int startY = 100;
String text = " line1 \n line2 \n unyan ^_^";

for (String line : text.split("\n")) { //this splits the string into smaller strings starting with \n
g.drawString(line, startX, startY += g.getFontMetrics().getHeight()); //and then prints each string
} //the height of the font gets added to startY each time to make the text print on a new line

}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("UxKism");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(3); //halts the program when you click the red x

myPanel panel = new myPanel();
frame.add(panel);
frame.setVisible(true);
}
}
////////////////////end//////////////////////

^ gee thanks :)
Well the problem is that... I don't know what the javax and JPanel and JFrame are... :(
But, oh well... I think that with my current knowledge... it is difficult to make that program

But thanks for your efforts

If you still want to look at my fail(utter fail) program...

import java.applet.*;
import java.awt.*;
public class Jump1 extends Applet implements Runnable
{

String jump1="\n\n\nabcd";/*actually I'd made two stickman figures in jump1 and jump2
but then deleted them cuz they were looking absurd without /n*/
String jump2="Hello";
String msg;
boolean sign;
Thread t=null;

public void init()
{
setBackground(Color.blue);
setForeground(Color.white);
Font f=new Font("Courier",Font.BOLD,15);
setFont(f);
}

public void start()
{
t=new Thread(this);
sign=true;
t.start();
}

public void run()
{
boolean flag=false;
while(sign)
{
if(flag){msg=jump2;}
else {msg= jump1;}
flag=!flag;
repaint();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{}
}
}

public void stop()
{sign=false; t=null;
}

public void paint(Graphics g)
{
g.drawString(msg,10,10);
}
}

Why die only once when you can die a little everyday

UsagixKitsune

UsagixKitsune

nsɐƃıxʞıʇsnuǝ

Sorry I gave you alot of irrelevant code. ^_^;
I added in the relevant part to yours.

//////////////////////////code/////////////(...)

import java.applet.*;
import java.awt.*;
public class Jump1 extends Applet implements Runnable
{

String jump1="\n\n\nabcd";/*actually I'd made two stickman figures in jump1 and jump2
but then deleted them cuz they were looking absurd without /n*/
String jump2="H\ne\nl\nl\no"; //this was originally "Hello" I just wanted to show you the \n working
String msg;
boolean sign;
Thread t=null;

public void init()
{
setBackground(Color.blue);
setForeground(Color.white);
Font f=new Font("Courier",Font.BOLD,15);
setFont(f);
}

public void start()
{
t=new Thread(this);
sign=true;
t.start();
}

public void run()
{
boolean flag=false;
while(sign) {
if(flag){msg=jump2;}
else {msg= jump1;}
flag=!flag;
repaint();
try
{
Thread.sleep(1000); //this is milliseconds by the way. It was originally 100 and that was too fast for me ^_^;
}
catch(InterruptedException e)
{}
}
}

public void stop()
{sign=false; t=null;
}

public void paint(Graphics g)
{
String text = msg; //new line
int startX = 10; //new line
int startY = 10; //new line
for (String line : text.split("\n")) { //this splits the string into smaller strings starting with \n //new line
g.drawString(line, startX, startY += g.getFontMetrics().getHeight()); //and then prints each string //new line
} //the height of the font gets added to startY each time to make the text print on a new line //new line
//g.drawString(msg,10,10); //you don't need this anymore so I commented it out
}
}
//////////////////////////end//////////////(...)

hope this helps ^_^

Great!
You're soooo nice :)
Thank You :D
It runs!
watch me jump in joy...
replace jump1 by "\n\n\n\n\n(5)o\n(3)__i__\n(5)i\n(5)i\n(4)/ \\\n(4)\\ /\n"
and jump2 by "(5)o\n(5)i\n(4)/i\\\n(3)/ i \\\n(4)I I\n \(4)I I\n(4)^ ^\n"
(the numbers in brackets indicate no. of blank spaces... MT trims them >_<)
and sleep(1000) by sleep(200) :)

Why die only once when you can die a little everyday

UsagixKitsune

UsagixKitsune

nsɐƃıxʞıʇsnuǝ

lol ^_^
and no problem.

page 1 of 1 6 total items

Back to Computers & Internet | Active Threads | Forum Index

Only members can post replies, please register.

Warning: Undefined array key "cookienotice" in /var/www/minitokyo/www/html2/footer.html on line 73
This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read more.