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
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Beginning Java
NullPointerException
Jing Liang
Ranch Hand
Posts: 40
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello guys,
I couldn't fix this NPE. Please Help. Thanks
In my playGame() method, I have
if(temp1==temp2) { repaint(); } else {repaint(); t= new Timer(2000, new GuessGame()); t.setRepeats(false); t.start(); }
I receive NPE from t.start();
Here are my program
import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax.swing.*; public class GuessGame extends Applet implements ActionListener, MouseListener { int x,y; Box [][] boxes = new Box[4][4]; Box boxChoice; Button newGame; Timer t; int col; int row; int initialBoxX = 50; int initialBoxY = 50; int width = 50; int height = 50; Graphics g; int counter=0; Color itemp, temp1, temp2; int row1, col1, row2, col2; public void init() { 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; int localX, localY; for(int col = 0; col <=3; col++) { for(int row = 0; row<=3; row++) { localY = initialBoxY + row*height; localX = initialBoxX + col*width; 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]); } } 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() { if(counter==3) { boxes[row1][col1].showColor=false; counter=0; repaint(); } if(counter==1) { temp1=itemp; row1=row; col1=col; repaint(); } if(counter==2) { row2=row; col2=col; if(row1==row2 && col1==col2) { counter=1; return; } else { temp2=itemp; counter=0; } if(temp1==temp2) { repaint(); } else {repaint(); t= new Timer(2000, new GuessGame()); t.setRepeats(false); t.start(); } } } public void actionPerformed(ActionEvent event) { if(event.getSource()==newGame) { setNewGame(); } else { boxes[row1][col1].showColor=false; boxes[row2][col2].showColor=false; repaint(); } } 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(); if((x>=initialBoxX && x<=4*width+initialBoxX)&&(y>=initialBoxY && y<=4*height+initialBoxY)) { int rowSubscript = (y - initialBoxY)/height; int colSubscript = (x - initialBoxX)/width; this.row=rowSubscript; this.col=colSubscript; if(boxes[rowSubscript][colSubscript].showColor==true) { counter=3; playGame(); } else { counter++; itemp = boxes[rowSubscript][colSubscript].boxColor; boxes[rowSubscript][colSubscript].showColor=true; playGame(); } } else { } } 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() { boxes[row][col].showColor=true; } } } class Ticker implements ActionListener { GuessGame timer=new GuessGame(); boolean tick = true; public void actionPerformed(ActionEvent eve) { timer.repaint(); } }
Jing Liang
Ranch Hand
Posts: 40
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Problem solved thanks guys
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
The x,y coordinates are not working
Need some help, pleeaaase!!
Turn Based Strategy Games
cannot find symbol
In need of help with making 4x4 grid
More...