मैं यह कहकर शुरू करना चाहता हूं कि यह एक असाइनमेंट है। मैं नहीं चाहता कि उत्तर चम्मच मुझे खिलाए लेकिन मैं जानना चाहता हूं कि मेरी समस्याएं क्या हो रही हैं।जावा: एक ग्रिड पर एक आयताकार भरना
मैं वर्तमान में Conway's Game of Life को कार्यान्वित कर रहा हूं। सेल को क्लिक करने से रंग बदलना चाहिए, यह दर्शाता है कि उस सेल को जीवित राज्य में स्विच किया जा रहा है। यदि फिर से क्लिक किया गया है, तो इसे डिफ़ॉल्ट रंग में वापस जाना चाहिए।
जब मैं खिड़की में कहीं भी क्लिक करता हूं, तो प्रोग्राम लाइन 56 पर एक नल पॉइंटर अपवाद फेंकता है। आखिरी दिन या उससे भी इस पर अटक गया है, इसलिए किसी भी मदद की सराहना की जाती है। धन्यवाद!
यहाँ कोड:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class VisibleGrid extends JPanel implements MouseListener, KeyListener{
CellGrid cellGrid;
Graphics rect;
public VisibleGrid(){
addMouseListener(this);
cellGrid = new CellGrid();
}
//Draw the grid of cells, 7px wide, 75 times to create 75x75 grid
public void paint(Graphics g){
for(int i=0; i<525;i=i+7){
for(int j = 0; j<525; j=j+7){
g.drawRect(i ,j,7,7);
}
}
}
//auxillary method called to fill in rectangles
public void paint(Graphics g, int x, int y){
g.fillRect(x, y, 7, 7);
repaint();
}
//main method, adds this JPanel to a JFrame and sets up the GUI
public static void main(String[] args){
JFrame j = new JFrame("Conway's Game of Life");
j.setLayout(new BorderLayout());
j.add(new VisibleGrid(), BorderLayout.CENTER);
JTextArea info = new JTextArea("Press S to Start, E to End");
info.setEditable(false);
j.add(info, BorderLayout.SOUTH);
j.setSize(530,565);
j.setVisible(true);
}
//these methods are to satisfy the compiler/interface
//Begin Mouse Events
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){
//fill the selected rectangle
rect.fillRect(e.getX(), e.getY(), 7,7);
repaint();
//set the corresponding cell in the grid to alive
int row = e.getY() /7;
int column = e.getX() /7;
cellGrid.getCell(row, column).setAlive(true);
}
//End Mouse Events
//These methods are to satisfy the compiler/interface
//Begin KeyEvents
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
कौन सी लाइन लाइन 56 है? जब मैं कोड कॉपी/चिपकाता हूं, तो यह int कॉलम = e.getX()/7 था; जो सही नहीं दिखता –
मैं शर्त रेखा 56 है _rect.fillRect (e.getX(), e.getY(), 7,7); _ –
जैसा कि @guido कहते हैं, सुनिश्चित करें कि ग्राफिक्स ऑब्जेक्ट, 'rect' इसे एक्सेस करने से पहले प्रारंभ या वैध संदर्भ है। – Rupak