0

I have design issue here. There two options which sound possible to implement but I don't like neither one.

I need to provide two functionality in my JPanel.

  • draw several lines
  • draw several strings

ads

  1. Approach extend twice

If I would need only to draw lines then I can just copy-past solution from the following answer.

Draw a line in a JPanel with button click in Java

public class LinePanel extends JPanel {
...
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...

But since I need to add second functionality I would need to do something like this

public class LineStringPanel extends JPanel {

which would leave LinePanel just useless intermediate class.

  1. Create two classes LineDrawer and StringDrawer and pass JPanel as parameter to some method which would perform drawing. The problem here that I would need to call which is not recommended.

Performing Custom Painting

Graphics g = panel.getGraphics();

What do you think and can recommend?

UPD: Inside the JPanel there are numerious JLabels as child which represent units and traits.

The lines are relation between some traits of units and Strings are kind of notifications for some actions.

So I want to show String on the top(front) of child JLabels.

Also JPanel is inside JScrollPane.

Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • 3
    Paint to a `BufferedImage` and display it in a `JLabel`, as shown in [this answer](http://stackoverflow.com/a/12683632/418556). – Andrew Thompson Nov 26 '12 at 11:30
  • @Nikolay Kuznetsov please not attack, by default implements and extends is about very short examples as answers ...., Graphics g = panel.getGraphics(); is about printing to the File, snapshot, that expired (internally) on 1st event from related APIs, please whats your goal – mKorbel Nov 26 '12 at 11:33
  • @AndrewThompson, this JPanel should have many child Components - JLabels with ImageIcons. The lines and strings should be the most front, to be shown above JLabels. Do you think it would worK? – Nikolay Kuznetsov Nov 26 '12 at 11:36
  • @mKorbel, I have added update. Does it make any clarification? – Nikolay Kuznetsov Nov 26 '12 at 11:44
  • @NikolayKuznetsov: Are you looking for something like [this](http://stackoverflow.com/a/11944233/230513). – trashgod Nov 26 '12 at 11:44
  • 1
    @trashgod, yes, similar. Imagine the JLabel has already JLabels where I load icons of different shapes. Now I want to draw lines and a title on every shape. Since JPanel is inside JScrollPane, titles and line should stay if I scroll. – Nikolay Kuznetsov Nov 26 '12 at 11:49
  • I don't know how is possible, bur I think that @Andrew Thompson has a new battery in his magic globe, basically you can to overlay one JLabel with another, including Graphics came from BufferedImage and/or with transparency depends of – mKorbel Nov 26 '12 at 11:49
  • 1
    or another way is showed in linked code by @trashgod, have to decide – mKorbel Nov 26 '12 at 11:51
  • @mKorbel, the problem with BufferedImage solution is that JPanel already has many other childs. I don't know how I can easily overlay a separate transparent front image at JPanel. – Nikolay Kuznetsov Nov 27 '12 at 05:54

1 Answers1

4

If you need to draw strings, you can do so directly in the paintComponent method (and assuming you don't need anything too fancy, it's pretty easy):

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...
    g.drawLine(p1.x, p1.y, p2.x, p2.y);
    ...
    g.setColor(Color.black);
    g.drawString("My Text", 0, 20);
}

Then you could use your trashgod's solution with hardly any changes.

Remember that the coordinates are of the baseline of the text - so if you used 0,0 it wouldn't display on screen.

Nick Rippe
  • 6,465
  • 14
  • 30
  • 1
    Exactly; see also [*Creating labels along with drawlines*](http://stackoverflow.com/q/5394364/230513). – trashgod Nov 27 '12 at 07:18