Hi, I'm developing a GUI with several forms, one of which contains a jLabel that displays a JPEG image of a graph as it's icon. How do I go about plotting a simple 2d curve on that image from a set of data? I know of drawLine etc, but how can I do that on a jLabel?
Many thanks!

Dani AI

Generated

Summary and a safe, practical path forward (building on , and others)

The easiest, most maintainable approaches are either (A) subclass a Swing component and draw the image plus the curve in its paintComponent method, or (B) draw into a BufferedImage and use that image as the JLabel icon. ’s compile error came from trying to use extends inside a method and from not matching the Swing override signature; painting in Swing is done by overriding protected void paintComponent(Graphics) and calling the superclass first so Swing can clear and paint the icon/text.

Concrete checklist and pitfalls to avoid

  • Use a custom component (JPanel or a JLabel subclass) and override paintComponent; call super.paintComponent(g) first.
  • Cast to Graphics2D, enable antialiasing, and dispose the created Graphics copy when done.
  • Map data coordinates to pixel coordinates: compute scale factors from data range to available width/height and invert Y (screen Y grows downward). ’s suggestion to connect sample points with short lines is exactly how to plot arbitrary datasets; for smoother curves consider GeneralPath or cubic interpolation if needed.
  • If you want to preserve the original JPEG, copy it into a BufferedImage before drawing overlays; do not mutate shared image objects. Setting a new ImageIcon on the JLabel after drawing is simple and thread-safe if done on the EDT.

Minimal example pattern (illustrative)

class PlotPanel extends JPanel {
  BufferedImage bg;
  double[] x,y; // data arrays
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    if (bg != null) g2.drawImage(bg,0,0,getWidth(),getHeight(),null);
    // map data->pixels, then draw lines between successive points
    g2.dispose();
  }
}

Extra notes: update UI only on the Event Dispatch Thread (use SwingUtilities.invokeLater), precompute scaled pixel positions for large datasets, and keep painting code fast (pre-render static parts into a BufferedImage). This covers the common, robust ways to draw 2D plots over an image in Swing.

Recommended Answers

All 15 Replies

use paintComponent(Graphics g)

use paintComponent(Graphics g)

huh? how does that help, just curious?

He said he wanted to draw some graphics on a JLabel, well that's how. Maybe you should think a little harder before posting stupid replys. Besides, if you didn't even know what I was talking about, why did you even post? It was obvious that helped...Now stop taking away from other people's posts.

He said he wanted to draw some graphics on a JLabel, well that's how. Maybe you should think a little harder before posting stupid replys. Besides, if you didn't even know what I was talking about, why did you even post? It was obvious that helped...Now stop taking away from other people's posts.

i was just trying to learn something :sad:

i don't know what he means my JLabel...i'd like to know

i don't know how that one line of code you gave him answers his question

it looked like only part of an answer...

ohhh Sorry dude, I thought you were like trying to insult my intelligence or something....Dang, I hate it when I do that...Really, I am sorry for being an a$$...please forgive me...

Anyways,

JLabel, is a component in swing...It's like any other label, in any other programming language..All it does is display information, and in matt's case, he's wanting to draw on top of the component...So, in order to do that, he has to override the paintComponent method...And no, it doesn't fully answer his question, it answers only part of it...I'll try answering the rest of it later...

Again, I'm really sorry..I thought you were trying to say that I didn't know what I was talking about! sorry :o

Are you talking about not answering the curve part? Could you just drwa multiple lines making it appear to be a curve only being made up of line segments.

ohhh Sorry dude, I thought you were like trying to insult my intelligence or something....Dang, I hate it when I do that...Really, I am sorry for being an a$$...please forgive me...

Anyways,

JLabel, is a component in swing...It's like any other label, in any other programming language..All it does is display information, and in matt's case, he's wanting to draw on top of the component...So, in order to do that, he has to override the paintComponent method...And no, it doesn't fully answer his question, it answers only part of it...I'll try answering the rest of it later...

Again, I'm really sorry..I thought you were trying to say that I didn't know what I was talking about! sorry :o

its no biggie, appreciate the information

i still don't have a complete grasp of what it is you guys are trying to do

but i have more to go by now, i need to look up JLabel, Swing, and basics of plotting 2 dimensional curves in java

Matt,
i was reading up on Java "Java for Dummies" a few weeks back and it spoke on using drawline which was some type of an object in one of java's built in libraries

from what i remeber it basically spoke on how "drawline" illustrates the grid of an applet by dragging the mouse from coordinates (x, y) and droping it to (x2, y2).

Is that what you mean my drawline (you'll have to excuse me i don't even own a computer, i just keep read this stuff until i can afford one)?

You can draw an arc in java, I guess that would be the same thing as a curve:

Somthing like... Graphics.drawArc(//code);

Are you talking about not answering the curve part? Could you just drwa multiple lines making it appear to be a curve only being made up of line segments.

That's probably not a bad idea, although I would check out the drawArc() method first, and if that didn't work for him, then doing what you said certainly would.

(you'll have to excuse me i don't even own a computer, i just keep read this stuff until i can afford one)?

Wow! if that is true, you are one dedicated person.

I haven't learned the draw arc method but now I know but the draw line method would give him a broader range of design.

I've tried the follwing, but it doesn't compile - it says it can't overide the paint method:

public void paint(Graphics g) extends jLabel1 {
g.setColor(Color.green);
g.drawLine(600,335,290,290);
}

Any idea why?

Your not doing that right, but answer this: Do you have to draw on a label? I mean you wont get much of an area to draw on...A much better way, would be to create an external class that extends Canvas, and use that as your drawing surface..When you have the external class created, you would override the paintComponent method, not the paint() method. Once the external class is created, you would simply add it to the other main class. If you want to do it this way, I can help you a lot more.

I've tried the follwing, but it doesn't compile - it says it can't overide the paint method:

public void paint(Graphics g) extends jLabel1 {
g.setColor(Color.green);
g.drawLine(600,335,290,290);
}

Any idea why?

try graphing the quadratic equation y = a(x-h)² + K (i.e. parabola)

j/k

you can't do this:

public void paint(Graphics g) extends JLabel

this is a method extending?

second, you have to ovveride the paintComponent method, not paint();

I think it would be something like this:

public void paintComponent(Graphics g) {
   g.drawLine(x1,x2,y1,y1);
   JLabelName.paintComponent(g);
}

I'm not sure about the last line of the code, but It might work..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.