?
Solved

Something on a canvas?

Posted on 2003-03-25
5
Medium Priority
?
367 Views
Last Modified: 2010-03-31
Hiya all,

I've got a class, c_RTG that extends Frame. However how can I make a canvas inside of this that allows me to draw simple lines on etc from function calls i.e:

public class c_RTG extends Frame{
   
    public Canvas myCanvas = new Canvas();

    function void f_ADV1(){
        myCanvas   //draw a line here
    }

    function void f_ADV2(){
        myCanvas   //draw something else here
    }

    function void f_BASIC(){
        add(myCanvas);
    }

    public c_RTG(){   //Just example calls, in fact only f_ADV2 may be called...
        f_BASIC();
        f_ADV1();
        f_ADV2();
    }
}


Something like that? The canvas doesn't seem to have a drawline on it like a Graphic, although I don't think I can use the paint method because there are different things that can be drawn on this canvas. I could have each one of those functions setting a variable and then the paint function switching through them, but then at each call the canvas will be wiped clean which is what I don't want.

by the way, I've got the size etc.. of the canvas sorted, but just can't figure out how to draw on it without using the paint() thingy...

Thanks
NearlyAnExpert
0
Comment
Question by:nearlyanexpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 8207067
Hi,

The important call is this.getGraphics(). This allows you to use the Graphics g similiar to that in paint(),

i.e: Graphics g = this.getGraphics();

I've provided a little testing program to demonstrate the idea. A word of warning, without telling paint() how to paint/repaint you'll lose everything on the screen, you can see this effect when the frame is minimzed and bring backup again. So you have to decide on the persistant state of yr drawings.


//Main frame class
public class c_RTG extends Frame implements ActionListener{
  myCanvas c;
  Button b;
  public c_RTG() {
    c = new myCanvas();
    b = new Button("draw circle");
    b.addActionListener(this);
    this.add("Center",c);
    this.add("South",b);
    this.setSize(500,500);
    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e){
    if(e.getSource()==b)
      c.myPaint(); //your own paint()
  }
  public static void main(String[] args) {
    c_RTG c_RTG1 = new c_RTG();
  }
}

// Canvas class
public class myCanvas extends Canvas{

  public myCanvas() {
    this.setSize(400,400);
  }

  public void paint(Graphics g){
    g.drawString("A line of text",100,100);
  }

  //Alternative to default paint(), can be called anywhere
  public void myPaint(){
    Graphics g = this.getGraphics();
    Random r = new Random();
    g.setColor(Color.red);
    g.drawOval(r.nextInt(400),r.nextInt(400),5,5);
  }
}

Bye.
0
 

Author Comment

by:nearlyanexpert
ID: 8209638
Great! :D How would I make sure that it doesn't wipe clean the canvas and just drawn a line as opposed to it firstly draws a line then when another paint() function is called it thendraws on top of what already is there? Is this a simple parameter or something?

Thanks
NAE
0
 
LVL 4

Accepted Solution

by:
funnyveryfunny earned 180 total points
ID: 8211150
Hi,

The only way I can think of is either to keep a history of things that are currently drawn on the screen and have a method, say m() called by paint(), to redraw them all back on screen. i.e

paint(){
  m();
}  

and if I'm not wrong the default repaint() calls paint(). So this is one way of retaining yr drawing on screen but can be tricky.

Alternatively, you can use BufferedImage (also known as Double Buffering) to simutaneously draw a carbon-copy of what on the screen into the BufferedImage then use paint() to draw the image.

Here is another version myCanvas that uses BufferedImage.

import java.awt.image.BufferedImage;

public class myCanvas extends Canvas{
  BufferedImage img;
  Graphics2D goff;
  public myCanvas() {
    this.setSize(400,400);
    img = new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
    goff = img.createGraphics();
    goff.setBackground(Color.white);
    goff.clearRect(0,0,400,400);
  }

  public void paint(Graphics g){
    g.drawImage((Image)img,0,0,this);
  }

  public void myPaint(){
    Graphics g = this.getGraphics();
    Random r = new Random();
    g.setColor(Color.red);
    goff.setColor(Color.red);
    //Simultaneous drawing
    goff.drawOval(r.nextInt(400),r.nextInt(400),5,5);
    g.drawOval(r.nextInt(400),r.nextInt(400),5,5);
  }
}

Bye
0
 

Author Comment

by:nearlyanexpert
ID: 8211474
Thanks for your time and efforts! :D
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 8212051
That's alright, cheers.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question