Results 1 to 7 of 7

Thread: Canvas

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404

    Canvas

    Anyone know how to move a Canvas?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    canvas1.setLocation(10,10);
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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.

    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);
    	}
    }
    running it in visual studio i just get a blank applet!!

    Any help would be gratefully appreciated here !

  4. #4

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    I'm off home now but I'll check for a reply tommorow....
    Thanks!

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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);
    	}
    }
    I changed it a little bit, it starts at the bottom, and scrolls 'Hello World' to the top, then starts at the bottom again.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    cheers M8

    I'll try it at work tommorow!

  7. #7

    Thread Starter
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width