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: 340
  • Last Modified:

Draw image *and* lines ...

Hi,
I need to create a JLabel, with which I can draw an image as the background, but also be able to draw lines over it.
I've tried the following:

// 1
ImageIcon ii = new ImageIcon("image.jpg");
JLabel display = new JLabel(ii)
{
    public void paintComponent( Graphics g )
    {
        g.setColor(Color.RED);
        g.drawLine(0,0,100,100);
    }
};
display.setPrefferedSize(new Dimension(100,100));


// 2
JLabel display = new JLabel()
{
    public void paintComponent( Graphics g )
    {
        Image i = new ImageIcon("foobar.jpg").getImage();
        g.drawImage(i, 100, 100, this);
        g.setColor(Color.RED);
        g.drawLine(0,0,100,100);
    }
};
display.setPreferredSize(new Dimension(100,100));


Neither of them work.  :o\

I even tried to override the paintComponent() method for the ImageIcon, and draw a line that way, but the line doesn't appear.

Any ideas?

Thanks in advance,
>> IM
0
InteractiveMind
Asked:
InteractiveMind
  • 2
1 Solution
 
aozarovCommented:
Add call to super.paintComponent(g);

This worked for me:
      ImageIcon ii = new ImageIcon("/usr/share/apache/icons/linux-small.jpg");

      JLabel display = new JLabel(ii)
      {
          public void paintComponent( Graphics g )
          {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawLine(0,0,100,100);
          }
      };
0
 
InteractiveMindAuthor Commented:
Thank you very much. :)
0
 
aozarovCommented:
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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