Results 1 to 3 of 3
  1. #1
    Urasb is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Need help with Image Component Frame

    I am trying to create a background or map on a frame.
    First I make a map class which is purpose of loading map from bunch of pictures.
    I have a Image 2D array under the map class that sort the image.

    Then I make a MapComponent class in order to draw Image.
    Last a MapViwer class to display frame.

    But it doesn't work. I guess I have some problem with processing image sorting or reading.

    here is my attempt
    Java Code:
    public class Map
    {
    	public Map()
    	{
    	}
    	
    	public void loading(int row, int column, String path)
    	{
    		this.row = row;
    		this.column = column;
    		Total_Images = row*column;
    		MapMatrix = new Image [row][column];
    		int count = 1;
    		String location;
    		for( int i = 0; i < row; i++)
    		{
    			for( int j = 0; j < column; j++)
    			{
    				location = DocumentBase.replaceAll("/","\\")+"Map/"+path+"/"+path+"_"+count+".gif"; // I tried to get the path but file  problem with displaying '/'
    				//MapMatrix[i][j] =Toolkit.getDefaultToolkit().createImage(location);
    				MapMatrix[i][j] =Toolkit.getDefaultToolkit().createImage("C:/Documents and Settings/Administrator/My Documents/cpsc/MYP/Map/FireHell/FireHell_1.gif");
    				count++;
    			}
    		}
    	}
    	
    	public int size_row()
    	{
    		return row;
    	}
    	
    	public int size_column()
    	{
    		return column;
    	}
    	
    	public Image getMap(int row, int column)
    	{
    		return MapMatrix[row][column];
    	}
    	
    	private int Total_Images;
    	private int row;
    	private int column;
    	private String DocumentBase = System.getProperty("user.dir");
    	private Image[][] MapMatrix;
    }
    
    
    public class MapComponent extends JComponent
    {
    	public MapComponent()
           {  
           }
           
           public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		//the check board background
    		Map currentMap = new Map();
    		currentMap.loading(6,21,"HellFire");
    		g2.drawImage(currentMap.getMap(1,1),100,100,this);
    		
    		
    	}
    }
    
     public class MapViewer
    {
    	public static void main(String[] args)
    	{
    		final int WIDTH = 500;
    		final int HIGHT = 525;
    		JFrame frame = new JFrame();
            	frame.setSize(WIDTH, HIGHT);
            	frame.setTitle("Map Viewer");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		MapComponent map = new MapComponent();
    		frame.add(map);
    		frame.setVisible(true);
    		
    	}
    	
    }

    Note: It is very important to keep the map load the maps in a matrix form
    easy to manipulate later.

    I believe MapMatrix[i][j] =Toolkit.getDefaultToolkit().createImage("C:/Documents and Settings/Administrator/My Documents/cpsc/MYP/Map/FireHell/FireHell_1.gif"); has some problem
    what I mean here is the MapMatrix[i][j] get the Image which is drawable in map component.
    and also have problem to find the solution for change the path over time.
    I know that there is method called getDocumentBase() but it is true for Applet, I don't use Applet in this case.

    Thanks for sharing all.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Double posted: Image Graph2D problem

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. How to post multiple image in frame?
    By Miwrath in forum AWT / Swing
    Replies: 2
    Last Post: 10-14-2010, 08:43 AM
  2. how to set an image to a frame in swing?
    By vrk in forum AWT / Swing
    Replies: 2
    Last Post: 10-20-2009, 03:52 PM
  3. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Swing
    Replies: 9
    Last Post: 10-18-2009, 01:21 AM
  4. adding image in a frame(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-23-2009, 11:49 AM
  5. how i can put image in background of frame??
    By ahmed13 in forum NetBeans
    Replies: 4
    Last Post: 01-15-2009, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •