James MV
James MV

Reputation: 8717

Getting started with Drawing and the 2D API

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.

Upvotes: 0

Views: 149

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168815

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.

Upvotes: 3

StanislavL
StanislavL

Reputation: 57381

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

Upvotes: 5

Related Questions