Anyone know how to move a Canvas?
Printable View
Anyone know how to move a Canvas?
:)Code:canvas1.setLocation(10,10);
Thanks crptcblade,
I wonder if you could help me,
I have an applet class and a canvas class
I'm trying to put text in the canvas and scroll the canvas upwards, heres my code.
running it in visual studio i just get a blank applet!!Code://applet class
import java.applet.*;
import java.awt.*;
public class NewsApplet extends java.applet.Applet
implements Runnable{
private Canvas news = new TextCanvas();
private Thread thread;
private long delay = 40;
public int x = 100;
public int y = 100;
public void init(){
news.setSize(100,100);
news.setLocation(10,10);
add(news);
}
public void start(){
thread = new Thread(this);
thread.start();
}
public void run(){
try {while (true) {
repaint();
Thread.sleep(delay);
};} catch (InterruptedException e) {
// end
};
}
public void stop(){
if (thread!=null) thread.stop();
thread=null;
}
public void paint(Graphics g){
x+=1;
y+=1;
news.setLocation(x,y);
}
}
//canvas class
import java.awt.*;
import java.applet.*;
public class TextCanvas extends Canvas
{
public void paint (Graphics g){
g.setColor(Color.black);
g.drawString("hello world",100,100);
}
}
Any help would be gratefully appreciated here !
I'm off home now but I'll check for a reply tommorow....
Thanks!
I changed it a little bit, it starts at the bottom, and scrolls 'Hello World' to the top, then starts at the bottom again.Code://applet class
import java.applet.*;
import java.awt.*;
public class NewsApplet extends Applet implements Runnable{
private TextCanvas news = new TextCanvas();
private Thread thread;
private long delay = 100;
public int x = 0;
public int y = 100;
public void init()
{
}
public void start()
{
this.setBackground(Color.red);
this.add(news);
news.setBounds(0,0,100,100);
thread = new Thread(this);
thread.start();
}
public void run(){
try
{
while (true)
{
this.repaint();
Thread.sleep(delay);
}
} catch (InterruptedException e) {}
}
public void stop(){
}
public void paint(Graphics g){
if (y<0) y = 100;
else y--;
news.setLocation(0,y);
news.repaint();
}
}
class TextCanvas extends Canvas
{
public void paint (Graphics g){
g.setColor(Color.black);
g.drawString("hello world",10,10);
}
}
:)
cheers M8 ;)
I'll try it at work tommorow!
I've tried this now, and it works!
Only thing is it goes at light speed no matter what i set the delay to !
Any Ideas