Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ. (επεξεργασμένο)

Παιδία καλησπέρα,

 Εχώ μια εργάσια σε Java για ενα παιχνίδι Battleship και εχω κολλήσει στο εξής error :

 

Στο κομμάτι απο κατώ φτιάχνω το aircraft τύπου CreateGrid , oi 2 πρώτες τιμές (1,5) ειναι γραμμές και οι στήλες.

airCraft=new CreateGrid(1,5,Color.darkGray, Color.black,2, airCraft,"AirCraft",null,null);

 

το CreateGrid κανει τα εξής

for (i = 0; i < rows; i++) {             
            for (j = 0; j < cols; j++){              
                innerCells[i][j] = new JPanel();
                innerCells[i][j].setLayout(new BorderLayout());
                innerCells[i][j].setBorder(BorderFactory.createLineBorder(lineColor));
                innerCells[i][j].setBackground(backgroundColor);
                innerCells[i][j].addMouseListener(new MouseListener()

στον mouselistener exώ βαλει τον παρακάτω έλεγχο

if(x.getBackground()==Color.darkGray && panname.equals("AirCraft")){
                            testpan.innerCells[0][0].setBackground(Color.pink);
                        }

το error μου λέει οτι ειναι στο

testpan.innerCells[0][0].setBackground(Color.pink);

Αν αντί για testpan.innerCells[0][0].setBackground(Color.pink);

βάλω κάποιο sout το κανει σώστα οταν πατάω στο πλοίο αλλα οταν θελω να του αλλάξω χρώμα στα πανελάκια κολλάει.

 

Δεν ξερω αν τα εξήγησα σωστά βεβαια....

 

 

Ξέρω οτι δεν προγραμματιζω σωστά αλλα μολις πήρα αυτο το μάθημα και ακομα μαθαίνω....

Επίσης αλλαξα μερικά ονόματα το βράδυ ελπίζω να μην ειναι πρόβλημα..

 

o κώδικας της CreateGrid ειναι ο εξής

 

 

 

/*
 * 
 */
package project;


import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class CreateGrid extends JPanel {
                                     
    public Color backgroundColor,lineColor;                                                   
    public JPanel[][] innerCells;                       
    public int countPlayer=0,countPc=0,rows,cols,i,j,what;
    public CreateGrid testpan;
    public String panname,jname;
    public JFrame selframe;
    

    public CreateGrid(int rows,int cols,Color backgroundColor,Color lineColor,int choice,CreateGrid panel,String name,JFrame tritFrame,String iname) {
        this.testpan = panel;
        this.jname=iname;
        this.selframe=tritFrame;
        this.panname = name;
        this.what = choice;
        this.rows = rows;                               
        this.cols = cols;                              
        this.backgroundColor = backgroundColor;         
        this.lineColor = lineColor;     
        innerCells = new JPanel[rows][cols];         
        this.setLayout(new GridLayout(rows, cols)); 
        for (i = 0; i < rows; i++) {             
            for (j = 0; j < cols; j++){              
                innerCells[i][j] = new JPanel(); 
                innerCells[i][j].setLayout(new BorderLayout());
                innerCells[i][j].setBorder(BorderFactory.createLineBorder(lineColor));
                innerCells[i][j].setBackground(backgroundColor);
                innerCells[i][j].addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        JPanel x = (JPanel) e.getSource();
                        if(x.getBackground()==Color.gray && panname.equals("AirCraft")){
                            aircraftMoved(testpan);
                        }
                        if(what<=1 && what !=7){
                        if(x.getBackground()!= Color.white && x.getBackground()!= Color.red && x.getComponents().length == 0){
                            x.setBackground(Color.white);
                        }else if(x.getComponents().length != 0){
                            x.setBackground(Color.red);
                            countPlayer++;
                        }
                        if(countPlayer==17){
                            JOptionPane.showMessageDialog(null,jname+" is the Winner !");
                            programEnd();
                        }
                       pcPlay();
                       }             
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    @Override               
                    public void mouseReleased(MouseEvent e) {
                        JPanel x = (JPanel) e.getSource();
                        if(x.getBackground()!= Color.white && x.getBackground()!= Color.red && x.getBackground()!=Color.gray){
                        x.setBackground(Color.cyan);
                        }
                    }
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    @Override
                    public void mouseExited(MouseEvent e) {
                        JPanel x = (JPanel) e.getSource();
                        if(x.getBackground() == Color.GREEN){
                            x.setBackground(Color.cyan);
                        }
                        }
                });
                this.add(innerCells[i][j]);
            }
         
            }
        
        }

 
    
    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public JPanel[][] getInnerCells() {
        return innerCells;
    }

    public void setInnerCells(JPanel[][] innerCells) {
        this.innerCells = innerCells;
    }

    public CreateGrid getPanel() {
        return testpan;
    }

    public void setPanel(CreateGrid panel) {
        this.testpan = panel;
    }
                    
    public void pcPlay(){
                                Random random = new Random();                        
                        int one = random.nextInt(10),two=random.nextInt(10);
                        System.out.println(""+one+two);
                        if(testpan.innerCells[one][two].getBackground()!=Color.white && testpan.innerCells[one][two].getBackground()!=Color.red){
                        if(testpan.innerCells[one][two].getBackground()==Color.gray){
                          testpan.innerCells[one][two].setBackground(Color.red);
                            countPc++;
                            if(countPc==17){
                                JOptionPane.showInternalMessageDialog(null, "You Lost!");
                            }
                        }
                          else{
                          testpan.innerCells[one][two].setBackground(Color.white);
                        }}else{
                            
                        }
        
    }                
    public void programEnd(){
    selframe.setVisible(false);
 }
    public void aircraftMoved(CreateGrid aircraft){
        aircraft.innerCells[0][0].setBackground(Color.pink);
    }
  

    }

 

 

Επεξ/σία από 3wPsO
Δημοσ.

Μπορείς να μας γράψεις όλο το stack trace απ το exception. Επίσης και τον κώδικα της createGrid().

Χωρίς αυτά προσωπικά δεν μπορώ να καταλάβω τί μπορεί να φταίει. Ίσως κάποιος άλλος μπορέσει να βοηθήσει

Δημοσ.

Μπορείς να μας γράψεις όλο το stack trace απ το exception. Επίσης και τον κώδικα της createGrid().

Χωρίς αυτά προσωπικά δεν μπορώ να καταλάβω τί μπορεί να φταίει. Ίσως κάποιος άλλος μπορέσει να βοηθήσει

Πως μπορώ να στείλω κώδικα ετσι ωστε να υπάρχει η επιλογή show/hide ?

Δημοσ.

Καλημέρα. Υπάρχει στον editor του φόρουμ το κουμπί < > που είναι για παράθεση κώδικα. Για τη λειτουργία show/hide δεν γνωρίζω.

Δημοσ.

είναι μια από τις απλές εξαιρέσεις της java, πήγαινε στην σειρά και δες πιο αντικείμενο δεν έχει αρχικοποιηθεί(Επομένως έχει null τιμή), πιθανότατα το innerCells

  • Like 1
Δημοσ. (επεξεργασμένο)

 

Παιδιά βρήκα το λάθος μου ! Σας ευχαριστω ολους για την βοήθεια ! Πως μπορώ να κλείσω το thread ?

Επεξ/σία από 3wPsO

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...