Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 29th, 2008, 04:07 PM
Chris123 Chris123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 1 Chris123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 14 sec
Reputation Power: 0
Basic Java graphics help

Hello, this is my first time posting here and probably not my last. I'm currently learning Java at uni and have run into a problem with my code. First I'll give you the code, then explain my problem.

The first part is Class Board, this sets up a board for the board game with an array to show if the board is populated with Black, White or blank spaces.

Code:
import java.awt.*;
import java.awt.event.*;

public class Board extends Frame
{
    public int size;
    public String[][] pieces;
    public int increments;

    public Board(int Size)
    {
        size = Size;
        setTitle("The Board");
        setSize(520,560);
        setBackground(Color.white);
        setVisible(true);
        addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
        pieces = new String[size][size];
        
        for (int r=0; r<size; r++) 
        {
            for (int c=0; c<size; c++)
            {
              pieces[r][c] = " - ";   
            }
        }       
        increments = (500 / (size));   
    }


    public void paint(Graphics g)
    {
        /**
         * draw grid
         */
        for (int i=0; i<size; i++) 
        {
            g.drawLine(10, (40 + (i * increments)), 510, (40 + (i * increments)));            
            g.drawLine((10 + (i * increments)), 40, (10 + (i * increments)), 540);
        }
        
        g.drawLine(10, 540, 510, 540);
        g.drawLine(510, 40, 510, 540);    
    
        for (int r=0; r<size; r++)
        {
            for (int c=0; c<size; c++)
            {
               if (pieces[r][c] == " B ")
               {             
                  g.fillOval(((r*increments)+(increments/2)),((c*increments)+(increments/2)),increments-10,increments-10);
               } else if (pieces[r][c] == " W ")
                      {
                         g.drawOval(r*increments, c*increments, increments-5, increments-5);
                      }              
            }
        }        
    }
}


This second class part just creates the Board object with size 8.

Code:
public class Game
{
    public static void main(String[] args) 
    {
        Board theBoard = new Board(8);               
    }

}


My problem isn't a big one, despite the code. I've narrowed it down to these if statements.
if (pieces[r][c] == " B ")
else if (pieces[r][c] == " W ")

These are in public void paint(Graphics g). Despire the pieces[][] array being in the main Board class which I thought was global, these aren't included in the paint() method.

How can I sort this out?

Edit: The error message that comes up is a Java.lang.NullPointerException

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Basic Java graphics help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT