# button + canvas



## blabla (12. Apr 2006)

Ich will einen Frame bei dem ich einen Text mit hilfe eines Buttons nach rechts schieben kann.
Der Text soll sich in einer Canvas befinden.
Funzt aber irgendwie nicht. Warum?


```
import java.awt.*;
import java.awt.event.*;




public class Test extends Frame  implements ActionListener
{
	MyCanvas canvas;
	Button b;
	
	public Test()
	{
		super("Test");
		setSize(450,300);
		setLocation(300,300);
		setLayout(null);
		
		b=new Button("Right");
		add(b);
		b.addActionListener(this );
		b.setLocation(350,10);
		b.setSize(80,15);
		
		canvas=new MyCanvas();
		add(canvas);
		canvas.setSize(300,200);
		canvas.setLocation(20,20);
	
		setVisible(true);
	}
	
	
	public static void main(String[] args)
	{
		new Test();
	}


	public void actionPerformed(ActionEvent e)
	{
		canvas.repaint();
	}

}

class MyCanvas extends Canvas
{
	Color bg;
	int w,h;
	int x;
	public MyCanvas()
	{
		super();
		w=300;
		h=200;
		bg=Color.WHITE;
		setBackground(bg);
		setForeground(Color.BLACK);
		x=10;
	}
	
	public Dimension getPreferredSize()
	{
		return new Dimension(w,h);
	}
	
	public Dimension getMinimumSize()
	{
		return getPreferredSize();
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void paint(Graphics g)
	{
		g.drawString("bla bla",x+=5,30);
	}
}
```
[/code]


----------



## mattulla (12. Apr 2006)

du musst in der paint-Methode deiner MyCanvas-Klasse nur eben die paint-Methode der Superklasse Canvas aufrufen, also:


```
public void paint(Graphics g) 
   {
	   super.paint(g);
	   g.drawString("bla bla",x+=5,30); 
   }
```


----------



## André Uhres (12. Apr 2006)

```
//package awt;
/*
 * Test.java
 */
import java.awt.*;
import java.awt.event.*;
public class Test extends Frame  implements ActionListener {
    private MyCanvas canvas;
    private Button b;
    public Test() {
        super("Test");
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        setSize(450,300);
        setLocationRelativeTo(null);
        b=new Button("Right");
        add(b, BorderLayout.NORTH);
        b.addActionListener(this );
        canvas=new MyCanvas();
        add(canvas, BorderLayout.CENTER);
        setVisible(true);
    }
    public static void main(String[] args) {
        new Test();
    }
    public void actionPerformed(ActionEvent e) {
        canvas.repaint();
    }
}
class MyCanvas extends Canvas {
    Color bg;
    int w,h;
    int x;
    public MyCanvas() {
        super();
        w=300;
        h=200;
        bg=Color.WHITE;
        setBackground(bg);
        setForeground(Color.BLACK);
        x=10;
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(w,h);
    }
    public void paint(Graphics g) {
        g.drawString("bla bla",x+=5,30);
    }
}
```


----------



## blabla (12. Apr 2006)

Funktioniert jetzt auf jeden Fall besser, bis auf das die Canvasgröße nicht stimmt.
Komisch ist auch wenn ich den Frame aus den Bildschirm hinausschiebe dann ist der Text im Canvas teilweise gelöscht


----------



## mattulla (12. Apr 2006)

Das Problem ist, dass du in der paint-Methode x immer um 5 hochzaehlst. Versuche es mal so:


```
//package awt; 
/* 
* Test.java 
*/ 
import java.awt.*; 
import java.awt.event.*; 
public class Test extends Frame  implements ActionListener { 
    private MyCanvas canvas; 
    private Button b; 
    public Test() { 
        super("Test"); 
        addWindowListener(new WindowAdapter(){ 
            public void windowClosing(WindowEvent e) { 
                System.exit(0); 
            } 
        }); 
        setSize(450,300); 
        setLocationRelativeTo(null); 
        b=new Button("Right"); 
        add(b, BorderLayout.NORTH); 
        b.addActionListener(this ); 
        canvas=new MyCanvas(); 
        add(canvas, BorderLayout.CENTER); 
        setVisible(true); 
    } 
    public static void main(String[] args) { 
        new Test(); 
    } 
    public void actionPerformed(ActionEvent e) { 
    	canvas.x += 5;
        canvas.repaint(); 
    } 
} 
class MyCanvas extends Canvas { 
    Color bg; 
    int w,h; 
    int x; 
    public MyCanvas() { 
        super(); 
        w=300; 
        h=200; 
        bg=Color.WHITE; 
        setBackground(bg); 
        setForeground(Color.BLACK); 
        x=10; 
    } 
    
    public Dimension getPreferredSize() { 
        return new Dimension(w,h); 
    } 

    public void paint(Graphics g) {
        g.drawString("bla bla",x ,30); 
    } 
}
```


----------



## Guest (13. Apr 2006)

So jetzt funktioniert alles...


```
import java.awt.*;
import java.awt.event.*;

public class Test extends Frame implements ActionListener,MouseListener
{
	
	MyCanvas c;
	Button[] b;
	String[] buttonText={"Left","Right","Up","Down"};
	Panel p;
	
	public Test()
	{
		super("Hello AWT und Canvas");
		c=new MyCanvas(40,40);
		b=new Button[buttonText.length];
		p=new Panel(new GridLayout(3,2));
		
		for(int i=0;i<b.length;i++)
		{
			b[i]=new Button(buttonText[i]);
			
			b[i].addActionListener(this);
			p.add(b[i]);
		}
		
		add(BorderLayout.CENTER,c);
		add(BorderLayout.EAST,p);
		
		pack();
		addMouseListener(this);
		addWindowListener(new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						dispose(); 
						System.exit(0);
					}
				}
		);
		
		setSize(300,250);
		setVisible(true);
		
	}
	
	public static void main(String[] args)
	{
		new Test();
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand()==b[0].getActionCommand())
		{
			System.out.println(e.getActionCommand());
			c.setX(c.getX()-5);
			c.repaint();
			
			
		}
		else if(e.getActionCommand()==b[1].getActionCommand())
		{
			System.out.println(e.getActionCommand());
			c.setX(c.getX()+5);
			c.repaint();
		}
		else if(e.getActionCommand()==b[2].getActionCommand())
		{
			System.out.println(e.getActionCommand());
			c.setY(c.getY()-5);
			c.repaint();
		}
		else if(e.getActionCommand()==b[3].getActionCommand())
		{
			System.out.println(e.getActionCommand());
			c.setY(c.getY()+5);
			c.repaint();
		}
		
	}

	public void windowOpened(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e)
	{
		dispose();
		System.exit(0);
	}

	public void windowClosed(WindowEvent e)
	{
			
	}

	public void windowIconified(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowActivated(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowDeactivated(WindowEvent e)
	{
		
		
	}

	public void mouseClicked(MouseEvent e)
	{
		System.out.println(e.getButton());
		
	}

	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}
}

class MyCanvas extends Canvas implements KeyListener
{
	
	int x,y;
	
	public MyCanvas(int x, int y)
	{
		super();
		// TODO Auto-generated constructor stub
		this.x = x;
		this.y = y;
		setForeground(Color.RED);
		setBackground(Color.BLUE);
		addKeyListener(this);
	}

	public Dimension getPreferredSize()
	{
		return new Dimension(300,250);
	}
	public Dimension getMinimumSize()
	{
		return new Dimension(300,250);
	}
	
	public int getX()
	{
		return x;
	}

	public void setX(int x)
	{
		this.x = x;
	}

	public int getY()
	{
		return y;
	}

	public void setY(int y)
	{
		this.y = y;
	}

	public void paint(Graphics g)
	{
		g.fillOval(x,y,30,30);
		
	}

	public void keyTyped(KeyEvent e)
	{
		System.out.println(e.getKeyCode());
		
	}

	public void keyPressed(KeyEvent e)
	{
		
		if(e.getKeyCode()==e.VK_RIGHT)
		{
			x+=10;
			repaint();
		}
		if(e.getKeyCode()==e.VK_LEFT)
		{
			x-=10;
			repaint();
		}
		if(e.getKeyCode()==e.VK_UP)
		{
			y-=10;
			repaint();
		}
		if(e.getKeyCode()==e.VK_DOWN)
		{
			y+=10;
			repaint();
		}
		
	}

	public void keyReleased(KeyEvent e)
	{
		
		
	}

	
}
```

Gut wäre noch das die Canvas eine Konstante größe von z.B 250x200 hätte.
und die Buttons wie ein Steuerkreuz angeordnet werden. 
Das geht wahrscheinlich nur mit nem null-Layout.
[/code]


----------

