• Post Reply Bookmark Topic Watch Topic
  • New Topic

I just get a blank panel.

 
Mal James
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am a long term on-and-off programmer but new to java graphics. I have been beating my head for a week now but I still get no image on my little grey box.
No bubbles
I know that whatever I am doing wrong is really simple but I cannot see it. Could someone please show me my error. I have stripped out everything to just leave a basic box.
the code as given produces the error on my machine.
The initComponents () was generated by netbeans 6 in the original project and I am using Java 6. All it is supposed to do is draw the BMP as an image in the box. Easy I thought......
Thanks in advance,

Mal


import java.awt.image.* ;
import java.awt.* ;
import javax.swing.* ;

//These two only for test.
import java.io.* ;
import javax.imageio.* ;

public class TestBmp2 extends javax.swing.JFrame {

bmpPanel bp ;

public TestBmp2 ( String in_fname ) {
initComponents();
bp = new bmpPanel (in_fname ) ;
getContentPane () . add ( bp ) ;
setVisible ( true ) ;
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 329, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 259, 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(61, Short.MAX_VALUE))
);
layout.setVerticalGroup(
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(30, Short.MAX_VALUE))
);

pack();
}// </editor-fold>


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestBmp2 ("C:\\temp\\fedvalTest\\Soap Bubbles.bmp") ;
}
});
}

// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration


private class bmpPanel extends JPanel {

BufferedImage bi ;

public bmpPanel ( String in_fname ) {
try {

bi = ImageIO.read( new File ( in_fname ) );
if ( bi == null ) {
System.out.println ( "Image read null.") ;
} else {
}
} catch (IOException e) {
System.out.println ( "Image read error.") ;
}
}

public void paintComponent ( Graphics in_g ) {
super . paintComponent ( in_g ) ;
Graphics2D g = (Graphics2D) in_g ;
g.drawImage(bi, 100, 100, this);
}
} // End of inner class bmpPanel
}
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mal, next time you post code, could you please surround it with code tags ( [CODE]Your Code Here[/CODE]). You can use the Code button at the top of the posting form to generate the code tags for you.


I took a look at your code and I couldn't find a reason for it to not draw the image, but I suspect that all that auto-generated layout in the initComponents() method is what is doing you in. Sure enough, I tested it on my system and using your code I got the same results. I took out that nitComponents method and things work fine. See code below. I suspect the layout would be forcing you to put the panel in a specific spot, but I don't know exactly where that spot is. My philosophy is if you don't know what the code is doing than it shouldn't be there.*

Modified Code for TestBmp2:


* If you don't know what the code is doing than it shouldn't be there.
I get in trouble a lot for deleting my co-worker's code. So maybe you should take this advice in moderation :-)
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:. . . nitComponents method
What a splendid misprint! Agree; you can't understand the auto-generated code from NetBeans.
Welcome to JavaRanch, Mal
I think this thread would sit better on the Swing forum. Moving.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:My philosophy is if you don't know what the code is doing than it shouldn't be there.*

* If you don't know what the code is doing than it shouldn't be there.
I get in trouble a lot for deleting my co-worker's code. So maybe you should take this advice in moderation :-)


Or perhaps you should modify it: if you don't know what your co-worker is doing, perhaps he/she shouldn't be there. Thanks for the chuckle!

To the original poster, I agree with Steve in that I suspect that your layout is at fault here. I advise you not to use GroupLayout when you want to add components to another JPanel or JComponent. Instead use one of the other more human-friendly layouts. You can learn about them here: Laying Out Components Within a Container

Much luck!
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I don't think bmp files are supported. Try a jpg, gif or png.

 
Mal James
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! great, Thanks to all who responded - especially Steve. And I thought I liked Netbeans
I got bubbles
And I will definitely use the code tags next time.
BMPs are supported from 1.5 - I read a lot trying to make this go; Should've just asked sooner.
Thanks again,
Mal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!