我确信我的代码是正确的,这只是一些非常简单的事情,给出了我的运行时错误并且没有绘制任何内容。 我知道这很简单,但我觉得我已经尝试过任何事情。
如果我使 numCols, numRows = 一个 num 它工作正常,但当我使它 = 相应的 nR/nC 时则不行
我应该能够从命令行调用 W1Graphics,其中包括两个用于设置行和列的数字。
它需要以这种方式完成,因为它是某些类(class)作业的一部分。
提前谢谢
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)
最佳答案
每个 int nR 都是它自己的新变量声明。 所以试试这个:
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。
关于java - 使用 ParseInt 从命令行转换字符串并分配绘制的 g2 图形的列和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132291/