Images In Java

Hi,

Ive got a JLabel at the moment that has an image in it (through the icon functionaility)

Now currently the image is large and the panel epands to fit that image.

Ive got this in a split pane, which is of set size, but the panes expand if the user drags the divider but not the window size.

What i want is for the image to fill the pane within the split pane (by resizing not cropping) and then resize as I expand the panel by adjusting the dividers.

Pleas ask if its not clear, its hard to put this question across...

Many Thanks For Any help

Cheers

Alex

[613 byte] By [alexcurtisa] at [2007-11-26 18:49:35]
# 1

Write your own image component.

class ImageComponent extends JComponent {

Image image = null;

public ImageComponent(Image image) {

this.image = image;

setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

}

}

Rodney_McKaya at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

Ok i took your concepts and formed my own:

class ImageCard extends JComponent {

public ImageCard(Image image, JComponent holder) {

this.image = image;

this.holder = holder;

}

protected void paintComponent(Graphics g) {

//Get Holder Size and set to component size

this.setSize(holder.getSize());

super.paintComponent(g);

g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

}

//Var

private JComponent holder;

private Image image;

}

However im unfamiliar with how drawing works in Java. How do I define what within the frame to draw onto. I want to draw onto a JPanel as that resizes when the splitpane resizes allowing me to redraw the image.

alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 3
Just add the component I gave you to the JPanel.It will resize automatically when you resize the split pane.Don't set the size of the component in paintComponent.
Rodney_McKaya at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 4
Ok,but in your code my IDE showed this line in error:setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));btw what is it actually
alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 5
Setting the starting preferred size of the component to the size of the image.What is the error you're getting?Maybe you didn't import the Dimension or Image classes?
Rodney_McKaya at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 6

Yes you were right i hadnt included the dimensions...

Also how would I add this to my JPanel -> panel.add(new cardImage(img));...?

Also where is the getWidth and getHeight data comming from...the JPanel?

Thanks For Your Help

-Alex

Edit: Also how do you import an image for the Image attribute?..Ive only used images through swing icons before.

Message was edited by:

alexcurtis

alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 7

Ok ive tryed to add your component to my jPanel, its not working however

HERE IS MY CODE, YOUR COMPONENT IS THE SAME AS YOU HAVE DONE IT

String fileName = "C:\\Users\\alex\\Desktop\\java\\loopCardBuilder\\images\\cardImage.jpg";

//Image Cards

Toolkit toolkit = Toolkit.getDefaultToolkit();

Image testImage = toolkit.getImage(fileName);

MediaTracker mediaTracker = new MediaTracker(this);

mediaTracker.addImage(testImage, 0);

try

{

mediaTracker.waitForID(0);

}

catch (InterruptedException ie)

{

System.err.println(ie);

System.exit(1);

}

finalPanel.add(new cardImage(testImage));

The cardImage is the component for the custom Image

alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 8
Its OK,I finally got it working, seems I fell into syntax hell...lolMany Thanks-Alex(Thanks Again to the only person that helped me)BTW: I would still like the know where the getWidth and getHeight are comming from when painting the image
alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 9

When you move the divider the size of the JPanel inside you split pane changes, and then the Layout Manager of this JPanel changes the size of your image component.

getWidth and getHeight return the size of the image component.

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html]Using Layout Managers[/url]

Rodney_McKaya at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 10

Thanks For Your Help.

I think im sorted.

Please accept my duke stars with the greatest thanks!

Edit: BTW is there anyway to smooth the graphics out, they are abit aliased when down scaled from origional...

Many Thanks

Alex

Message was edited by:

alexcurtis

Message was edited by:

alexcurtis

alexcurtisa at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...
# 11
Yes.Add this before calling g.drawImage:((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
Rodney_McKaya at 2007-7-9 6:23:36 > top of Java-index,Desktop,Core GUI APIs...