Results 1 to 3 of 3
  1. #1
    Joe88 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Why is the paint method in this ImageCanvas Class, being endlessly called?

    When ImageB is drawn in the paint method instead of ImageA, the paint method is endlessly called when an object from this class is shown in a JFrame.

    Java Code:
    package canvastesting;
    
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.io.File;
    import javax.imageio.ImageIO;
    
    /**
     *
     * @author joe
     */
    public class ImageCanvas extends Canvas
    {
        private Image imageA;
    
        public ImageCanvas(String url)
        {
            try
            {
                imageA = ImageIO.read(new File(url));
            }
    
            catch(Exception e){System.out.println("" + e);}
        }
    
        @Override
        public void paint(Graphics g)
        {
            Toolkit kit = Toolkit.getDefaultToolkit();
            Image imageB = kit.createImage(imageA.getSource());
            g.drawImage(imageB,0,0,this);
            
            System.out.println("paint used.");
        }
    }
    Here is the code for a JFrame extending class which uses the Image Canvas Class, all of its code has been generated by the Netbeans GUI wizard - I have only changed the declaration for the Canvas object created by the wizard.

    Java Code:
    public class Frame extends javax.swing.JFrame
    {
        /** Creates new form Frame */
        public Frame() {
            initComponents();
    
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            canvas1 = new canvastesting.ImageCanvas("/home/joe/NetBeansProjects/PaintingProblem/src/testImage.gif");
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(java.awt.Color.red);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Frame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private java.awt.Canvas canvas1;
        // End of variables declaration                   
    }
    Although the Frame Class has a Main() method, it is not yet the main class in the Netbeans project, where this ImageCanvas is being tested. The main class being used, simply provides a main() method which starts the Java program by creating a Frame object and making it visible.

    A zip containing a Netbeans project which demonstrates this problem, can be downloaded here.
    Last edited by Joe88; 08-24-2011 at 12:05 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Displaying an AWT component, a Canvas, in a JFrame makes little sense. Why do you feel you must do this? Nor should you create an Image object inside of the paint method since this type of method (including paint and paintComponent) needs to be as fast as can be. Also you never show us how you use objects of this class in your program, so it's hard to see if you're misusing it there.

    For my money, I'd use a JPanel, I'd draw in its paintComponent method, not the paint method, and I'd get my images in its constructor only (if possible). Or even simpler would be to simply display the image in a ImageIcon in a JLabel.

    My main recommendation: read the tutorials as they explain all of this.
    Last edited by Fubarable; 08-23-2011 at 06:34 AM.

  3. #3
    Joe88 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for replying and telling me how I can improve my rubbish Image Canvas code.

    Displaying an AWT component, a Canvas, in a JFrame makes little sense. Why do you feel you must do this?
    I have done this because I'm a bad programmer and I don't know much about the workings of Java's GUI System. I will read the tutorials and I will make the improvements you have suggested.

Similar Threads

  1. Call paint method from another class using actionevent
    By chidurihan in forum AWT / Swing
    Replies: 3
    Last Post: 03-14-2011, 10:45 AM
  2. Replies: 5
    Last Post: 10-22-2010, 02:55 PM
  3. Client freezes when method is called
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 07-26-2010, 10:14 PM
  4. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 07:21 PM
  5. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •