0

I'm trying to integrate some drawing functionality into my program.

I have a JLabel that has an image set on it.

I want to write a method to return my image:

public Graphics getImage(){
    Graphics g = currentImage;
    return g
}

But I don't know how to convert it from a JLabel to a graphics object. Then as a simple test I want to draw a line on this image:

public void paint(Graphics g) {  
    g.drawLine(20, 500, 700, 600);
}

Some help with getting started on this would be great.

Jonas
  • 109,920
  • 93
  • 295
  • 369
James MV
  • 8,381
  • 16
  • 63
  • 92
  • possible duplicate of [How to draw on a JLabel?](http://stackoverflow.com/questions/8466030/how-to-draw-on-a-jlabel) – trashgod Dec 12 '11 at 15:14

2 Answers2

5

Override paintComponent(Graphics g) method of JLabel and place all the drawing code there.

StanislavL
  • 56,113
  • 9
  • 64
  • 93
3

I have a JLabel that has an image set on it.

Create a copy of the image (BufferedImage image2..) and put image2 in the label.

When you need to draw, call image2.getGraphics() for a Graphics object, or image2.createGraphics() for a Graphics2D object.


See this answer for examples of creating and using images.

Community
  • 1
  • 1
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420