Search code examples
javaswinggraphics2dparseint

Using ParseInt to convert a string from the command line and assign the cols and rows of the drawn g2 graphics


I'm sure my code is correct and this is just something very simple giving my runtime errors and nothing being painted. I know this is very simple but i feel like I've tried anything.

If I make numCols, numRows = a num it works fine but not when i make it = the corresponding nR/nC

I should be able to rung W1Graphics from the command line including two numbers that will set the rows and columns.

It needs to be done this way because is it part of some coursework.

Thanks ahead of time

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


public class W1Graphics extends JFrame
{
    MyPanel myVeryOwnPanel;
    int nR;
    int nC;

    public static void main(String[] args)
    {
        W1Graphics w = new W1Graphics();
        w.setVisible(true);

        // Convert the String into its corresponding integer
        //by calling one of the methods of the integer wrapper class
        int nR = Integer.parseInt(args[0]);
        int nC = Integer.parseInt(args[1]);
        System.out.println("The rows are: " +nR);
        System.out.println("The columns are: " +nC);
    }

    public W1Graphics()
    {
        setTitle("Workshop 1 - Lewis John Sherlock (Graphics): starting code");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(370, 290); ///Must stay in proportion to keep Ovals within lines
        setLocation(300,300);
        myVeryOwnPanel = new MyPanel(nR, nC);
        add(myVeryOwnPanel);
    }

 }

.......................

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

class MyPanel extends JPanel
{
    int numCols;
    int numRows;
    int nC, nR;


    public MyPanel(int nC, int nR)
    {
        numRows = nR;
        numCols = nC;
    }

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        // above: this "upgrades" the Graphics class to a Graphics2D class:
        // this has some extra methods that we'll use later on

        int w = getWidth();
        int h = getHeight();

        g2.setColor(Color.red);
        Rectangle r1 = new Rectangle(0,0,w,h);
        g2.fill(r1);

        g2.setColor(Color.green);
        Rectangle r2 = getRect(3,2);
        g2.fillOval(r2.x, r2.y, r2.width, r2.height);

        g2.setColor(Color.blue);
        Rectangle r3 = getRect(3,2);
        g2.fillOval(r3.x, r3.y, r3.width, r3.height);

        g2.setColor(Color.green);
        Rectangle r4 = getRect(1,1);
        g2.fillOval(r4.x, r4.y, r4.width, r4.height);

        g2.setColor(Color.blue);
        Rectangle r5 = getRect(2,2);
        g2.fillOval(r5.x, r5.y, r5.width, r5.height);

        g2.setColor(Color.blue);
        System.out.println( "Width is: " +w );
        System.out.println( "Height is: " +h );

        g2.setColor(Color.white);

        BasicStroke b = new BasicStroke(4);

        g2.setStroke(b);

        for(int thisRow = 0;thisRow<numRows;thisRow++)

        {
            Rectangle r6 = getRect(2, thisRow);
            g2.draw(r6);


            for(int thisCol = 0;thisCol<numCols;thisCol++)

            {
                Rectangle r7 = getRect(thisCol, thisRow);
                g2.draw(r7);
            }
        }
    }

    public Rectangle getRect(int thisCol, int thisRow)
    {
        if ((thisCol <= numRows-1) || (thisRow <= numCols-1))

                    {

                    int r_w = getWidth()/numCols;
                    int r_h = getHeight()/numRows;
                    int r_x = thisCol * r_w;
                    int r_y = thisRow * r_h;

                    Rectangle r = new Rectangle(r_x, r_y,50,50);


                        return r;
                    }

                    else {
                            return null;
                 }
    }

}

Runtime ERROR I receive is this....

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at MyPanel.paint(MyPanel.java:33)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown S
ource)
        at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.awt.Container.paint(Unknown Source)
        at java.awt.Window.paint(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.access$700(Unknown Source)
        at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at MyPanel.paint(MyPanel.java:33)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JLayeredPane.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown S
ource)
        at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.awt.Container.paint(Unknown Source)
        at java.awt.Window.paint(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.access$700(Unknown Source)
        at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Solution

  • Every int nR is its own declaration of a new variable. So try this:

    int nR;
    int nC;
    
    public static void main(String[] args)
    {
        // Convert the String into its corresponding integer
        //by calling one of the methods of the integer wrapper class
        int xnR = Integer.parseInt(args[0]);
        int xnC = Integer.parseInt(args[1]);
        System.out.println("The rows are: " +xnR);
        System.out.println("The columns are: " +xnC);
    
        W1Graphics w = new W1Graphics(xnR, xnC);
        w.setVisible(true);
    }
    
    public W1Graphics(int nR, int nC)
    {
        this.nR = nR;
        this.nC = nC;
    

    Previously the object's nR and nC were 0.