Results 1 to 3 of 3
- 10-28-2008, 06:12 PM #1
Member
- Join Date
- Oct 2008
- Location
- Lisbon, Portugal
- Posts
- 14
- Rep Power
- 0
[SOLVED] why is the image being redrawn??
Hi,
I use the code below to draw an image to a JPanel in a JApplet viewer, the image is display in the right location with the right width and height.
The problem is, when I resize the window with the mouse, the image is "redrawn" with the it's original width and height (not the size I wanted) and with the default location (center I guess).
Any hints on why the image is set to the default style???Java Code:public void displayImage(String url, JPanel panel, int w, int h){ Graphics g = panel.getGraphics(); //importante Graphics2D g2D = (Graphics2D) g; Image img = getImage(getDocumentBase(),url); ImageIcon icon = new ImageIcon(img); JLabel label = new JLabel(icon,JLabel.CENTER); panel.add(label,BorderLayout.CENTER); paintComponent(g2D,img,w,h); } protected void paintComponent(Graphics g, Image i, int w, int h) { // Center image in this component. (minus 300 for this case) System.out.println("Drawing image..."); int x = (getWidth() - i.getWidth(null))/2; int y = (getHeight() - i.getHeight(null))/2; g.drawImage(i, x-300, y, w, h, this); }
What can I do in an Applet viewer to prevent this??
c0elh0
- 10-28-2008, 06:44 PM #2
There are two way to show an image:
1 — set it in a JLabel via ImageIcon and add the JLabel to the component hierarchy of your top–level container
2 — draw it in painting method; most easily in the paintComponent method of a JPanel.
You seem to be trying to do both.
If you want to move an image around the second method is usually the way to go. You can resize images easily with either approach.
- 10-28-2008, 06:47 PM #3
Member
- Join Date
- Oct 2008
- Location
- Lisbon, Portugal
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
How to rotate an image
By ChipChamp in forum New To JavaReplies: 4Last Post: 06-20-2012, 07:22 PM -
Canvas Image popups another image (SWT)
By SpaceY in forum New To JavaReplies: 2Last Post: 11-11-2008, 01:25 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Image resizing
By alley in forum Java 2DReplies: 2Last Post: 11-13-2007, 10:10 AM -
Help with larger image
By cachi in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks