473,508 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of Canvas

35 New Member
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 .)
Sep 24 '07 #1
12 2975
JosAH
11,448 Recognized Expert MVP
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
Sep 24 '07 #2
Ramola
35 New Member
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
Sep 25 '07 #3
JosAH
11,448 Recognized Expert MVP
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:

Expand|Select|Wrap|Line Numbers
  1. JPanel myGlassPane= new JPanel(<suitable layout manager>);
  2. myGlassPane.setOpaque(false);
  3. myGlassPane.setVisible(true);
  4.  
  5. myFrame.setGlassPane(myGlassPane);
  6.  
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
Sep 25 '07 #4
Ramola
35 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. JPanel myGlassPane= new JPanel(<suitable layout manager>);
  2. myGlassPane.setOpaque(false);
  3. myGlassPane.setVisible(true);
  4.  
  5. myFrame.setGlassPane(myGlassPane);
  6.  
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.setGlassPane(myGlassPane);"
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 :

Expand|Select|Wrap|Line Numbers
  1.  
  2. JPanel pnlmap=new JPanel();
  3.     ImageIcon imgicon =new ImageIcon("mainMap.gif");
  4.    JLabel lblmapimg=new JLabel(imgicon); 
  5.     pnlmap.add(lblmapimg); 
  6.  getContentPane.add(pnlmap,BorderLayout.CENTER);
  7.  
  8.  
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
Sep 25 '07 #5
JosAH
11,448 Recognized Expert MVP
Jos,
when u say " myFrame.setGlassPane(myGlassPane);"
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
Sep 25 '07 #6
Ramola
35 New Member
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
Sep 26 '07 #7
JosAH
11,448 Recognized Expert MVP
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)
Expand|Select|Wrap|Line Numbers
  1. int curPos= ...
  2. int nxtPos= (++curPos)%34;
  3.  
... assuming that position indexes start at 0, not 1; Java likes it that way.

kind regards,

Jos
Sep 26 '07 #8
Ramola
35 New Member
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)
Expand|Select|Wrap|Line Numbers
  1. int curPos= ...
  2. int nxtPos= (++curPos)%34;
  3.  
... 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
Sep 26 '07 #9
JosAH
11,448 Recognized Expert MVP
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
Sep 26 '07 #10
Ramola
35 New Member
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

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class Instructions extends JFrame
  9. {
  10.     FileInputStream in;
  11.     JPanel pnlinstructions;
  12.     JTextArea txtarea;
  13.     Container content;
  14.     byte data[];
  15.  
  16.  
  17.     public Instructions()
  18.     {
  19.  
  20.         pnlinstructions=new JPanel();
  21.         txtarea=new JTextArea();
  22.         try
  23.         {
  24.            in = new FileInputStream("abc.txt");
  25.            data = new byte[10000];
  26.  
  27.            BufferedInputStream bis = new BufferedInputStream(in);
  28.            DataInputStream dis = new DataInputStream(bis);
  29.  
  30.            int filelength = dis.read(infile);
  31.            String filestring = new String(infile, 0, filelength);
  32.            System.out.println("FILE CONTENT=" + filestring);
  33.             txtarea.setText(filestring);
  34.  
  35.         }
  36.         catch(IOException e)
  37.         {}       
  38.  
  39.  
  40.  
  41.         setTitle("Instructions");
  42.          pnlinstructions.add(txtarea);
  43.  
  44.         content = getContentPane();
  45.         content.add(pnlinstructions);
  46.  
  47.  
  48.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  49.         setSize(500,500);
  50.         setVisible(true);
  51.  
  52.     }
  53.  public static void main(String args[])
  54.  {
  55.      new Instructions();
  56.  }
  57. }
  58.  
plz suggest something !!

kind regards ,
Malavika
Sep 28 '07 #11
r035198x
13,262 MVP
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

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class Instructions extends JFrame
  9. {
  10.     FileInputStream in;
  11.     JPanel pnlinstructions;
  12.     JTextArea txtarea;
  13.     Container content;
  14.     byte data[];
  15.  
  16.  
  17.     public Instructions()
  18.     {
  19.  
  20.         pnlinstructions=new JPanel();
  21.         txtarea=new JTextArea();
  22.         try
  23.         {
  24.            in = new FileInputStream("abc.txt");
  25.            data = new byte[10000];
  26.  
  27.            BufferedInputStream bis = new BufferedInputStream(in);
  28.            DataInputStream dis = new DataInputStream(bis);
  29.  
  30.            int filelength = dis.read(infile);
  31.            String filestring = new String(infile, 0, filelength);
  32.            System.out.println("FILE CONTENT=" + filestring);
  33.             txtarea.setText(filestring);
  34.  
  35.         }
  36.         catch(IOException e)
  37.         {}       
  38.  
  39.  
  40.  
  41.         setTitle("Instructions");
  42.          pnlinstructions.add(txtarea);
  43.  
  44.         content = getContentPane();
  45.         content.add(pnlinstructions);
  46.  
  47.  
  48.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  49.         setSize(500,500);
  50.         setVisible(true);
  51.  
  52.     }
  53.  public static void main(String args[])
  54.  {
  55.      new Instructions();
  56.  }
  57. }
  58.  
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.
Sep 28 '07 #12
JosAH
11,448 Recognized Expert MVP
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
Sep 28 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2191
by: Mickel Grönroos | last post by:
Hi, I'm trying to put an Tkinter.Entry of fixed size onto a specific location on a canvas using the place manager. The idea is that one can double-click a rectangle object on a canvas to get an...
1
3125
by: Mickel Grönroos | last post by:
Hi, I have a Tkinter.Canvas of variable width. Is there a standard way of asking the canvas which parts of it that is visible? I.e. on the horizontal scale, I would like to know at what fraction...
5
5565
by: Andrew Poulos | last post by:
Is there a right/best way to draw an ellipse using Canvas? (With VML there's the v:oval element which makes the exercise trivial). I tried drawing a 360 degree arc (a circle) and then scaling it...
11
2054
by: Aaron Gray | last post by:
Hi, I have put together a bit of JavaScript to make a square resizable canvas :- http://www.aarongray.org/Test/JavaScript/resizable.html Problems I have :- a) I cannot seem to center it...
3
19863
by: moondaddy | last post by:
I'm working in a WPF windows application and am wondering if it's possible to rotate a Canvas or user control derived from Canvas. I created a user control which derives from Canvas and I need to...
2
18221
TMS
by: TMS | last post by:
Schools over!!! Now its time to play. I would like to learn how to make objects move from one location to the next on a canvas widget. For example: from Tkinter import * class square:...
6
3530
by: Nebulism | last post by:
I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from...
2
5554
by: devnew | last post by:
hi i am new to tkinter and would like some help with canvas i am doing validation of contents of a folder and need to show the ok/error messages on a canvas resultdisplay =Canvas(...)...
10
7338
by: blaine | last post by:
Hey everyone! I'm not very good with Tk, and I am using a very simple canvas to draw some pictures (this relates to that nokia screen emulator I had a post about a few days ago). Anyway, all is...
4
10076
by: moondaddy | last post by:
I have a wpf project where I use a canvas to drag shapes around. when I drag a shape beyond the right or bottom side I get scrollbars which is good. I also get scrollbars when I zoom in and a...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.