Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

JPanel + Image Problem

Hi all,
I'm "trying" to write a Image processing program for my project in College and I'm having trouble getting the Image to display properly in my JPanel. It displays the image but for some reason only at 10pix instead of original size.
Am using SDK 1.4
console output
********************
In display method // goes into display method
new size: width:10 height:10 // for some reason resizes image
Pixel grab successful
image width: 200
image hieght: 200
*******************
I have higlighted all relative code, please please advise.

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.image.*;
import javax.swing.border.*;
/
public class MainClassGUI extends JFrame implements ActionListener{

/**
*
*/
private JMenuBar MB;
private JMenu fileMenu, filterMenu;
private JMenuItem open, save;

JPanel DrawPanel = new JPanel(); *****************

Image OriginalImg;*****************
Image modImg;
int imgCols;//Number of horizontal pixels*****************
int imgRows;//Number of rows of pixels*****************

int[][][] threeDPix;
int[][][] threeDPixMod;
int[] oneDPix;

ImgIntfc02 imageProcessingObject;

Display display = new Display();*****************
MediaTracker tracker;*****************

static String theProcessingClass =
"ProgramTest";
static String theImgFile = "manwthbg.jpg";*****************

public static void main(String[] args) {
MainClassGUI obj = new MainClassGUI();
}

public MainClassGUI() {
super("Main GUI");

JMenuBar MB = new JMenuBar();
fileMenu = new JMenu("File");
MB.add(fileMenu);

filterMenu = new JMenu("Filter");
MB.add(filterMenu);

setJMenuBar(MB);
open= new JMenuItem("Open",'O');
open.addActionListener(this);
fileMenu.add(open);

save= new JMenuItem("Save",'S');
save.addActionListener(this);
fileMenu.add(save);

//mainPanel.setLayout(new FlowLayout());
//getContentPane().add(mainPanel, BorderLayout.SOUTH);


OriginalImg = Toolkit.getDefaultToolkit().*****************
getImage(theImgFile);*****************


tracker = new MediaTracker(this);*****************
tracker.addImage(OriginalImg,1);*****************

try{
if(!tracker.waitForID(1,10000)){
System.out.println("Load error.");
System.exit(1);
}//end if
}catch(InterruptedException e){
e.printStackTrace();
System.exit(1);
}
if((tracker.statusAll(false)
& MediaTracker.ERRORED
& MediaTracker.ABORTED) != 0){
System.out.println(
"Load errored or aborted");
System.exit(1);
}//end if

imgCols = OriginalImg.getWidth(this);
imgRows = OriginalImg.getHeight(this);
getContentPane().setLayout(new FlowLayout());
//DrawPanel.setBackground(Color.CYAN);
DrawPanel.add(display, BorderLayout.NORTH);*****************
DrawPanel.setBorder(new LineBorder(Color.black, 2));
DrawPanel.setSize(new Dimension(imgCols,imgRows));*****************
getContentPane().add(DrawPanel, BorderLayout.CENTER);
setSize(2*imgCols,2*imgRows);
// mainPanel.add(display, BorderLayout.NORTH);
// TODO Auto-generated constructor stub
//getContentPane().setSize(imgCols ,imgRows );
setVisible(true);


oneDPix = new int[imgCols * imgRows];//1d array to recive the image data
try{
PixelGrabber pic = new PixelGrabber(
OriginalImg,0,0,imgCols,imgRows,
oneDPix,0,imgCols);
if(pic.grabPixels() &&
((pic.getStatus() &
ImageObserver.ALLBITS)
!= 0)){
threeDPix = convertToThreeDim(
oneDPix,imgCols,imgRows);
System.out.println(
"Pixel grab successful");
System.out.println("image width: " + imgCols);
System.out.println("image hieght: " + imgRows);
}
else System.out.println(
"Pixel grab not successful");
}catch(InterruptedException e){
e.printStackTrace();
}//end catch
this.setVisible(true);
}
*************************************************************************************
class Display extends JPanel{

public void paintComponent(Graphics g){
super.paintComponent(g);
Dimension size = getSize();


if (tracker.statusID(1, false) ==
MediaTracker.COMPLETE){
if((OriginalImg != null)){
g.drawImage(OriginalImg,0,0,size.width,size.height,DrawPanel);
System.out.println(
"In display method");
System.out.println(
"new size: width:" + size.width + " height:" + size.height );

//g.drawImage(modImg,0,imgRows + 1,this.getWidth(),this.getHeight(),DrawPanel);
}//end if
}//end if
}//end paint()
*************************************************************************************
}



int[][][] convertToThreeDim(
int[] oneDPix,int imgCols,int imgRows){
int[][][] data =
new int[imgRows][imgCols][4];
for(int row = 0;row < imgRows;row++){
//Extract a row of pixel data into a
// temporary array of ints
int[] aRow = new int[imgCols];
for(int col = 0; col < imgCols;col++){
int element = row * imgCols + col;
aRow[col] = oneDPix[element];
}
for(int col = 0;col < imgCols;col++){
//Alpha data
data[row][col][0] = (aRow[col] >> 24)
& 0xFF;
//Red data
data[row][col][1] = (aRow[col] >> 16)
& 0xFF;
//Green data
data[row][col][2] = (aRow[col] >> 8)
& 0xFF;
//Blue data
data[row][col][3] = (aRow[col])
& 0xFF;
}//end for loop on col
}//end for loop on row
return data;
}//end convertToThreeDim
int[] convertToOneDim(
int[][][] data,int imgCols,int imgRows){
int[] oneDPix = new int[
imgCols * imgRows * 4];
for(int row = 0,cnt = 0;row < imgRows;row++){
for(int col = 0;col < imgCols;col++){
oneDPix[cnt] = ((data[row][col][0] << 24)
& 0xFF000000)
| ((data[row][col][1] << 16)
& 0x00FF0000)
| ((data[row][col][2] << 8)
& 0x0000FF00)
| ((data[row][col][3])
& 0x000000FF);
cnt++;
}
}
return oneDPix;
}

public void actionPerformed(ActionEvent e)
{

}
}
[7870 byte] By [lsdinc] at [2007-11-11 8:27:14]
# 1 Re: JPanel + Image Problem
1. check if you are not loading a thumb of your image instead of the image itself
2. debug (print out) the size of the image, where you get it (at imgCols = OriginalImg.getWidth(this);
imgRows = OriginalImg.getHeight(this);
3. try setting the sizes of all the components you use, eg. setPreferredSize for panel and setSize for frame
graviton at 2007-11-11 22:35:24 >
# 2 Re: JPanel + Image Problem
Hi, thanks for gettin back to me.
yeah, I tried that and it only displays a part of image that is 10 x10 pix for some reason. I tried setting size in Display method and in Drawpanel but I get the same result.
>>debug (print out) the size of the image, where you get it (at imgCols = OriginalImg.getWidth(this);
imgRows = OriginalImg.getHeight(this); I get
>>imgCols just after I get it:200
imgRows just after I get it:200
I was using setPreferedsize but got same result:-( just the JPanel fit to size 200 x 200.
Me head is melted with it.
lsdinc at 2007-11-11 22:36:19 >
# 3 Re: JPanel + Image Problem
you have the lines

Dimension size = getSize();
if (tracker.statusID(1, false) ==
MediaTracker.COMPLETE){
if((OriginalImg != null)){
g.drawImage(OriginalImg,0,0,size.width,size.height ,DrawPanel);
what's printed out, when you debug size.width and size.height?
sometimes the default size of the corresponding jcomponent is set to very small.
graviton at 2007-11-11 22:37:22 >
# 4 Re: JPanel + Image Problem
new size: width:10 height:10
I know, for some reason somewhere it gets converted to 10 x 10.
lsdinc at 2007-11-11 22:38:28 >
# 5 Re: JPanel + Image Problem
your problem is the panel for the display:
class Display extends JPanel{

public void paintComponent(Graphics g){
super.paintComponent(g);
Dimension size = getSize();
this panel has, when the paintComponent() method is called,
the size 10x10, and thus g.drawImage(OriginalImg,0,0,size.width,size.height ,DrawPanel); paints only that part.
what you need is the preferredsize of exactly that component.
try to overwrite the constructor of that panel and there set the preferred size, like:
class Display extends JPanel{
public Display(){
super();
setPreferredSize(new Dimension(500, 500));
}
...
}
or by passing the cnstructor the needed size.
perhaps this will help.
graviton at 2007-11-11 22:39:32 >
# 6 Re: JPanel + Image Problem
Hey, Sorry I did not get back to you sooner, I had to take a break from this project to work on my other one.
But I'm back on it now.
I have tried setPrefered size on drawPanel, I get a panel the right size but the image is still just 10 x 10 in the panel.
I have tried hard coding it iin places too.
Grrrrrr!
lsdinc at 2007-11-11 22:40:25 >
# 7 Re: JPanel + Image Problem
well, i had a similar problem which i couldn't solve. but try the following attribute as well: setMinimumSize
graviton at 2007-11-11 22:41:34 >
# 8 Re: JPanel + Image Problem
I actualy got it goin, thanks
I created an object of display and set it up like a Jpanel and added it to Drawpanels.
Thanks
for your help.
Les
lsdinc at 2007-11-11 22:42:26 >
# 9 Re: JPanel + Image Problem
object of display? you shurely used jcomponent, or what was it?
graviton at 2007-11-11 22:43:29 >
# 10 Re: JPanel + Image Problem
Sorry, used the wrong terminology. I used
private ImagePanel disply1 = new ImagePanel();

display1.showImage(image)
display1.setPreferredSize(imgRows,imgCols).
That seemed to do it.
lsdinc at 2007-11-11 22:44:30 >