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


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

Re: How do I paint on an existing Panel?

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.gui
Subject Re: How do I paint on an existing Panel?
Date 2012-02-16 13:28 -0800
Organization http://groups.google.com
Message-ID <800725.801.1329427684213.JavaMail.geo-discussion-forums@pbbpk4> (permalink)
References <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> <4f3d6f9c$0$2500$da0feed9@news.zen.co.uk>

Show all headers | View raw


On Thursday, February 16, 2012 1:05:25 PM UTC-8, 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.

Note that you did not start the GUI on the Event Dispatch Thread (EDT). I don't 
know if this causes your problem, but it's a mistake.

> 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.

We like it formatted according to the well-established Java Coding Conventions 
or a slight variation thereof.

> ------------------------
> import java.awt.*;
> import java.awt.event.*;
> import java.awt.geom.*;
> import java.util.*;
> import javax.swing.*;
> 
> public class Vectorine extends JFrame implements MouseListener

"Favor composition over inheritance".
http://java.sun.com/docs/books/effective/

> {
> private static final long serialVersionUID = 159L;

indent 2 (or 3 or 4) spaces per level, no TABs.

Why did you pick '159L'? Just use '1L'. What is up with 159?

> // List to contain all the lines generated
> private final java.util.List<ColoredLine> lineList = new 
> java.util.ArrayList<ColoredLine>();

Why do you import java.util.* and still use FQNs?

> public static void main() {Vectorine v = new Vectorine();}

Start GUIs on the EDT! Golly!

> public Vectorine()
> {
> super("Vectorine");
> setSize(200, 200);

Use 'setPreferredSize()'.

> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You forgot 'pack()'.

> 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.");

Don't use 'System.out.println()' in a GUI, nor for logging.

> }
> 
> public void paintComponent(Graphics g2d)
> {
>   Graphics2D g = (Graphics2D)g2d;

Your naming is backwards.

>   System.out.println("Painting...");

Don't use 'System.out.println()' in a GUI, nor for logging.

>   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);
>   }
> }

Shouldn't you be painting these lines into a component contained within the 
JFrame and not the JFrame itself?
> 
> 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();

I'm less familiar with idioms that interfere with the repainting loops, but 
shouldn't you be invalidating? Do you actually have to repaint?

It's hard to reason about this because the other errors complicate the 
analysis.

>    }
> }
> 
> /** Blank methods to keep MouseListener happy. */

You could have just inherited the listener from 
<http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseAdapter.html>

> 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;}
> }

I cannot be sure without putting far too much time into it, but I think the 
problem stems from a combination of factors:
- not running the GUI on the EDT;
- drawing on the JFrame itself rather than a contained component, leading to 
  components drawing themselves over your graphic;
- failure to pack();
- omission of 'setPreferredSize()';

I promise that I have not analyzed your program in the depth needed to properly 
suss out your problem. I do know that it helps to eliminate even potentially 
complicating factors, particularly the failure to use the EDT. Most of those 
factors won't impinge on the immediate question, but the effort to fix bad code 
is never wasted.

-- 
Lew

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