473,499 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array

25 New Member
Hi

i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.

how do i creat the box first using swing and no applet?

please note i dont wanna use button.

thank you
Apr 26 '08 #1
9 1674
Kid Programmer
176 New Member
just say:

g.drawRect(point.x, point.y, rectWidth, rectHeight);

Hope this helps. I'm not to good with Graphical User Interfaces though.
Apr 26 '08 #2
JosAH
11,448 Recognized Expert MVP
Hi

i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.
How do you want to display a four dimensional box on a two dimensional screen?

kind regards,

Jos
Apr 26 '08 #3
soty
25 New Member
How do you want to display a four dimensional box on a two dimensional screen?

kind regards,

Jos
i wanna use swing not applet. and i'm using frame and mouseEventListener
Apr 26 '08 #4
JosAH
11,448 Recognized Expert MVP
i wanna use swing not applet. and i'm using frame and mouseEventListener
Huh? erm, and I wanna have a beer not soda. and I'm using strong black tobacco
and Dutch jenever but I fail to see what that has to with four dimensional boxes.

kind regards,

Jos
Apr 26 '08 #5
sukatoa
539 Contributor
Hi

i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.

how do i creat the box first using swing and no applet?

please note i dont wanna use button.

thank you
Are you attempting to store all the RGB color combinations in an array?

Maybe you can just have it in a 2 dimensional array....
eg: box[current color][RGB colors]

regards,
sukatoa
Apr 27 '08 #6
soty
25 New Member
Are you attempting to store all the RGB color combinations in an array?

Maybe you can just have it in a 2 dimensional array....
eg: box[current color][RGB colors]

regards,
sukatoa
i'm tryna create a tictactoe game. but i dont wanna use gdraw . i'm tryna see if i could use jpanel. also i dont wanna use buttons either since i'm gonna use mouse eventlistener. finally the array for the boxes should be 2-dimensional array.

thank you
Apr 27 '08 #7
soty
25 New Member
finally i was able to come up wif this. but the thing is wrote method for checking if the board is full, method for win, and method t clear screen but i dont know how to use mouse listener to connect it.... also i dont know if my logic is right.


please i need help its due tuesday.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.event.MouseEvent;
  4. import javax.swing.*;
  5. import javax.swing.text.*;
  6. import java.text.DecimalFormat;
  7. import java.awt.datatransfer.*;
  8.  
  9. public class TicTacToeGame extends JFrame implements ActionListener {
  10.  
  11.     private JPanel ticTacPanel = new JPanel();
  12.     JLabel tictacpanelDisplay[][] = new JLabel[3][3];
  13.     ImageIcon xImage = createImageIcon("XImage.gif", "");
  14.     ImageIcon OImage = createImageIcon("OImage.gif", "");
  15.     ImageIcon plainImage = createImageIcon("plain.gif", "");
  16.     char player1 = 'x';
  17.     char player2 = 'o';
  18.     char empty = 'p';
  19.     char field[][] = new char[3][3];
  20.  
  21.     public TicTacToeGame() {
  22.  
  23.  
  24.         this.setLayout(new BorderLayout());
  25.         ticTacPanel.setLayout(new GridLayout(3, 3));
  26.         ticTacPanel.addMouseListener(new MouseAdapter() {
  27.         });
  28.         //create an instance of the menu
  29.         MenuBar mnuBar = new MenuBar();
  30.         setMenuBar(mnuBar);
  31.  
  32.         //construct and populate the File menu
  33.         Menu mnuFile = new Menu("File", true);
  34.         mnuBar.add(mnuFile);
  35.         MenuItem mnuFileNew = new MenuItem("New Game");
  36.         mnuFile.add(mnuFileNew);
  37.         MenuItem mnuFileScore = new MenuItem("See Score");
  38.         mnuFile.add(mnuFileScore);
  39.         MenuItem mnuFileExit = new MenuItem("Exit");
  40.         mnuFile.add(mnuFileExit);
  41.  
  42.         mnuFileNew.addActionListener(this);
  43.         mnuFileScore.addActionListener(this);
  44.         mnuFileExit.addActionListener(this);
  45.  
  46.         mnuFileNew.setActionCommand("New Game");
  47.         mnuFileScore.setActionCommand("See Score");
  48.         mnuFileExit.setActionCommand("Exit");
  49.  
  50.         for (int i = 0; i < 3; i++) 
  51.         {
  52.             for (int j = 0; j < 3; j++) 
  53.             {
  54.                 tictacpanelDisplay[i][j] = new JLabel(plainImage);
  55.                 ticTacPanel.add(tictacpanelDisplay[i][j]);
  56.             }
  57.         }
  58.         add(ticTacPanel, BorderLayout.CENTER);
  59.  
  60.         for (int row = 0; row < 3; row++) 
  61.         {
  62.             for (int column = 0; column < 3; column++) 
  63.             {
  64.                 field[row][column] = empty;
  65.                 System.out.print(field[row][column]);
  66.             }
  67.         }
  68.  
  69.     }
  70.  
  71.  
  72.     public boolean BoarfIsFull()
  73.     {
  74.         for (int i = 0; i < 3; i++)
  75.         {
  76.             for (int j = 0; j < 3; j++) 
  77.             {
  78.                 if(field[i][j]== empty)
  79.                     return false;
  80.             }
  81.         }
  82.         return true;
  83.  
  84.     }
  85.  
  86.  
  87.      public void clearboard() 
  88.      {
  89.         for (int i = 0; i < 3; i++) 
  90.         {
  91.             for (int j = 0; j < 3; j++) 
  92.             {
  93.                 field[i][j] = empty;
  94.             }
  95.         }
  96.         for (int i = 0; i < 3; i++) 
  97.         {
  98.             for (int j = 0; j < 3; j++) 
  99.             {
  100.                 tictacpanelDisplay[i][j] = new JLabel(plainImage);
  101.                 ticTacPanel.add(tictacpanelDisplay[i][j]);
  102.             }
  103.         }
  104.  
  105.     }
  106.  
  107.     public boolean BoardIsFull() {
  108.         for (int i = 0; i < 3; i++) {
  109.             for (int j = 0; j < 3; j++) {
  110.                 if (field[i][j] == empty) {
  111.                     return false;
  112.                 }
  113.             }
  114.         }
  115.         return true;
  116.  
  117.     }
  118.  
  119.     public void playgame(boolean click) 
  120.     {
  121.           int count=0;
  122.         for (int i = 0; i < 3; i++) 
  123.         {
  124.             for (int j = 0; j < 3; j++) 
  125.             {
  126.                 if (click && (!BoardIsFull()) &&(count==0||count==2||count==4||count ==6||count ==8)) 
  127.                 {
  128.  
  129.                    field[i][j]=player1;
  130.                 }
  131.                 else if(click && (!BoardIsFull()) &&(count==1||count==3||count==5||count ==7))
  132.                     field[i][j] = player2;
  133.  
  134.             }
  135.  
  136.         }
  137.     }
  138.  
  139.     public boolean isAwin(char token) {
  140.         for (int i = 0; i < 3; i++) {
  141.             if ((field[i][0] == token) && (field[i][1] == token) && (field[i][2] == token)) {
  142.                 return true;
  143.             }
  144.         }
  145.  
  146.         for (int j = 0; j < 3; j++) {
  147.             if ((field[0][j] == token) && (field[1][j] == token) && (field[2][j] == token)) {
  148.                 return true;
  149.             }
  150.         }
  151.  
  152.         if ((field[0][0] == token) && (field[1][1] == token) && (field[2][2] == token)) {
  153.             return true;
  154.         }
  155.  
  156.         if ((field[0][2] == token) && (field[1][1] == token) && (field[2][0] == token)) {
  157.             return true;
  158.         }
  159.  
  160.         return false;
  161.     }
  162.  
  163.     //     The code is executed when the mouse is pressed
  164.     public void mousePressed(MouseEvent me) {
  165.     }
  166.  
  167.  
  168.     public void actionPerformed(ActionEvent e) {
  169.         //test for menu item clicks
  170.         String arg = e.getActionCommand();
  171.         if (arg == "Exit") {
  172.             System.exit(0);
  173.         }
  174.     }
  175.  
  176.     /** Returns an ImageIcon, or null if the path was invalid. */
  177.     protected ImageIcon createImageIcon(String path,
  178.             String description) {
  179.         java.net.URL imgURL = getClass().getResource(path);
  180.         if (imgURL != null) {
  181.             return new ImageIcon(imgURL, description);
  182.         } else {
  183.             System.err.println("Couldn't find file: " + path);
  184.             return null;
  185.         }
  186.     }
  187.  
  188.  
  189.     public static void main(String args[]) {
  190.         TicTacToeGame mine = new TicTacToeGame();
  191.         mine.setBounds(300, 300, 300, 400);
  192.         mine.setTitle("The Game of Tic Tac Toe");
  193.         mine.setVisible(true);
  194.     }
  195. }
Apr 28 '08 #8
r035198x
13,262 MVP
1.) Don't mix AWT and Swing.
2) For the mouselistener, read Sun's tutorial here.
3.) For the correct logic, write down the correct steps in psuedo code and see if your program conforms to those steps.
Apr 28 '08 #9
soty
25 New Member
1.) Don't mix AWT and Swing.
2) For the mouselistener, read Sun's tutorial here.
3.) For the correct logic, write down the correct steps in psuedo code and see if your program conforms to those steps.
thanks i finally solve is but i dont know was jus happen and i started getting this error

i'm finally done wif ma code but i'm getting this error. do u av any idea wat the prob is?



Expand|Select|Wrap|Line Numbers
  1. init:
  2. deps-jar:
  3. Compiling 1 source file to /Users/ask4soteria/NetBeansProjects/ticTacToeGame/build/classes
  4. compile-single:
  5. run-single:
  6. Exception in thread "main" java.lang.NullPointerException
  7.         at ticTacToeGame.<init>(ticTacToeGame.java:38)
  8.         at ticTacToeGame.main(ticTacToeGame.java:208)
  9. Java Result: 1
  10. BUILD SUCCESSFUL (total time: 2 seconds)
Apr 30 '08 #10

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

Similar topics

2
2755
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5153
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3460
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55523
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10195
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10042
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
16845
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
3165
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
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
6894
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
7388
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...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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
4600
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
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.