LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem with paint() in java (https://www.linuxquestions.org/questions/programming-9/problem-with-paint-in-java-947434/)

lemon09 05-29-2012 01:24 PM

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

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
}


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
}

please do help me!!!

ntubski 06-02-2012 11:07 AM

Quote:

Originally Posted by lemon09 (Post 4690504)
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().

Code:

    public void paint(Graphics g)        {
                ...
                                        img = ImageIO.read(new File("pic.jpg"));
                ...
                        //set panel size according to the image
                        this.setSize(image.getWidth(this) , image.getHeight(this));
          }


I'm not sure what you mean by "implement methods declared in paint()", and it's kind of hard to follow what's happening with the automatically generated code. As a rule, you shouldn't be creating an Image object every time paint() is called, and you definitely shouldn't be resizing a component from within its paint() method!


All times are GMT -5. The time now is 06:07 AM.