Weird random errors, java drawing
<p>I have written a program which I think ought to work flawlessly. For some reason it doesn't. I will provide the code and hope that somebody can figure out what's wrong. I've been sitting with it for hours but I can't get any further.</p>

<pre><code>import java.awt.*;
import javax.swing.*;

public class CrystalModel
{
    private int radius;
    private int index;
    private boolean[][] crystal;
    private Point concludingPoint;
    private int escapeRadius;

    /**
     * Constructor. Initiates an electron bath of size 30x30.
     */
    public CrystalModel()
    {
        radius = 30;
        index = 30*2-1;
        start();
    }

    /**
     * Constructor. Initiates an electron bath of size r.
     * @param r bath radius
     */
    public CrystalModel(int r)
    {
        radius = r;
        index = r*2-1;
        start();
    }

    /**
     * Initiates the experiment
     */
    private void start()
    {
        crystal = new boolean[radius*2][radius*2];
        crystal[radius][radius] = true; //The width is always even (2*r), this is as close to the center as one gets
        escapeRadius = (int)(1.1*radius);
    }

    /**
     * Determines if a given xy-coordinate is within radius
     * @param x x-coordinate
     * @param y y-coordinate
     * @return whether the active ion is out of range
     */
    private boolean outsideCircle(int r, int x, int y)
    {
        return (Math.pow(x,2)+Math.pow(y,2) &gt;= Math.pow(r, 2));
    }

    /**
     * Determines if the currently active ion has a neighbouring crystallized ion
     * @param whether the is a neighbour
     */
    private boolean anyNeighbours(int x, int y)
    {
            x = xBathToModel(x);
            y = yBathToModel(y);

            boolean left    = (x-1 &gt;= 0) &amp;&amp; (x-1 &lt;= index) &amp;&amp; (y &gt;= 0) &amp;&amp; (y &lt;= index)  ? crystal[x-1][y] : false;
            boolean right   = (x+1 &gt;= 0) &amp;&amp; (x+1 &lt;= index) &amp;&amp; (y &gt;= 0) &amp;&amp; (y &lt;= index)  ? crystal[x+1][y] : false;
            boolean up      = (y-1 &gt;= 0) &amp;&amp; (y-1 &lt;= index) &amp;&amp; (x &gt;= 0) &amp;&amp; (x &lt;= index)  ? crystal[x][y-1] : false;
            boolean down    = (y+1 &gt;= 0) &amp;&amp; (y+1 &lt;= index) &amp;&amp; (x &gt;= 0) &amp;&amp; (x &lt;= index)  ? crystal[x][y+1] : false;

            return ( left || right || up || down );
    }

    /**
     * Determines an xy-coordinate at radius distance from the center
     * @param radius radius of escape for ions
     * @return Point object encapsulating x, y
     */
    private Point dropNewIon()
    {
        double angle = (int)(Math.random()*2*Math.PI);
        return new Point( (int)( Math.cos(angle)*(index/2) ), (int)( Math.sin(angle)*(index/2) ) );
    }

    /**
     * Transform x-coordinate upon the moving of origo from center to top-left
     * @ x x-coordinate
     */
    public int xBathToModel(int x)
    {
        return radius+x;
    }

    /**
     * Transform y-coordinate upon the moving of origo from center to top-left
     * @param y y-coordinate
     */
    public int yBathToModel(int y)
    {
        return radius+y;
    }

    /**
     * Increments the number of ions in the crystal by one
     * @return boolean indicating whether the experiment is done or not
     */
    private Point crystallizeOneIon()
    {
        Point point = dropNewIon();
        for( ; ; )
        {
            switch((int)(Math.random()*4+1))
            {
                case 1: point.x+=1;
                    break;
                case 2: point.x-=1;
                    break;
                case 3: point.y+=1;
                    break;
                case 4: point.y-=1;
                    break;
            }

            if(outsideCircle(escapeRadius, point.x, point.y)) point = dropNewIon();

            if(anyNeighbours(point.x, point.y)) break;
        }

        crystal[xBathToModel(point.x)][yBathToModel(point.y)] = true;
        return point;
    }

    /**
     * Let the algorithm (CrystalExperiment) simulate a number of steps
     * @param steps how many steps
     * @return boolean indiciating whether the experiment is concluded
     */
    public boolean runExperiment(int steps)
    {
        for(int i=0; i&lt;steps; i++)
        {
            concludingPoint = crystallizeOneIon();

            if(outsideCircle((int)index/2, concludingPoint.x, concludingPoint.y)) 
            {
                concludingPoint.x = xBathToModel(concludingPoint.x);
                concludingPoint.y = yBathToModel(concludingPoint.y);
                return true;
            }
            else
            {
                concludingPoint.x = xBathToModel(concludingPoint.x);
                concludingPoint.y = yBathToModel(concludingPoint.y);
            }
        }

        return false;
    }

    /**
     * Return a textual representation of the crystal
     * @return String representing the crystal
     */
    public String toString()
    {
        String output = "";
        for(int y=-1; y&lt;=index+1; y++)
        {
            for(int x=-1; x&lt;=index+1; x++)
            {
                if(y == -1) output+="--";
                else if(y == index+1) output+="--";
                else
                {
                    if(x == -1) output+="|";
                    else if(x == index+1) output+="|";
                    else
                    {
                        if(concludingPoint.equals(new Point(x,y))) output+="# ";
                        else if(crystal[x][y] == true) output+="* ";
                        else output+="  ";
                    }
                }
            }
            output+="\n";
        }

        return output;
    }

    public int getIndexSize()
    {
        return index;
    }

    public boolean getMatrixValue(int x, int y)
    {
        return crystal[x][y];
    }

    private void drawCrystal()
    {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setBounds(0, 0, 200, 200);

        CrystalView drawing = new CrystalView(this);
        frame.add(drawing);
    }

    public static void main(String[] args)
    {
        (new CrystalModel(200)).drawCrystal();
    }

}
</code></pre>

<p>This is my view, it doesn't work (although the toString method works, and this is similar):</p>

<pre><code>import javax.swing.*;
import java.awt.*;

public class CrystalView extends JPanel
{
    CrystalModel model;
    public CrystalView(CrystalModel m)
    {
        model = m;
    }

    public void paintComponent(Graphics g)
    {
        while(!model.runExperiment(1))
        {
            for(int y=0; y&lt;model.getIndexSize(); y++)
            {
                for(int x=0; x&lt;model.getIndexSize(); x++)
                {
                    if(model.getMatrixValue(x,y))
                    g.drawOval(x, y, 1, 1);
                }
            }
            this.repaint();
        }
    }
}
</code></pre>

<p>The error is in the drawing. It (1) generates errors in the terminal, and (2) doesn't draw a full size. Please run the program and you will see what I mean.</p>
