G
gzell
Hi,
I added a Canvas in a JFrame. By clicking a JButton
within the JFrame I want to access a method within the Canvas-Class.
Is there someone who can tell me, why this does not work ?
Have a nice day, Guenter
------------my Code ------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyGrafik extends JFrame{
MyCanvas2 c1 = new MyCanvas2();
JButton btn1 = new JButton("ok");
ActionListener A1 = new ActionListener( ){
public void actionPerformed(ActionEvent evt){
doIt();
}
};
public void doIt(){
c1.zeichneGerade();
}
public MyGrafik(){
getContentPane().setLayout(null);
btn1.setBounds(300,10,50,30);
getContentPane().add(btn1);
c1.setBounds(200,100,300,200);
c1.setBackground(Color.yellow);
getContentPane().add(c1);
}
public static void main(String args[]){
JFrame MyGrafik = new MyGrafik();
MyGrafik.setSize(800,600);
MyGrafik.setVisible(true);
}
}
//------------class MyCanvas2 ---------
import java.awt.*;
public class MyCanvas2 extends Canvas{
public MyCanvas2(){
super();
}
public void zeichneGerade(){
Graphics g = getGraphics();
g.setColor(Color.red);
g.drawLine(10,10,60,60);
update(g);
}
}
I added a Canvas in a JFrame. By clicking a JButton
within the JFrame I want to access a method within the Canvas-Class.
Is there someone who can tell me, why this does not work ?
Have a nice day, Guenter
------------my Code ------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyGrafik extends JFrame{
MyCanvas2 c1 = new MyCanvas2();
JButton btn1 = new JButton("ok");
ActionListener A1 = new ActionListener( ){
public void actionPerformed(ActionEvent evt){
doIt();
}
};
public void doIt(){
c1.zeichneGerade();
}
public MyGrafik(){
getContentPane().setLayout(null);
btn1.setBounds(300,10,50,30);
getContentPane().add(btn1);
c1.setBounds(200,100,300,200);
c1.setBackground(Color.yellow);
getContentPane().add(c1);
}
public static void main(String args[]){
JFrame MyGrafik = new MyGrafik();
MyGrafik.setSize(800,600);
MyGrafik.setVisible(true);
}
}
//------------class MyCanvas2 ---------
import java.awt.*;
public class MyCanvas2 extends Canvas{
public MyCanvas2(){
super();
}
public void zeichneGerade(){
Graphics g = getGraphics();
g.setColor(Color.red);
g.drawLine(10,10,60,60);
update(g);
}
}