Results 1 to 3 of 3
  1. #1
    c0elh0 is offline Member
    Join Date
    Oct 2008
    Lisbon, Portugal
    Rep Power

    Question [SOLVED] why is the image being redrawn??

    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).

    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);
        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);
    Any hints on why the image is set to the default style???
    What can I do in an Applet viewer to prevent this??


  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    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.

  3. #3
    c0elh0 is offline Member
    Join Date
    Oct 2008
    Lisbon, Portugal
    Rep Power

Similar Threads

  1. How to rotate an image
    By ChipChamp in forum New To Java
    Replies: 4
    Last Post: 06-20-2012, 07:22 PM
  2. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 01:25 PM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 10:10 AM
  5. Help with larger image
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:59 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