Results 1 to 8 of 8
Thread: Unable to draw buffered image
- 08-06-2008, 01:41 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
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:
Then in jButtonActionPerfrormed function I have this line: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); }
but nothing happens when I click the JButton.Please helpJava Code:ImageDrawingComponent temp=new ImageDrawingComponent();
- 08-06-2008, 02:00 PM #2
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.
- 08-06-2008, 02:15 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
I'm using NetBeans to create GUI.It does it automatically.
- 08-06-2008, 02:23 PM #4
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().
- 08-07-2008, 11:27 AM #5
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
This is the complete code for the form:
The code for the ImageDrawingComponent is already posted.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; }
- 08-07-2008, 02:17 PM #6
The above doesn't compile
descs is NOT defined.return descs;
Where is the ImageDrawingComponent object add() to the GUI?
Just creating an object doesn't do anything to display it.
- 08-08-2008, 09:04 AM #7
Member
- Join Date
- Aug 2008
- Posts
- 4
- Rep Power
- 0
I've added these changes in jButton1ActionPerformed function :
but still nothing.jScrollPane is automatically added by NetBeans to jFrame object(I think)Java Code:ImageDrawingComponent temp=new ImageDrawingComponent(); jScrollPane1.add(temp); jScrollPane1.setVisible(true); jScrollPane1.repaint();
- 08-08-2008, 03:49 PM #8
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.
Look at the code and see what the IDE does.automatically added by NetBeans to jFrame
Similar Threads
-
Unable to Run Servlet
By basark in forum Java ServletReplies: 4Last Post: 07-11-2008, 08:08 AM -
FileReader / Buffered Reader
By sepaht in forum New To JavaReplies: 9Last Post: 07-10-2008, 08:05 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
how to draw an image inside of jscrollpane
By paty in forum Java AppletsReplies: 1Last Post: 07-24-2007, 12:44 AM


LinkBack URL
About LinkBacks

Bookmarks