Results 1 to 8 of 8
  1. #1
    pedjasmek is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default Unable to draw buffered image

    I have a simple application where I try to draw an image after button clicked.First I use this class:
    Java Code:
    public class ImageDrawingComponent extends Component{
       
        
        int opIndex;
        public BufferedImage bi;
        int w, h;
        
       
        public ImageDrawingComponent() {
            try {
                File slika=new File("jedinstvo.jpg");
                bi = ImageIO.read(slika);
                w = bi.getWidth(null);
                h = bi.getHeight(null);
                if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
                    BufferedImage bi2 =
                        new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                    Graphics big = bi2.getGraphics();
                    big.drawImage(bi2, 0, 0, null);
                    bi = bi2;
                   
                }
            } catch (IOException e) {
                System.out.println("Slika nije u?itana!");
                System.exit(1);
            }
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(w, h);
        }
    
        static String[] getDescriptions() {
            return descs;
        }
    
        void setOpIndex(int i) {
            opIndex = i;
        }
        
        public void paint(Graphics g) {
        //super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
    
           
                g2.drawImage(bi, 0, 0, null);
           
        }
    Then in jButtonActionPerfrormed function I have this line:
    Java Code:
    ImageDrawingComponent temp=new ImageDrawingComponent();
    but nothing happens when I click the JButton.Please help

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    Where do you add that component to a container so that it will show in your GUI. Just creating a component will NOT cause it to be visible.

  3. #3
    pedjasmek is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    I'm using NetBeans to create GUI.It does it automatically.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    If its done automatically, then it should work.
    Otherwise look at the code and see what's wrong. You haven't posted the code, so no one can tell what is happening.

    To get the JVM to call your paint()/paintComponent() method, you call repaint().

  5. #5
    pedjasmek is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    This is the complete code for the form:
    Java Code:
    /*
     * Slika.java
     *
     * Created on July 24, 2008, 11:26 AM
     */
    
    package slika;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JComboBox;
    
    /**
     *
     * @author  Pedja
     */
    public class Slika extends javax.swing.JFrame {
        
        /** Creates new form Slika */
        public Slika() {
            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.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    formMouseClicked(evt);
                }
            });
    
            jButton1.setText("OK");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(230, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(227, 227, 227))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(414, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(20, 20, 20))
            );
            pack();
        }// </editor-fold>                        
    
        private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
    // TODO add your handling code here:
            System.out.println(evt.getX());
        }                                 
    
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here
           
        ImageDrawingComponent temp=new ImageDrawingComponent();
            
        }                                        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                   
                    new Slika().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        // End of variables declaration                   
    
        private BufferedImage bi;
        
    }
    The code for the ImageDrawingComponent is already posted.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    The above doesn't compile
    return descs;
    descs is NOT defined.
    Where is the ImageDrawingComponent object add() to the GUI?
    Just creating an object doesn't do anything to display it.

  7. #7
    pedjasmek is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    I've added these changes in jButton1ActionPerformed function :
    Java Code:
    ImageDrawingComponent temp=new ImageDrawingComponent();
         jScrollPane1.add(temp);
         jScrollPane1.setVisible(true);
         jScrollPane1.repaint();
    but still nothing.jScrollPane is automatically added by NetBeans to jFrame object(I think)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    I suggest you try a simpler project where you add your component to a container and get that to work, before trying to use an IDE to build a more complicated program. Get it to display the picture and then you can move the class to your IDE project.
    automatically added by NetBeans to jFrame
    Look at the code and see what the IDE does.

Similar Threads

  1. Unable to Run Servlet
    By basark in forum Java Servlet
    Replies: 4
    Last Post: 07-11-2008, 08:08 AM
  2. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 08:05 PM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. how to draw an image inside of jscrollpane
    By paty in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 12:44 AM

Posting Permissions

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