Results 1 to 6 of 6
Thread: problem with paint() in java
- 05-29-2012, 08:29 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
problem with paint() in java
hi,
Presently I'm facing a new problem with the paint() in java. A call to an object of JPanel which has the paint() overridden does not actually implement the methods declared in paint().
Here's the code
Java Code:package testzoom; /** * * @author Lob Kush */ public class Zoom extends javax.swing.JFrame { ImageContainer ic ; /** Creates new form Zoom */ public Zoom() { ic = new ImageContainer(); 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() { jPanel1 = new javax.swing.JPanel(); jPanel1.add(ic); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Zoom Test"); setBackground(new java.awt.Color(102, 102, 102)); jPanel1.setBackground(new java.awt.Color(255, 255, 204)); jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(3, 3, 3, 3, new java.awt.Color(0, 0, 0))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 394, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 272, Short.MAX_VALUE) ); 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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(90, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Zoom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Zoom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Zoom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Zoom.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Zoom().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel jPanel1; // End of variables declaration }
Java Code:package testzoom; import java.awt.*; import java.awt.image.*; import javax.imageio.*; import java.io.*; /** * * @author Lob Kush */ public class ImageContainer extends javax.swing.JPanel { /** Creates new form ImageContainer */ public ImageContainer() { initComponents(); //paint(this.getGraphics()); //this.repaint(); } @Override public void paint(Graphics g) { System.out.println("hello world"); BufferedImage img = null; try { img = ImageIO.read(new File("pic.jpg")); } catch(Exception e) { System.out.println("ERROR : File Not Found"); System.out.println("Error Message : "+e.getMessage()); } Graphics2D g2 = (Graphics2D)g; Image image = img.getScaledInstance(1500,1500,Image.SCALE_SMOOTH); //g2.scale(3.59,4.44); //g2.drawImage(img,0,0,null); //g2.drawImage(image,10,10,700,700,0,0,1000,1000,null); g.setColor(Color.WHITE); g.drawString("hello",30,40); //set panel size according to the image this.setSize(image.getWidth(this) , image.getHeight(this)); } /** 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() { jButton1 = new javax.swing.JButton(); setBackground(new java.awt.Color(255, 204, 102)); jButton1.setText("jButton1"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(184, Short.MAX_VALUE) .addComponent(jButton1) .addGap(143, 143, 143)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(111, 111, 111) .addComponent(jButton1) .addContainerGap(166, Short.MAX_VALUE)) ); }// </editor-fold> // Variables declaration - do not modify private javax.swing.JButton jButton1; // End of variables declaration }
here I have tried to add the object of ImageContainer to a panel in Zoom...but the panel is not shown;
So can you please help me????Last edited by lemon09; 05-29-2012 at 08:31 PM.
- 05-29-2012, 09:59 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: problem with paint() in java
Perhaps my quick glance missed something, but I don't see where you add the JPanel to the content pane of the JFrame.
- 05-30-2012, 03:58 AM #3
Re: problem with paint() in java
Don't ever use getGraphics() of a component. Using repaint(0 is the correct way to go.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-30-2012, 04:00 AM #4
Re: problem with paint() in java
Cross posted
problem with paint() in java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-30-2012, 08:44 PM #5
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: problem with paint() in java
Well, let me make it a bit clear. Whenever a component is created, by default, the paint() function (which is overridden by your custom class) is called. However in my case, this particular job is an exception. I mean the overridden paint() is not called even if I use repaint(). I tried putting a comment in the paint() to which there is no output.
- 05-30-2012, 09:08 PM #6
Re: problem with paint() in java
You might get better help if you post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Many members (self included) won't wade through all that IDE generated code and inane comments.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Paint is invalid type for variable paint.
By minibronya in forum New To JavaReplies: 3Last Post: 05-25-2012, 05:52 AM -
Paint problem =P
By santa in forum New To JavaReplies: 7Last Post: 02-08-2011, 09:29 AM -
Java Paint Program problem (JPanel)
By KilKidd in forum Advanced JavaReplies: 6Last Post: 11-20-2010, 04:31 AM -
paint and repaint problem
By koddy in forum NetBeansReplies: 2Last Post: 05-21-2010, 06:43 AM -
Java Paint Method?
By leapinlizard in forum Java 2DReplies: 2Last Post: 02-11-2010, 07:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks