JPanel + Image Problem
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)
{
}
}

