-
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)
{
}
}
-
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
-
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.
-
you have the lines
Code:
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.
-
new size: width:10 height:10
I know, for some reason somewhere it gets converted to 10 x 10.
-
your problem is the panel for the display:
Code:
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:
Code:
class Display extends JPanel{
public Display(){
super();
setPreferredSize(new Dimension(500, 500));
}
...
}
or by passing the cnstructor the needed size.
perhaps this will help.
-
Still no joy
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!
-
well, i had a similar problem which i couldn't solve. but try the following attribute as well: setMinimumSize
-
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
-
object of display? you shurely used jcomponent, or what was it?
-
Sorry, used the wrong terminology. I used
private ImagePanel disply1 = new ImagePanel();
display1.showImage(image)
display1.setPreferredSize(imgRows,imgCols).
That seemed to do it.
Similar Threads
-
By angkerzzone in forum Java
Replies: 0
Last Post: 03-23-2006, 09:40 PM
-
By Scorpion_1984 in forum Java
Replies: 1
Last Post: 05-31-2005, 04:56 PM
-
Replies: 0
Last Post: 10-30-2002, 04:39 AM
-
By Shravan in forum VB Classic
Replies: 5
Last Post: 05-24-2002, 05:10 AM
-
By Michael Shutt in forum XML
Replies: 0
Last Post: 06-26-2001, 11:51 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks