Results 1 to 3 of 3
- 02-19-2011, 09:28 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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.
- 02-19-2011, 03:52 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Double posted: Image Graph2D problem
-
Similar Threads
-
How to post multiple image in frame?
By Miwrath in forum AWT / SwingReplies: 2Last Post: 10-14-2010, 07:43 AM -
how to set an image to a frame in swing?
By vrk in forum AWT / SwingReplies: 2Last Post: 10-20-2009, 02:52 PM -
Drawing image on JPanel from another frame
By jeryslo in forum AWT / SwingReplies: 9Last Post: 10-18-2009, 12:21 AM -
adding image in a frame(urgent)
By arunkumarinfo in forum NetBeansReplies: 0Last Post: 02-23-2009, 10:49 AM -
how i can put image in background of frame??
By ahmed13 in forum NetBeansReplies: 4Last Post: 01-15-2009, 05:47 PM


LinkBack URL
About LinkBacks


Bookmarks