?
Solved

Draw image *and* lines ...

Posted on 2005-04-03
3
Medium Priority
?
336 Views
Last Modified: 2012-05-05
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
Comment
Question by:InteractiveMind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13691857
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
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13691926
Thank you very much. :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13691932
:-)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question