首先,我对java很新。 我必须制作2个棋盘格,但我的代码不打印任何东西。 我之前从未见过这个错误所以我不知道下一步该做什么。有人可以帮忙吗?
import java.awt.*;
import java.applet.*;
/**
* checkers
* @author /////////
* @version 3.7.2018
*/
public class checkers extends Applet {
public static void main(String args[]){
Graphics myG;
drawCheckerboard(100,100, myG);
drawCheckerboard(120,500, myG);
}
/**
* this method makes a checkerboard in the size that you want.
* @param Graphics thaks the Graphics as a parameter
*/
public void drawCheckerboard(int x, int y, Graphics g) {
int row;
int col;
for ( row = 0; row < 8; row++ ) {
for ( col = 0; col < 8; col++) {
x = col * 20;
y = row * 20;
if ( (row % 2) == (col % 2) ){
g.setColor(Color.white);
}
else{
g.setColor(Color.black);
g.fillRect(x, y, 20, 20);
}
}
}
}
答案 0 :(得分:0)
小程序已被正式弃用 - 是时候停止使用它们了 - 此外,Applet
没有main
方法。
显然对于绘画在AWT / Swing中的工作原理缺乏了解,因为你要长时间地进入NullPointerException
。停止您正在做的事情,首先阅读Performing Custom Painting和Painting in AWT and Swing
public static void main
是static
上下文,这意味着它无法引用非静态(或实例)字段和方法。有关详细信息,请查看Understanding Class Members和Static methods vs Instance methods in Java。
请记住,static
不是你的朋友,请谨慎而明智地使用它。
我没有尝试直接修改你的代码,而是因为Applet
已经死了,我提供了一个如何完成绘画的简单例子......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
drawCheckerboard(100, 100, g2d);
g2d.dispose();
}
protected void drawCheckerboard(int x, int y, Graphics2D g) {
int row;
int col;
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
x = col * 20;
y = row * 20;
if ((row % 2) == (col % 2)) {
g.setColor(Color.white);
} else {
g.setColor(Color.black);
g.fillRect(x, y, 20, 20);
}
}
}
}
}
}
现在,您的绘画例程也存在两个问题。第一个,它实际上并没有使用传递给它的x
/ y
参数,因此两个板都被绘制到同一个位置。其次,它忽略了white
单元格。
你可以简单地解决这个问题......
protected void drawCheckerboard(int x, int y, Graphics2D g) {
int row;
int col;
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
int xPos = col * 20;
int yPos = row * 20;
if ((row % 2) == (col % 2)) {
g.setColor(Color.white);
} else {
g.setColor(Color.black);
}
g.fillRect(x + xPos, y + yPos, 20, 20);
}
}
}
此外,您需要更改getPreferredSize
以容纳内容...
@Override
public Dimension getPreferredSize() {
return new Dimension(280, 660);
}