Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
The x,y coordinates are not working
Jing Liang
Ranch Hand
Posts: 40
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
When I click on the squres, it shows the color on the left. Could someone please help me out?
Here are my codes
/* *Program Name: GuessGame *Description: This program is designed to create a color matching game. */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class GuessGame extends Applet implements ActionListener, MouseListener { int x,y; Box [][] boxes = new Box[4][4]; Box boxChoice; int col; int row; int initialBoxX = 50; int initialBoxY = 50; int width = 50; int height = 50; Graphics g; int localX, localY; public void init() { Button newGame = new Button("New Game"); add(newGame); newGame.addActionListener(this); addMouseListener(this); setBackground(Color.white); setNewGame(); } public void setNewGame() { int chosenColors[] = {0,0,0,0,0,0,0,0}; int rint; for(int col = 0; col <=3; col++) { System.out.println("my col value is: " + col); for(int row = 0; row<=3; row++) { System.out.println("my row value is: " + row); localY = initialBoxY + row*height; rint = (int)(Math.random() * 8); while (chosenColors[rint] >= 2) { rint = (int)(Math.random() * 8); } chosenColors[rint] = chosenColors[rint] +1; Color colorChoices[] = {Color.red,Color.blue,Color.cyan, Color.green, Color.magenta,Color.pink,Color.orange, Color.yellow}; boxes[row][col] = new Box(localX, localY, height, width, colorChoices[rint]); System.out.println("coordinates: row " + row + " column " + col); } localX = initialBoxX + col*width; } repaint(); } public void paint(Graphics g) { displayGame(g); } public void displayGame(Graphics g) { // go to each box and display it. for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { //System.out.println("display coordinates: row "+i+" column "+j); boxes[i][j].display(g); } } // now make the solid lines around the boxes } public void playGame(Box newestChosen) { } public void actionPerformed(ActionEvent event) { setNewGame(); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { x=e.getX(); y=e.getY(); int rowSubscript = (y - initialBoxY)/height; int colSubscript = (x - initialBoxX)/width; boxes[rowSubscript][colSubscript].showColor=true; repaint(); //which will call display for all } } class Box { int x, y; int width; int height; Color boxColor; Graphics g; boolean showColor = false; // this is a flag - it will later tell the box // if it has been clicked or not public Box(int X1,int Y1,int h ,int w, Color myColor) { x = X1; y = Y1; height = h; width = w; boxColor = myColor; } public void display(Graphics graph) { g = graph; if (showColor == false) { g.setColor(Color.gray); g.fillRect(x, y, height, width); } else { g.setColor(boxColor); g.fillRect(x, y, height, width); } } public void picked() { } }
Jing Liang
Ranch Hand
Posts: 40
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have figured it out.
Thanks anyway.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
NullPointerException
cannot find symbol
need help with making a 4x4 grid. code and details inside.
In need of help with making 4x4 grid
Confused on how to create a rectangle
More...