Use of Canvas

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ramola
    New Member
    • Sep 2007
    • 35

    Use of Canvas

    hi , I am developing a game wherein i need to place a map( static ). The cities in this map need to be traversed .
    I was wondering if anybody could help me with the following :

    1. how do I place the map on the panel ? ( using what.... )
    2. How do i traverse it step by step on the roll of a dice? ( the moves should be auto generated i.e. the user is not allowed to drag his marble on the board .)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by malavikarane
    hi , I am developing a game wherein i need to place a map( static ). The cities in this map need to be traversed .
    I was wondering if anybody could help me with the following :

    1. how do I place the map on the panel ? ( using what.... )
    2. How do i traverse it step by step on the roll of a dice? ( the moves should be auto generated i.e. the user is not allowed to drag his marble on the board .)
    You could represent that map as a simple image which you can draw in your JPanel.
    The marbles are best drawn on the 'glasspanel' of the JFrame itself; they appear
    'on top' of the map then. Read all about it in the API documentation of the JFrame.

    kind regards,

    Jos

    Comment

    • Ramola
      New Member
      • Sep 2007
      • 35

      #3
      Thanks alot Josah.......... .

      The first part of the task is achieved ! i.e. I've put the map on a JLabel n then put tht JLabel on the respective panel. I hope this is what u suggested ( if there is some kind of variation , plz let me know )

      Now , the second part : Placing the marbles on the map
      Like u suggested the use of glasspanes ,I have no clue abt. the same !
      I was wondering if u use a JPanel and set it as opaque, will it achieve the same effect ?

      kind regards,
      Malavika

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by malavikarane
        Now , the second part : Placing the marbles on the map
        Like u suggested the use of glasspanes ,I have no clue abt. the same !
        I was wondering if u use a JPanel and set it as opaque, will it achieve the same effect ?

        kind regards,
        Malavika
        You're on the right track: a glass pane is a non-opaque pane in front of all the
        other stuff in your JFrame. You should set your JPanel non-opaque and visible:

        [code=java]
        JPanel myGlassPane= new JPanel(<suitabl e layout manager>);
        myGlassPane.set Opaque(false);
        myGlassPane.set Visible(true);

        myFrame.setGlas sPane(myGlassPa ne);
        [/code]

        When you add a component, icon, or whatever to the myGlassPane component
        it will show up in front of the image which is drawn in your normal content pane
        somewhere.

        As an alternative, if you want marbles to pass in front of each other, you could
        have a look at the JLayeredPane class.

        kind regards,

        Jos

        Comment

        • Ramola
          New Member
          • Sep 2007
          • 35

          #5
          Originally posted by JosAH
          You're on the right track: a glass pane is a non-opaque pane in front of all the
          other stuff in your JFrame. You should set your JPanel non-opaque and visible:

          [code=java]
          JPanel myGlassPane= new JPanel(<suitabl e layout manager>);
          myGlassPane.set Opaque(false);
          myGlassPane.set Visible(true);

          myFrame.setGlas sPane(myGlassPa ne);
          [/code]

          When you add a component, icon, or whatever to the myGlassPane component
          it will show up in front of the image which is drawn in your normal content pane
          somewhere.

          As an alternative, if you want marbles to pass in front of each other, you could
          have a look at the JLayeredPane class.

          kind regards,

          Jos
          Jos,
          when u say " myFrame.setGlas sPane(myGlassPa ne);"
          here myFrame is gonna be the panel wherein I have my map rite ???

          n then like I added the map to the panel in the following way :

          [code=java]

          JPanel pnlmap=new JPanel();
          ImageIcon imgicon =new ImageIcon("main Map.gif");
          JLabel lblmapimg=new JLabel(imgicon) ;
          pnlmap.add(lblm apimg);
          getContentPane. add(pnlmap,Bord erLayout.CENTER );

          [/code]

          I can place the marbel image in a similar style of coding ( i.e. placing an imge on d label n then adding the label to the panel .....) as well rite ??

          or is there any variation to it ????

          regards,
          Malavika

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by malavikarane
            Jos,
            when u say " myFrame.setGlas sPane(myGlassPa ne);"
            here myFrame is gonna be the panel wherein I have my map rite ???
            Erm, no; if you want your map (image) stored stored in just a JPanel which is
            part of the JFrame, use the alternative method I suggested. Read up on the
            JLayeredPane class; it can do the job too.

            Stick your image map in the lowest layer and put your marble in a layer on top
            of it.

            kind regards,

            Jos

            Comment

            • Ramola
              New Member
              • Sep 2007
              • 35

              #7
              Originally posted by JosAH
              Erm, no; if you want your map (image) stored stored in just a JPanel which is
              part of the JFrame, use the alternative method I suggested. Read up on the
              JLayeredPane class; it can do the job too.

              Stick your image map in the lowest layer and put your marble in a layer on top
              of it.

              kind regards,

              Jos
              Hey, I used JLayeredPane n got it working !
              Thanks Alot!

              I am now able to place a marble on top of the map !

              the next thing tht I need to know is.......
              On the roll of the dice the marble takes the respective no. of steps.
              These no. of steps will b taken automatically i.e. the player will not be allowed to drag the marble n place it ( so to avoid validation i.e. if the player has dragged only the rite no. of steps ).
              The system will do they placing of marbles at appropriate pos.
              There in all 34 positions !

              I was thinking of storing the co-ordinates of these 34 positions ( in what ?????)
              n once the dice is rolled goto d appropriate pos using the array of co-ordinates !

              After reaching the 34 th pos d player should be able to go back to pos 1 n continue the cycle !

              so what i want to exactly know is :
              1. Is my track of thinking rite ?
              2. What sort of a collection is likely to be the best for storing the co-ordinates ?
              3. how do i loop back ?( i.e. after pos 34 goto 1 pos....)


              Kind regards,
              Malavika

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by malavikarane
                so what i want to exactly know is :
                1. Is my track of thinking rite ?
                2. What sort of a collection is likely to be the best for storing the co-ordinates ?
                3. how do i loop back ?( i.e. after pos 34 goto 1 pos....)


                Kind regards,
                Malavika
                1) I don't know; you were talking about all sorts of ideas.

                2) I think an array or an ArrayList could do the job easily; design a 'Coordinate'
                class (it should be a simple x,y object) and have an array(list) of those.

                3) [code=java]
                int curPos= ...
                int nxtPos= (++curPos)%34;
                [/code]

                ... assuming that position indexes start at 0, not 1; Java likes it that way.

                kind regards,

                Jos

                Comment

                • Ramola
                  New Member
                  • Sep 2007
                  • 35

                  #9
                  Originally posted by JosAH
                  1) I don't know; you were talking about all sorts of ideas.

                  2) I think an array or an ArrayList could do the job easily; design a 'Coordinate'
                  class (it should be a simple x,y object) and have an array(list) of those.

                  3) [code=java]
                  int curPos= ...
                  int nxtPos= (++curPos)%34;
                  [/code]

                  ... assuming that position indexes start at 0, not 1; Java likes it that way.

                  kind regards,

                  Jos
                  1. Am i thinking in the rite direction with ref to : Having a list of co-ordinates n then traversing these co-ordinates using the list !
                  is there a better way to achieve to the same kind of functionality ?

                  kind regards,
                  Malavika

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by malavikarane
                    1. Am i thinking in the rite direction with ref to : Having a list of co-ordinates n then traversing these co-ordinates using the list !
                    is there a better way to achieve to the same kind of functionality ?

                    kind regards,
                    Malavika
                    I don't know; your idea makes sense especially because you want your coordinates
                    to be indexed, i.e. coordinate #0, coordinate #1 etc.

                    kind regards,

                    Jos

                    Comment

                    • Ramola
                      New Member
                      • Sep 2007
                      • 35

                      #11
                      Originally posted by JosAH
                      I don't know; your idea makes sense especially because you want your coordinates
                      to be indexed, i.e. coordinate #0, coordinate #1 etc.

                      kind regards,

                      Jos

                      There is a totally different thing tht i wish to ask you :

                      how can i read data from a file n put it on a screen a JFrame ........

                      i was trying to do the following but it doesnt seem to work

                      Code:
                      import java.util.*;
                      import java.io.*;
                      import javax.swing.*;
                      import java.awt.*;
                      import java.awt.event.*;
                      
                      
                      public class Instructions extends JFrame
                      {
                      	FileInputStream in;
                      	JPanel pnlinstructions;
                      	JTextArea txtarea;
                      	Container content;
                      	byte data[];
                      
                      	
                      	public Instructions()
                      	{
                      		
                      		pnlinstructions=new JPanel();
                      		txtarea=new JTextArea();
                      		try
                      		{
                      		   in = new FileInputStream("abc.txt");
                      		   data = new byte[10000];
                      		  
                      		   BufferedInputStream bis = new BufferedInputStream(in);
                      		   DataInputStream dis = new DataInputStream(bis);
                      			
                      		   int filelength = dis.read(infile);
                      		   String filestring = new String(infile, 0, filelength);
                      		   System.out.println("FILE CONTENT=" + filestring);
                      			txtarea.setText(filestring);
                      		
                      		}
                      		catch(IOException e)
                      		{}	   
                      		   
                      		   
                      	 	
                      	    setTitle("Instructions");
                      	 	pnlinstructions.add(txtarea);
                      		
                      		content = getContentPane();
                      		content.add(pnlinstructions);
                      		
                      		
                      		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                      		setSize(500,500);
                      		setVisible(true);
                      				
                      	}
                       public static void main(String args[])
                       {
                       	new Instructions();
                       }
                      }
                      plz suggest something !!

                      kind regards ,
                      Malavika

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by malavikarane
                        There is a totally different thing tht i wish to ask you :

                        how can i read data from a file n put it on a screen a JFrame ........

                        i was trying to do the following but it doesnt seem to work

                        Code:
                        import java.util.*;
                        import java.io.*;
                        import javax.swing.*;
                        import java.awt.*;
                        import java.awt.event.*;
                        
                        
                        public class Instructions extends JFrame
                        {
                        	FileInputStream in;
                        	JPanel pnlinstructions;
                        	JTextArea txtarea;
                        	Container content;
                        	byte data[];
                        
                        	
                        	public Instructions()
                        	{
                        		
                        		pnlinstructions=new JPanel();
                        		txtarea=new JTextArea();
                        		try
                        		{
                        		   in = new FileInputStream("abc.txt");
                        		   data = new byte[10000];
                        		  
                        		   BufferedInputStream bis = new BufferedInputStream(in);
                        		   DataInputStream dis = new DataInputStream(bis);
                        			
                        		   int filelength = dis.read(infile);
                        		   String filestring = new String(infile, 0, filelength);
                        		   System.out.println("FILE CONTENT=" + filestring);
                        			txtarea.setText(filestring);
                        		
                        		}
                        		catch(IOException e)
                        		{}	   
                        		   
                        		   
                        		 
                        	    setTitle("Instructions");
                        		 pnlinstructions.add(txtarea);
                        		
                        		content = getContentPane();
                        		content.add(pnlinstructions);
                        		
                        		
                        		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                        		setSize(500,500);
                        		setVisible(true);
                        				
                        	}
                         public static void main(String args[])
                         {
                         	new Instructions();
                         }
                        }
                        plz suggest something !!

                        kind regards ,
                        Malavika
                        Do not use DataInputStream for reading textfiles.
                        See Sun's tutorial on JTextComponents and use one of those components to display your file contents.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by malavikarane
                          plz suggest something !!
                          Read the API documentation of the JTextArea class: it can read from a Reader
                          and setup itself with the content of the Reader.

                          kind regards,

                          Jos

                          Comment

                          Working...