5 Replies - 908 Views - Last Post: 15 June 2012 - 07:11 AM Rate Topic: -----

#1 ganessh.gothi@rediff.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

I am not getting images from folder using netbeans.

Posted 14 June 2012 - 03:15 AM

Hi,
I am new in this forum, Thanks
Now my problem is , I have created applet and other class in my application. Now in one class I want to get images in a static blog but not getting images from this class. My code is:-

package comapplet;

import java.awt.Image;
import java.awt.Toolkit;
//import java.io.File;
//import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;


public class AllCard {
    
	static List <Card>allCard=new ArrayList<Card>();
   
    static{
    	try {        
    		
    	    String suits = "shdc";
    	    String faces = "a23456789tjqk";
    	  	int i=0;
    	  	for (int suit=0; suit<suits.length(); suit++) 
    	  	{
    	  		for (int face=0; face<faces.length(); face++) 
    	  		{
    	  		  System.out.println("I is:" +i);
    	  		  Card c=new Card("cardsp/layingcard/"+ faces.charAt(face)
        	  				+ suits.charAt(suit) + ".gif",i);
    	  		  System.out.println("Image is:"+c);
    	  		  allCard.add(c);
    	  		  i++;   
    	  			
    	  		}
     
    	  	}
    	  	System.out.println("Card is: :"+allCard);
      }catch (Exception ex)
      {
    	  System.err.println("Caught Exception: " + ex.getMessage());
      // handle exception...
      }
    }

}





and my Card class is:-
package comapplet;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class Card extends DraggableComponent implements ImageObserver{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String imagePath;
    int id=0;
    protected Image image;
    private boolean autoSize = false;
    private Dimension autoSizeDimension = new Dimension(0, 0);

    public Card(String imagePath,int id){
        super();
        //this.image=image;
        this.id=id;
        this.imagePath=imagePath;
        //setLayout(null);
        //setBackground(Color.black);
        ImageIcon ic=new ImageIcon(imagePath);
        image=ic.getImage();
        System.out.println("Card path is: "+ic);
        setImage(image);//Sets image
        //setAutoSize(true);
        setOverbearing(true);//On click ,this panel gains lowest z-buffer
        setBorder(new LineBorder(Color.blue, 1));
        setSize(ic.getIconWidth(),ic.getIconHeight());
        
    }

	public int getId() {
        return id;
    }

    @Override
    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
        if (infoflags == ALLBITS) {
            repaint();
            setAutoSizeDimension();
            return false;
        }
        return true;
    }
 
    @Override
    public void paintComponent(Graphics g) {
        //super.paintComponents(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.clearRect(0, 0, getWidth(), getHeight());
        if (image != null) {
            setAutoSizeDimension();
            g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        } else {
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    }
    private Dimension adaptDimension(Dimension source, Dimension dest) {
        int sW = source.width;
        int sH = source.height;
        int dW = dest.width;
        int dH = dest.height;
        double ratio = ((double) sW) / ((double) sH);
        if (sW >= sH) {
            sW = dW;
            sH = (int) (sW / ratio);
        } else {
            sH = dH;
            sW = (int) (sH * ratio);
        }
        return new Dimension(sW, sH);
    }
    private void setAutoSizeDimension() {
        if (!autoSize) {
            return;
        }
        if (image != null) {
            if (image.getHeight(null) == 0 || getHeight() == 0) {
                return;
            }
            if ((getWidth() / getHeight()) == (image.getWidth(null) / (image.getHeight(null)))) {
                return;
            }
            autoSizeDimension = adaptDimension(new Dimension(image.getWidth(null), image.getHeight(null)), this.getSize());
            setSize(autoSizeDimension.width, autoSizeDimension.height);
        }
    }
     public void setImage(String image) {
        setImage(Toolkit.getDefaultToolkit().getImage(image));
    }

    /**
     * Set the value of image
     *
     * @param image new value of image
     */
    public void setImage(Image image) {
        this.image = image;
        repaint();
        setAutoSizeDimension();
    }
    public void setAutoSize(boolean autoSize) {
        this.autoSize = autoSize;
    }
}




please tell where i am wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: I am not getting images from folder using netbeans.

#2 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: I am not getting images from folder using netbeans.

Posted 14 June 2012 - 05:14 AM

Are you looking in the right folder?

"cardsp/layingcard/"


should it be

"cards/playingcard/"

Was This Post Helpful? 0
  • +
  • -

#3 ganessh.gothi@rediff.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Re: I am not getting images from folder using netbeans.

Posted 15 June 2012 - 05:54 AM

yes it is.
Was This Post Helpful? 0
  • +
  • -

#4 ganessh.gothi@rediff.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Re: I am not getting images from folder using netbeans.

Posted 15 June 2012 - 06:04 AM

I have tried from this but not getting the images form a folder. I don't know why?
I have find applet path , and the path is
file:/C:/Documents%20and%20Settings/admin/My%20Documents/Downloads/ProgressoApplication/web/


now I have paste my card folder in this directory and and use code base like-

img = getImage(getCodeBase(),C:\\cards\\playingcard\\+ faces.charAt(face)+ suits.charAt(suit) + ".gif");


Its work fine . but i don't want to use c directory , because its useless from server.
Now I have put folder in web dir. and the code base is


img = getImage(getCodeBase(),cards/playingcard/+ faces.charAt(face)+ suits.charAt(suit) + ".gif");

and also-
img = getImage(getCodeBase(),/cards/playingcard/+ faces.charAt(face)+ suits.charAt(suit) + ".gif");


but not getting images at applet, please help me, I have tried many times, please help me.

Sorry the code is after codebase
"C:\\cards\\playingcard\\"


Please consider this for above post.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: I am not getting images from folder using netbeans.

Posted 15 June 2012 - 06:31 AM

Create a package called 'images' under your source root and move all the images into it. Then try

img = getImage(getCodeBase(), "/images/" + faces.charAt(face)+ suits.charAt(suit) + ".gif");
// or img = getImage(getCodeBase(), "images/" + faces.charAt(face)+ suits.charAt(suit) + ".gif");

Was This Post Helpful? 0
  • +
  • -

#6 ganessh.gothi@rediff.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-June 12

Re: I am not getting images from folder using netbeans.

Posted 15 June 2012 - 07:11 AM

I have paste my cards folder with src folder its working on netbeans when I am generating a class file of applet and related class and the run not getting the images on applet.
I have past may cards in the same folder with my package.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1