Results 1 to 3 of 3
- 08-23-2011, 12:33 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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.
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: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."); } }
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.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 }
A zip containing a Netbeans project which demonstrates this problem, can be downloaded here.Last edited by Joe88; 08-23-2011 at 11:05 PM.
-
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 05:34 AM.
- 08-23-2011, 11:04 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
Thanks for replying and telling me how I can improve my rubbish Image Canvas code.
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.Displaying an AWT component, a Canvas, in a JFrame makes little sense. Why do you feel you must do this?
Similar Threads
-
Call paint method from another class using actionevent
By chidurihan in forum AWT / SwingReplies: 3Last Post: 03-14-2011, 09:45 AM -
Paint(Graphics g) method is not called on its run.
By vsanandan in forum Java 2DReplies: 5Last Post: 10-22-2010, 01:55 PM -
Client freezes when method is called
By chyrl in forum Advanced JavaReplies: 9Last Post: 07-26-2010, 09:14 PM -
Why the paint() method is called two times ?
By supremo in forum New To JavaReplies: 4Last Post: 06-03-2010, 06:21 PM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks