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

    Default Image Graph2D problem

    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.
    Last edited by Urasb; 02-19-2011 at 07:10 AM.

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

    Default

    Double posted: Need help with Image Component Frame

    But it doesn't work.
    What about it doesn't work? Does it compile? Exceptions? Unexpected behavior?

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

    Default

    All the code above compilable, but it doesn't do what I expect.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    it doesn't do what I expect.
    And what do you expect? What does it do? You need to give us much more information than just it doesn't do what I want...without knowing what you want the advice you receive could mean anything

  5. #5
    Urasb is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    sorry to make you confuse.
    OK I will list what I except.
    1. A class for loading pictures, get the images from file with a 2d array of type Image.
    (public class Map)
    2. A component to draw the pictures.
    (public class MapComponent extends JComponent)
    3. A Frame to display the component.
    (public class MapViewer)

    Appreciate.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Start simple -- can you upload and display images in a simple app? Also, you don't want to read files, upload images or create objects (exceptions to the last occasionally) in a paintComponent method as this method must be as sleek and blindingly fast as possible. Also, you have no direct control on when or if the method will be called.

  7. #7
    Urasb is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks you guys. I have solve my problem, here is my new code
    Java Code:
    public class Map
    {
    	public Map()
    	{
    	}
    	
    	public void loading(int row, int column, String path)
    	{
    		try{
    		this.row = row;
    		this.column = column;
    		Total_Images = row*column;
    		MapMatrix = new BufferedImage[row][column];
    		int count = 1;
    		String location;
    		for( int i = 0; i < row; i++)
    		{
    			for( int j = 0; j < column; j++)
    			{
    				//location = DocumentBase.replace("\\","/")+"/Map/"+path+"/"+path+"_"+count+".gif";
    				MapMatrix[i][j] =ImageIO.read(new File(DocumentBase.replace("\\","/")+"/Map/"+path+"/"+path+"_"+count+".gif"));
    				count++;
    			}
    		}
    	        }
    		catch (IOException ex) {
    		// handle exception...
    		}
    	}
    	
    	public int size_row()
    	{
    		return row;
    	}
    	
    	public int size_column()
    	{
    		return column;
    	}
    	
    	public BufferedImage 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 BufferedImage[][] MapMatrix;
    }
    
    public class MapComponent extends JComponent
    {
    	final int ScreenX = 20;
    	final int ScreenY = 15;
    	final int unitX = 40;
    	final int unitY = 40;
    
    	private BufferedImage image;
    	private Map currentMap;
    
    	public MapComponent() 
    	{
    		currentMap = new Map();
    		currentMap.loading(6,21,"FireHell");
    	}
    
    	public void paintComponent(Graphics g) 
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		for(int x = 0; x < ScreenX; x++)
    		{
    			for(int y = 0; y < ScreenY; y++)
    			{
    				if(x < currentMap.size_column()&& y < currentMap.size_row())
    				{
    					g2.drawImage(currentMap.getMap(y,x), x*unitX, y*unitY,unitX,unitY,null);
    				}
    			}
    		}
    	
    	}
    }
    
     public class MapViewer
    {
    	public static void main(String[] args)
    	{
    		final int WIDTH = 800;
    		final int HIGHT = 600;
    		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);
    		
    	}
    	
    }

Similar Threads

  1. still problem with image processing
    By syarizma in forum Advanced Java
    Replies: 2
    Last Post: 01-26-2012, 06:47 PM
  2. Problem with loading image
    By m00nchile in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 01:34 PM
  3. image problem when using any browser
    By ocean in forum New To Java
    Replies: 3
    Last Post: 09-17-2009, 10:29 PM
  4. Image reading from URL problem
    By paras in forum Advanced Java
    Replies: 4
    Last Post: 04-18-2009, 07:26 AM
  5. moving image - PROBLEM
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-17-2008, 06:52 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
  •