My project wont run in netbeans?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3500 Views - Last Post: 13 November 2011 - 09:36 PM Rate Topic: -----

#1 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

My project wont run in netbeans?

Posted 13 November 2011 - 05:32 PM

Ok so I have been following this book, Sams Teach yourself java in 24 hours, So i can basically get the foundation of programing. So I am completely new to programming anything, so sorry if I don't understand your response.

My program is packaged and I am trying to create a game (that the book tells me to create) But the book tells me to Clean and build the program. So I do then I press run project, and it tells me No main project is found. It has made me angry over the past three days of carefully reading what the book says and researching the internet, till I finally decided to post here. Anyway if you would like to see my codes, and all the classes I will be more then happy to post them. There are no errors (That I see or NetBeans notices) and whenever I click on the .html file and select view it opens up the webpage and has a box on the web page that says "Error, cannot load file." The book does say something about an FTP. Now am I supposed to upload my file to an FTP just to get it to run in netbeans so I can play the game the book taught me how to write?

Please help thanks

Is This A Good Question/Topic? 0
  • +

Replies To: My project wont run in netbeans?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 05:34 PM

On your projects menu in NetBeans, make sure you Right-Click and select the "Set As Main Project" or similar option. Also, make sure you are trying to run a class with a main() method.
Was This Post Helpful? 0
  • +
  • -

#3 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 05:40 PM

View Postmacosxnerd101, on 13 November 2011 - 05:34 PM, said:

On your projects menu in NetBeans, make sure you Right-Click and select the "Set As Main Project" or similar option. Also, make sure you are trying to run a class with a main() method.


Im not sure if I have a main() method looking at everything, I have 3.java classes and 1.html class but It was pretty much copied from the authors book and he ran it perfectly would you like me to upload my 3 java classes? and maybe you can explain why its not running, so I can understand the mistake?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 05:45 PM

You are welcome to post them. Please remember to use code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

#5 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 05:52 PM

This is Button.java

 
package com.java24hours.banko;

import javax.swing.*;

public class Button extends JButton {
    // the buttonís position
    public int row;
    public int column;
    // the buttonís state (-2 money, -1 cleared, 0 uncleared)
    public int state;
    // the number of adjacent money bags
    public int near;
    // true if this is a money bag that has been found
    public boolean found;
    // the game board
    private Board board;
    // the two bag graphics
    private static ImageIcon unknownBag;
    private static ImageIcon moneyBag;

    // create a new button
    Button(Board board, int row, int column) {
        super("");
        // store the board that contains this button
        this.board = board;
        // store the buttonís position
        this.row = row;
        this.column = column;
        // set up button graphics (if necessary)
        if (unknownBag == null) {
            unknownBag = board.applet.getImageIcon("unknown_bag.gif");
            moneyBag = board.applet.getImageIcon("money_bag.gif");
        }
        // set up the button
        setup();
    }

    // set up the button for gameplay
        public void setup() {
        fill();
        state = 0;
        near = 0;
        found = false;
        // remove the background color (from a previous game)
        setBackground(null);
    }

    // reveal an unopened bag in the square
    public void fill() {
        // give this button the unopened bag icon
        setIcon(unknownBag);
        // remove the label
        setText("");
        // enable the button
        setEnabled(true);
    }

    // reveal an empty square and count adjacent money bags
    public void clear(boolean clicked) {
        if (clicked) {
            /* this square was manually cleared by the user, so tell
               the applet to charge the player */
            board.applet.spendMoney();
        }
        // remove the icon
        setIcon(null);
        // tell the button it is clear and disable it
        state = -1;
        setEnabled(false);
        if (near > 0) {
            // reveal that one or more money bags are nearby
            setText("" + near);
        } else {
            /* inspect the buttons above, below, left, and right
               of this one */
            Button above = above();
            Button below = below();
            Button left = left();
            Button right = right();
            /* if any of these buttons is clear and has no money bags
                nearby, clear it ó using recursion to fan out around
                the board */
            if ((above != null) && (above.state == 0)) {
                // clear the button above
                above.clear(false);
            }
            if ((below != null) && (below.state == 0)) {
                // clear the button below
                below.clear(false);
            }
            if ((left != null) && (left.state == 0)) {
                // clear the button to the left
                left.clear(false);
            }
            if ((right != null) && (right.state == 0)) {
                // clear the button to the right
            right.clear(false);
            }
        }
    }

    // reveal a money bag
    public void revealMoney() {
        // give this button the money bag icon
        setIcon(moneyBag);
        // remove the label
        setText("");
        if (!found) {
            /* this bag has never been found, so tell the applet
               to award money to the player */
            board.applet.earnMoney();
            // tell this bag it has been found
            found = true;
        }
    }

    /* these four directional methods inspect four buttons adjacent to
    this one, returning null on the edges of the game board */

    public Button above() {
        if (row > 0) {
            // return the adjacent button
            return board.square[row - 1][column];
        }
        // return null to indicate a board edge has been passed
        return null;
    }

    public Button below() {
        if (row < board.rows - 1) {
            return board.square[row + 1][column];
        }
        return null;
    }

    public Button left() {
        if (column > 0) {
            return board.square[row][column - 1];
        }
        return null;
    }

    public Button right() {
        if (column < board.columns - 1) {
            return board.square[row][column + 1];
        }
        return null;
    }
}



next will be Board.java

package com.java24hours.banko;

import java.awt.*;
import javax.swing.*;

public class Board extends JPanel {
    // the boardís rows, columns, and total money bags
    protected int rows;
    protected int columns;
    protected int money_bags;
    // the buttons on the board
    public Button[][] square;
    // the game applet
    public Banko applet;

    public Board(Banko applet, int rows, int columns, int money_bags) {
        // store the applet
        this.applet = applet;
        // store the rows, columns, and money bags on the board
        this.rows = rows;
        this.columns = columns;
        this.money_bags = money_bags;
        // create the board and set its layout
        square = new Button[rows][columns];
        GridLayout grid = new GridLayout(rows, columns);
        setLayout(grid);
        // set up each button
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                // create the button
                Button button = new Button(this, row, column);
                // add it to the board
                square[row][column] = button;
                // assign the applet to monitor button clicks
                button.addActionListener(applet);
                // add the button to the user interface
                add(button);
            }
        }
        setup();
    }

    // set up the board for gameplay
    public void setup() {
        // set up each button for gameplay
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                square[row][column].setup();
            }
        }
        // count the number of money bags hidden so far
        int hidden = 0;
        while (hidden < money_bags) {
            // choose a random position for the money bag
            int row = (int)Math.floor(Math.random() * rows);
            int column = (int)Math.floor(Math.random() * columns);
            if (square[row][column].state == -2) {
                // thereís already a money bag in that square
                continue;
            }
            // hide a money bag and increment the count
            square[row][column].state = -2;
            hidden++;
        }
        // count the number of bags in adjacent squares
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                if (square[row][column].state == 0) {
                    // count the bags adjacent to this square
                    square[row][column].near = getBagCount(row, column);
                }
            }
        }
    }

    // determine the money bags adjacent to a square
    private int getBagCount(int row, int column) {
        // start counting
        int count = 0;
        /* set the lower and upper boundaries of the search area around a
           square, making sure not to go outside the game board */
        int above = (row > 0) ? row - 1 : row;
        int below = (row < rows - 1) ? row + 1 : row;
        int left = (column > 0) ? column - 1 : column;
        int right = (column < columns - 1) ? column + 1 : column;
        for (int i = above; i <= below; i++) {
            for (int j = left; j <= right; j++) {
                if (square[i][j].state == -2) {
                    /* a money bag is nearby, so increment the count for
                       this square */
                    count++;
                }
            }
        }
        return count;
    }

    // set the margins of the game board to 15 pixels
    public Insets getInsets() {
        // set the top, left, bottom and right inset margins
        return new Insets(15, 15, 15, 15);
    }
}



next is Banko.java (its the game im trying to re-create as the author is teaching me

package com.java24hours.banko;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class Banko extends JApplet implements ActionListener {
    // the money inside a money bag
    public static int REWARD = 1000;
    // the cost of opening an empty bag
    public static int COST = 250;
    // the size of the game board and total number of money bags
    public static int ROW_COUNT = 9;
    public static int COLUMN_COUNT = 14;
    public static int BAG_COUNT = 10;
    // the fields that report how a playerís doing
    private JTextField moneyField;
    private JTextField foundField;
    // the restart button
    public JButton restart;
    // a playerís money and the number of money bags found
    public int money;
    public int found;
    // the game board
    private Board board;

    // create the applet
    public void init() {
        // set its layout manager
        BorderLayout border = new BorderLayout();
        setLayout(border);

        // create the top panel
        JPanel top = new JPanel();
        // create the "Money:" label and text field
        JLabel moneyLabel = new JLabel("Money: $");
        moneyField = new JTextField("", 8);
        // prevent it from being edited
        moneyField.setEditable(false);
        // create the "Found:" label and text field
        JLabel foundLabel = new JLabel("Found: ");
        foundField = new JTextField("", 8);
        foundField.setEditable(false);
        // create the Restart button
        restart = new JButton("Restart");
        // assign the applet to monitor clicks of this button
        restart.addActionListener(this);
        // add the components to the top panel
        top.add(moneyLabel);
        top.add(moneyField);
        top.add(foundLabel);
        top.add(foundField);
        top.add(restart);
        // add the panel to the borderís topmost position
        add(top, BorderLayout.NORTH);

        // create the game board
        board = new Board(this, ROW_COUNT, COLUMN_COUNT, BAG_COUNT);
        // add the board to the borderís center position
        add(board, BorderLayout.CENTER);
        // set up the applet
        setup();
        // display the user interface
        setVisible(true);
    }

    // set up the applet for gameplay
    public void setup() {
        found = -1;
        money = 0;
        // give the player starting money
        earnMoney();
    }

    // take money from the player for opening an empty bag
    public void spendMoney() {
        // deduct funds and display the new total
        money = money - COST;
        moneyField.setText("" + money);
        if (money <= 0) {
            // the playerís broke, so end the game
            revealBoard();
        }
    }

    // award money to the player for finding a money bag
    public void earnMoney() {
        // add funds and display the total
        money = money + REWARD;
        moneyField.setText("" + money);
        // count the newly found bag
        found++;
        foundField.setText("" + found + " of " + BAG_COUNT);
        if (found >= BAG_COUNT) {
            // the playerís found all bags, so end the game
            revealBoard();
        }
    }

    // reveal the entire board at gameís end
    public void revealBoard() {
        // inspect every square on the board
        for (int row = 0; row < ROW_COUNT; row++) {
            for (int column = 0; column < COLUMN_COUNT; column++) {
                // get the current button
                Button button = board.square[row][column];
                if (button.state == -2) {
                    // display this money bag
                    button.found = true;
                    button.revealMoney();
                } else {
                    // set up happy and sad colors
                    Color green = new Color(204, 255, 204);
                    Color red = new Color(255, 204, 204);
                    if (money > 0) {
                        // player won, so make this empty square green
                        button.setBackground(green);
                    } else {
                        // player lost, so make this empty square red
                        button.setBackground(red);
                    }
                }
                if (button.state == 0) {
                    // this square has never been cleared, so do so now
                    button.clear(false);
                }
            }
        }
    }

    // load a button graphic using the appletís codebase as a base URL
    public ImageIcon getImageIcon(String filename) {
        ImageIcon icon = null;
        try {
            // build a URL using the codebase and the graphicís filename
            URL url = new URL(getCodeBase(), filename);
            // load an image icon from that URL
            icon = new ImageIcon(url);
        } catch (java.net.MalformedURLException exception) {
            // report an error because the URL isnít valid
            System.out.println(exception.getMessage());
            return null;
        }
        return icon;
    }

    // monitor button clicks on the game board and restart button
    public void actionPerformed(ActionEvent event) {
        // determine the button the player clicked
        Object source = event.getSource();
        if (source instanceof Button) {
            // the buttonís on the game board
            Button button = (Button) event.getSource();
            if (button.state == -2) {
                // it contains a money bag, so reveal it
                button.revealMoney();
            } else {
                // it doesnít contain a money bag, so clear it
                button.clear(true);
            }
        } else {
            // the restart button was clicked
            // set up the applet anew
            setup();
            // set up the board too
            board.setup();
        }
    }
}



next is Banko.html

<applet 
    code="com.java24hours.banko.Banko"
    archive="Banko.jar"
    width="650"
    height="450">
</applet>



This should load it to a web page as explained in the author

here is the authors web page

http://workbench.cad.../chapter.php/24

it tells me build sucessful, but the program still wont find the main project when i try to run it.

Also if you could recommend another starter book to learn java that would be helpful, Thanks a lot
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 06:41 PM

Honestly, I think you're biting off too much too soon. I would start with something smaller. A lot smaller. Even if your book provides you sample code. Game programming isn't easy, especially if you don't have a good handle on the basics. I have a thread Getting Better at Programming Java, that outlines a learning path along with DIC tutorials. Now, we also have an ongoing thread on beginner books for Java.

I don't believe NetBeans has an Applet Viewer. I could be wrong. You could easily set up a JFrame subclass to run your Applet in NetBeans though.
public class MyFrame extends JFrame{

   public MyFrame(){
      this.setSize(800,800);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Banko banko = new Banko();
      banko.init();
      this.add(banko);
      this.setVisible(true);
      
   }

   public static void main(String[] args){ new MyFrame(); }
}


Was This Post Helpful? 0
  • +
  • -

#7 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 06:52 PM

Sir, with all due respect; I was able to run applets in netbeans before in other chapters of the book and they opened up an applet window (too the size I set). If that is what the JApplet command is supposed to do, then I believe netbean does open up applet windows called from the applet class. But Like I said I am only an extremely novice programmer and I do not know fully if that is the right window so I could be wrong.

But I do agree with jumping into too much too soon, as I am extremely confused with what he was talking about. Even though that was the last chapter of the book, I only understood about three quarters of what he was talking about. (still having trouble understanding what (!=) means. I actually have been looking at your tutorial and Its good im learning some things that the book did not teach me, like the scanner topic is what I am on now. But I will also check out the books also.

Thanks a lot for the help sir i appreciate it.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 06:56 PM

I've never figured out how to run Applets from NetBeans. To be fair, I've never looked that hard either. That's always been my method of testing. :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:06 PM

View PostTalcik, on 13 November 2011 - 09:52 PM, said:

Sir, with all due respect; I was able to run applets in netbeans before in other chapters of the book and they opened up an applet window (too the size I set).

OK, how did you created your .jar file ?

Show us the cointent of it
Was This Post Helpful? 0
  • +
  • -

#10 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:29 PM

View Postmacosxnerd101, on 13 November 2011 - 06:56 PM, said:

I've never figured out how to run Applets from NetBeans. To be fair, I've never looked that hard either. That's always been my method of testing. :)



Sir, its actually quite easy first you just import the class

 import java.applet.*; //If needed

public class //name of .java file extends from JApplet {
// code




heres an example im sure you probably already know this

import java.awt.*;

public class SalutonApplet extends javax.swing.JApplet {
    String greeting;
    
    public void init() {
        greeting = "hey!";
    }
    public void paint(Graphics screen) {
        Graphics2D screen2D = (Graphics2D) screen;
        screen2D.drawString(greeting, 25, 50);
    }
}


then you just need an .html file to get this to run on a webpage if you want or, you can just run that file alone and you should just get a java applet window which says "hey!"
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:31 PM

Yes I am convince macosxnerd101 knows Java :) and how to write an Applet in NetBeans, Eclipse, NotePad, ...
We are more concerned on how YOU use NetBeans

How did you created your .jar file ?

Show us the content of it

This post has been edited by pbl: 13 November 2011 - 08:00 PM

Was This Post Helpful? 2
  • +
  • -

#12 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:40 PM

View Postpbl, on 13 November 2011 - 07:31 PM, said:

Yes I think macosxnerd101 knows Java :) and how to write an Applet
We are more concerned on how YOU use NetBeans

How did you created your .jar file ?

Show us the content of it


the .jar file, was created once I selected build main projected as directed in the book, and the .jar file is in the dist folder of my project. if you understand what I am trying to say Sir

how do I go about uploading the content?
Was This Post Helpful? 0
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:44 PM

> jar tf Banko.jar
Was This Post Helpful? 0
  • +
  • -

#14 Talcik   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:50 PM

View Postpbl, on 13 November 2011 - 07:44 PM, said:

> jar tf Banko.jar



That is my jar file sir,

sorry with all due respect Im not 100% sure if your asking a question sir, or If you are trying to say thats how I upload it again, I am new extremely new. But would you like me to upload my jar file? or tell you everything in my projects folders?
Was This Post Helpful? 0
  • +
  • -

#15 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: My project wont run in netbeans?

Posted 13 November 2011 - 07:56 PM

Don't want you to upload your .jar
Want you to show us what your .jar contains by executing and showing us the output of the command

> jar tf Banko.jar
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2