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


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

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-01-18 15:00 -0800
Organization A noiseless patient Spider
Message-ID <jf7itq$t0k$2@dont-email.me> (permalink)
References <4f16f997$0$2957$fa0fcedb@news.zen.co.uk> <jf6vhm$4df$1@dont-email.me> <4f170b8c$0$2477$db0fefd9@news.zen.co.uk>

Show all headers | View raw


On 1/18/2012 10:12 AM, A B wrote:
> "Knute Johnson" <nospam@knutejohnson.com> wrote in message
> news:jf6vhm$4df$1@dont-email.me...
>> On 1/18/2012 8:55 AM, A B wrote:
>>> I expect this is a really obvious question; I must get round to getting
>>> a bigger Java book! Part of my program starts by displaying a JPanel,
>>> with an image put on it using paintComponent(). How do I then draw some
>>> shapes over the top of that image when a particular button is clicked? I
>>> can't just put them in paintComponent(), because they haven't to appear
>>> at first. Would it be easier in an applet, rather than an application?
>>>
>>> I'm sure this comes up in the example programs in the JDK package, but
>>> for some mysterious reason they won't work. Wrong version of IE,
>>> probably; have to look into that.
>>>
>>
>> Put all your drawing code in the paintComponent() method. One idea,
>> you could stash your shapes in a List and in the paintComponent()
>> method, retrieve and paint them. Press a button put a shape in the
>> List, call repaint().
>
> I'm not quite sure what you mean. If the drawing code was in
> paintComponent(), what's to stop it executing when the JPanel first
> appears? And what do Lists do, they aren't in my book? Sorry to be
> gormless.

I had to look up gormless.  You must be in the UK?  You need to read the 
docs for java.util.List.  To answer your question, you don't paint it 
unless it needs to be painted.  There are numerous ways to do that, with 
just two examples in the code below.

You could also look at the code for my Asteroids game, 
http://rabbitbrush.frazmtn.com/asteroids.html.  Lots of shapes in there.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;

public class test extends JPanel implements ActionListener {
     private final Random random =
      new Random(System.currentTimeMillis());
     private BufferedImage image;
     private final java.util.List<ColoredShape> shapeList =
      new java.util.ArrayList<ColoredShape>();

     public test() {
         try {
             URL url =
              new URL("http://rabbitbrush.frazmtn.com/kittens.jpg");
             image = ImageIO.read(url);
             setPreferredSize(new Dimension(
              image.getWidth(),image.getHeight()));
         } catch (MalformedURLException murle) {
              // you need to catch these
         } catch (IOException ioe) {
         }
     }

     public void actionPerformed(ActionEvent ae) {
         double w = random.nextDouble() * getWidth();
         double h = random.nextDouble() * getHeight();
         double x = random.nextDouble() * getWidth() -
          0.5 * getWidth();
         double y = random.nextDouble() * getHeight() -
          0.5 * getHeight();
         Color color = new Color(
          random.nextInt(256),random.nextInt(256),random.nextInt(256));
         ColoredShape shape = new ColoredShape(x,y,w,h,color);
         shapeList.add(shape);
         repaint();
     }

     // do all of your drawing in the overridden method paintComponent()
     public void paintComponent(Graphics g2d) {
         Graphics2D g = (Graphics2D)g2d;

         if (image != null)
             g.drawImage(image,0,0,null);

         for (ColoredShape shape : shapeList) {
             g.setColor(shape.getColor());
             g.fill(shape);
         }
     }

     class ColoredShape extends Ellipse2D.Double {
         private final Color color;

         public ColoredShape(double x,double y,double w,double h,
          Color color) {
             super(x,y,w,h);
             this.color = color;
         }

         public Color getColor() {
             return color;
         }
     }

     public static void main(String[] args) {
         // create GUI on EDT (event dispatch thread)
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 JButton b = new JButton("Add Shape");
                 b.addActionListener(t);
                 f.add(b,BorderLayout.SOUTH);
                 f.pack();
                 f.setVisible(true);
             }
         });
     }
}

-- 

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