Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Something on a canvas?

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
nearlyanexpert
Asked:
nearlyanexpert
  • 3
  • 2
1 Solution
 
funnyveryfunnyCommented:
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
 
nearlyanexpertAuthor Commented:
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
 
funnyveryfunnyCommented:
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
 
nearlyanexpertAuthor Commented:
Thanks for your time and efforts! :D
0
 
funnyveryfunnyCommented:
That's alright, cheers.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now