0

私のコードは正しいと確信しています。これは非常に単純なもので、実行時エラーが発生し、何もペイントされません。これはとても簡単なことですが、何でも試したような気がします。

numColsを作成すると、numRows = numは正常に機能しますが、作成すると機能しません=対応するnR / nC

行と列を設定する2つの数値を含むコマンドラインからW1Graphicsを実行できるはずです。

それはいくつかのコースワークの一部であるため、この方法で行う必要があります。

よろしくお願いします

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;
                 }
    }

}

私が受け取るランタイムエラーはこれです...

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)
4

1 に答える 1

3

すべてのintnRは、新しい変数の独自の宣言です。だからこれを試してみてください:

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;

以前は、オブジェクトのnRとnCは0でした。

于 2012-08-26T17:30:29.513 に答える