Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5040

Re: How do I paint on an existing Panel?

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.gui
Subject Re: How do I paint on an existing Panel?
Date 2012-02-17 17:04 -0800
Organization A noiseless patient Spider
Message-ID <jhmte3$1tq$2@dont-email.me> (permalink)
References <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> <4f3d6f9c$0$2500$da0feed9@news.zen.co.uk> <jhju7b$f12$1@dont-email.me> <4f3e977d$0$2966$fa0fcedb@news.zen.co.uk>

Show all headers | View raw


On 2/17/2012 10:07 AM, A B wrote:
> "Knute Johnson" <nospam@rabbitbrush.frazmtn.com> wrote on 16th February:
>> On 2/16/2012 1:05 PM, A B wrote:
>>> Sorry, I'm stumped, again. I really have been trying to sort it out
>>> myself, but no luck. I've done it by adapting Knute Johnson's code
>>> (which works fine in itself). Mine now defines a line and calls
>>> repaint() as nice as you like, but there's no answer.
>>>
>>> I've chopped the code back to just the bits directly involved with the
>>> drawing and sprinkled debugging statements everywhere, which established
>>> that the bit that calls paintComponent() (via repaint() - that's right
>>> isn't it?) is firing but paintComponent() itself isn't. Here's what's
>>> left, if you're interested. Sorry if anyone finds it hard to read, I
>>> don't know how you like it formatted.
>>>
>>> ------------------------
>>> import java.awt.*;
>>> import java.awt.event.*;
>>> import java.awt.geom.*;
>>> import java.util.*;
>>> import javax.swing.*;
>>>
>>> public class Vectorine extends JFrame implements MouseListener
>>> {
>>> private static final long serialVersionUID = 159L;
>>> // List to contain all the lines generated
>>> private final java.util.List<ColoredLine> lineList = new
>>> java.util.ArrayList<ColoredLine>();
>>>
>>> public static void main() {Vectorine v = new Vectorine();}
>>>
>>> public Vectorine()
>>> {
>>> super("Vectorine");
>>> setSize(200, 200);
>>> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>>> setVisible(true);
>>>
>>> // Set up window's initial contents
>>> Container contentArea = getContentPane();
>>> FlowLayout layout = new FlowLayout();
>>> contentArea.setLayout(layout);
>>> JPanel panelA = new JPanel();
>>> panelA.addMouseListener(this);
>>> contentArea.add(panelA);
>>> setContentPane(contentArea);
>>>
>>> System.out.println("If you click anywhere in the box, 3 random lines
>>> should appear in it.");
>>> }
>>>
>>> public void paintComponent(Graphics g2d)
>>> {
>>> Graphics2D g = (Graphics2D)g2d;
>>> System.out.println("Painting...");
>>> for (ColoredLine hand : lineList)
>>> {
>>> System.out.println("Drawing line...");
>>> BasicStroke pen = new BasicStroke(hand.getThickness());
>>> g.setStroke(pen);
>>> g.setColor(hand.getColor());
>>> g.draw(hand);
>>> }
>>> }
>>>
>>> public void mouseClicked(MouseEvent event)
>>> {
>>> System.out.println("Mouse clicked");
>>> double xcoord = 0, ycoord = 0;
>>> for (int count=0; count<3; count++)
>>> {
>>> xcoord = 100 * Math.random();
>>> ycoord = 100 * Math.random();
>>> System.out.println("xcoord="+xcoord+", ycoord="+ycoord);
>>> ColoredLine hand = new ColoredLine(0F, 0F, (float)xcoord, (float)ycoord,
>>> Color.red, 2);
>>> lineList.add(hand);
>>> System.out.println("Calling repaint()...");
>>> repaint();
>>> }
>>> }
>>>
>>> /** Blank methods to keep MouseListener happy. */
>>> public void mousePressed(MouseEvent event) {}
>>> public void mouseReleased(MouseEvent event) {}
>>> public void mouseEntered(MouseEvent event) {}
>>> public void mouseExited(MouseEvent event) {}
>>> }
>>>
>>> /** The actual lines drawn are instances of the ColoredLine class. */
>>> class ColoredLine extends Line2D.Double
>>> { private static final long serialVersionUID = 149L;
>>> private final Color color;
>>> private final int thickness;
>>> public ColoredLine(double x,double y,double w,double h,Color color,int
>>> thickness)
>>> {
>>> super(x,y,w,h);
>>> this.color = color;
>>> this.thickness = thickness;
>>> }
>>>
>>> public Color getColor() {return color;}
>>> public int getThickness() {return thickness;}
>>> }
>>>
>>
>> You need to follow the example I gave you a little closer. I would
>> extend JPanel rather than JFrame. You can draw on the JPanel. I would
>> put the MouseListener into the JPanel rather than implementing it on
>> the JPanel. That you can do with a MouseAdapter and you don't have to
>> create all of the methods. You need to change the order in which you
>> set up your GUI. You do not want to make it visible until you have
>> created all of the part. You also need to wrap all Swing GUI creation
>> code in EventQueue.invokeLater() so that it will be created on the
>> Event Dispatch Thread. The example I gave you shows that.
>>
>> Note also that you rarely need the ContentPane of a JFrame anymore.
>> JFrame.add() has been modified to add the Component to the JFrame's
>> ContentPane.
>
> Thanks very much. I'll try that. Actually I thought I had done it in the
> JPanel, only I can't have, because I cut out the JPanel when I was
> simplifying, and here the MouseListener still is. Daft. I didn't realise
> the EventQueue stuff was necessary to the drawing routine, I think I
> thought it was for something else.

All GUI creation and almost all method calls to the Swing components 
must be done on the Event Dispatch thread.  The EventQueue.invokeLater() 
is used to cause that code to be run on the EDT.  After you do a few of 
these you'll get the hang of that.  It's really not difficult but the 
GUI may not work correctly if it isn't created on the EDT.

-- 

Knute Johnson

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-01-18 16:55 +0000
  Re: How do I paint on an existing Panel? Knute Johnson <nospam@knutejohnson.com> - 2012-01-18 09:29 -0800
    Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-01-18 18:12 +0000
      Re: How do I paint on an existing Panel? Knute Johnson <nospam@knutejohnson.com> - 2012-01-18 15:00 -0800
  Re: How do I paint on an existing Panel? markspace <-@.> - 2012-01-18 10:55 -0800
  Re: How do I paint on an existing Panel? Roedy Green <see_website@mindprod.com.invalid> - 2012-01-20 19:54 -0800
  Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-01-30 17:59 +0000
  Re: How do I paint on an existing Panel? Neil Morris <neil.morris4@googlemail.com> - 2012-02-04 20:58 +0000
    Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-02-05 01:01 -0800
    Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-10 12:10 +0000
    Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-16 20:55 +0000
      Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-02-16 13:04 -0800
        Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-16 21:33 +0000
  Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-16 21:05 +0000
    Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-02-16 13:28 -0800
      Re: How do I paint on an existing Panel? "John B. Matthews" <nospam@nospam.invalid> - 2012-02-16 21:18 -0500
      Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-17 18:20 +0000
        Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-02-17 10:45 -0800
        Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-02-17 10:49 -0800
          Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-19 19:31 +0000
            Re: How do I paint on an existing Panel? Lew <noone@lewscanon.com> - 2012-02-19 14:41 -0800
              Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-03-04 16:51 +0000
                Re: How do I paint on an existing Panel? Jeff Higgins <jeff@invalid.invalid> - 2012-03-04 12:36 -0500
                Re: How do I paint on an existing Panel? Lew <noone@lewscanon.com> - 2012-03-04 10:47 -0800
                Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-03-14 17:39 +0000
                Re: How do I paint on an existing Panel? Lew <lewbloch@gmail.com> - 2012-03-14 11:07 -0700
                Re: How do I paint on an existing Panel? "John B. Matthews" <nospam@nospam.invalid> - 2012-03-14 23:01 -0400
                Re: How do I paint on an existing Panel? markspace <-@.> - 2012-03-15 09:30 -0700
        Re: How do I paint on an existing Panel? Knute Johnson <nospam@knutejohnson.com> - 2012-02-17 17:01 -0800
    Re: How do I paint on an existing Panel? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2012-02-16 13:59 -0800
      Re: How do I paint on an existing Panel? "A B" <@bleBaker.uk> - 2012-02-17 18:07 +0000
        Re: How do I paint on an existing Panel? Knute Johnson <nospam@knutejohnson.com> - 2012-02-17 17:04 -0800
        Re: How do I paint on an existing Panel? Knute Johnson <nospam@knutejohnson.com> - 2012-02-17 17:08 -0800

csiph-web