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


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

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-05 01:01 -0800
Organization http://groups.google.com
Message-ID <31390969.1720.1328432493350.JavaMail.geo-discussion-forums@prie27> (permalink)
References <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> <xuKdncnk5PeOAbDSnZ2dnUVZ8qidnZ2d@brightview.co.uk>

Show all headers | View raw


Neil Morris wrote:
> I had the same problem! my solution is as follows
> 
> 
> /*
>   * To change this template, choose Tools | Templates
>   * and open the template in the editor.
>   */
> package imagelibrary;
> 
> //~--- JDK imports 
> ------------------------------------------------------------
> import java.awt.Dimension;
> import java.awt.Graphics;
> import java.awt.LayoutManager;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.IOException;
> import javax.imageio.ImageIO;
> import java.io.InputStream;
> import javax.swing.JPanel;
> 
> /**
>   *
>   * @author NeilLewisMorris
>   */
> public class ImagePanel extends JPanel {
> 
>      private BufferedImage Bimage = null;

Please follow the Java naming conventions ('bImage' or 'bimage', not 'Bimage').

It is not necessary to redundantly initialize member variables to 'null' (or 
the primitive defaults if the variable is primitive). This just has the result 
of setting the variable to 'null' twice.

>      private File imageFile = null;
>      private InputStream imageInputStream = null;
>      private Dimension size;
>      private double panelWidth;
>      private double panelHeight;
>      private double BimageWidth;

... naming conventions ...

>      private double BimageHeight;
>      private double scaledWidth;
>      private double scaledHeight;

'scaledWidth' and 'scaledHeight' should not be member variables, based on what 
you show in your code. They have nothing to do with instance state and should 
be relegated to local variables. Likewise with 'scaled' and several others. You 
need to analyze your scope strategy thoroughly; it isn't right.

>      private double scaled;
>      private double startX = 0;

While it's not necessary to set these values to 0 (actually 0.0) explicitly, 
some think that there's a documentary advantage to doing so. I don't see it, 
but that's me.

>      private double startY = 0;
> 
>      public ImagePanel() {
>          super();

This call to 'super()' is not needed and is potentially distracting.

>      }
> 
>      public ImagePanel(LayoutManager layout) {
>          super(layout);
>      }
> 
>      @Override
>      public void paintComponent(Graphics g) {
>          super.paintComponent(g);
>          if (Bimage != null) {
>              BimageWidth = Bimage.getWidth();
>              BimageHeight = Bimage.getHeight();
>              panelWidth = getWidth();
>              panelHeight = getHeight();
>              scaledWidth = panelWidth / BimageWidth;
>              scaledHeight = panelHeight / BimageHeight;
> 
>              if (scaledWidth > scaledHeight) {
>                  scaled = scaledHeight;
>              } else {
>                  scaled = scaledWidth;
>              }
>              scaledWidth = scaled * BimageWidth;
>              scaledHeight = scaled * BimageHeight;
>              startX = (panelWidth - scaledWidth) / 2;
>              startY = (panelHeight - scaledHeight) / 2;
>          } else {
>              scaledWidth = 0;
>              scaledHeight = 0;
>          }
>          if (Bimage != null) {


>              g.drawImage(Bimage, (int)startX, (int)startY, (int) 
> scaledWidth, (int) scaledHeight, this);
>          }
>      }
> 
>      public File getImageFile() {
>          return imageFile;
>      }
> 
>      public boolean setImageFile(File file) throws IOException {
>          imageFile = file;
>          return setImageInputStream(new FileInputStream(imageFile));
>      }
> 
>      public boolean setImageInputStream(InputStream inputstream) {
>          imageInputStream = inputstream;
>          try {
>              Bimage = ImageIO.read(imageInputStream);
>          } catch (IOException e) {
>              return false;

Logging?

>          }
>          revalidate();
>          repaint();

This doesn't seem the right place to 'revalidate()', much less 'repaint()'. 
Also, it might be a good idea to move the file action off the EDT. 

>          return true;
>      }
> }

Review best practices on variable scoping and study the Java coding 
conventions. Flouting them is jarring to code reviewers and increases the risk 
of bugs.

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