Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Canvas

Thread view: 
gzell@gmx.de - 17 May 2006 18:08 GMT
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);
}

}
Knute Johnson - 17 May 2006 18:46 GMT
> Hi,
>
[quoted text clipped - 56 lines]
>
> }

Guenter:

You've got a few problems going there Guenter.  All drawing needs to be
done in the paint() or paintComponent() methods.  Also you will have
problems mixing AWT and Swing components.  If you are going to use a
JFrame use a JPanel.  If you are using a Frame then you can use a Panel
or Canvas to draw on.

Here is a simple example of how to draw different things in the same
component and how to call methods on your objects.

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

public class test9 extends Frame {
    public test9() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        final MyCanvas myc = new MyCanvas();
        myc.setSize(100,100);

        Button b = new Button("Draw");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                myc.set();
                myc.repaint();
            }
        });

        add(b,BorderLayout.WEST);
        add(myc,BorderLayout.EAST);

        pack();
        setVisible(true);
    }

    class MyCanvas extends Canvas {
        boolean isSet = false;

        public MyCanvas() {
            super();
        }

        public void set() {
            isSet = true;
        }

        public void paint(Graphics g) {
            if (isSet) {
                g.setColor(Color.RED);
                g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
            } else {
                g.drawString("Not set yet!",10,getHeight()/2);
            }
        }
    }

    public static void main(String[] args) {
        new test9();
    }
}

Signature

Knute Johnson
email s/nospam/knute/

gzell@gmx.de - 17 May 2006 19:13 GMT
Hi Knute,

>....Here is a simple example of how to draw different things in the same
> component and how to call methods on your objects.

thank you for your quick and very informativly answer. Your input
was very helpful for me to do my next beginner steps...

Ciao, Guenter


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.