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
  • 121,568
  • 97
  • 310
  • 388
James MV
  • 8,569
  • 17
  • 65
  • 96
  • 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,971
  • 9
  • 68
  • 98
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
  • 168,117
  • 40
  • 217
  • 433