我正在编写一个程序,该程序应该创建3张图像,并使用Applet通过Java为它们着色。编译后,Eclipse会抛出大量错误,我不知道如何解决该问题。
我已经研究了这个问题,并且发现有人指出我需要作为配置运行,并将JRE环境更改为较新的版本,但是我已经使用了最新的环境。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JApplet;
public class GraphicalImages extends JApplet {
private String shapeType;
public void init() {
// Get width
int width = Integer.parseInt(getParameter("width"));
// Get height
int height = Integer.parseInt(getParameter("height"));
// Set dimension
setSize(width, height);
// Get shape
shapeType = getParameter("shapeType");
}
public void paint(Graphics g) {
// Check shape type
if (shapeType.equalsIgnoreCase("bus")) {
// Draw chassis
g.setColor(Color.YELLOW);
g.fillRect(100, 100, 350, 150);
g.fillRect(450, 150, 80, 100);
// Draw windows
g.setColor(Color.LIGHT_GRAY);
g.fillRect(120, 130, 40, 40);
g.fillRect(190, 130, 40, 40);
g.fillRect(260, 130, 40, 40);
g.fillRect(330, 130, 40, 40);
g.fillRect(400, 130, 40, 40);
// Draw door
g.fillRect(460, 170, 30, 60);
// Draw wheels
g.setColor(Color.BLACK);
g.fillOval(140, 230, 50, 50);
g.fillOval(425, 230, 50, 50);
} else if (shapeType.equalsIgnoreCase("flower")) {
// Draw petals
g.setColor(Color.CYAN);
g.fillOval(250, 100, 99, 99);
g.fillOval(184, 156, 99, 99);
g.fillOval(316, 156, 99, 99);
g.fillOval(217, 232, 99, 99);
g.fillOval(283, 232, 99, 99);
// Draw center
g.setColor(Color.YELLOW);
g.fillOval(250, 170, 99, 99);
} else if (shapeType.equalsIgnoreCase("tree")) {
// Set color
g.setColor(Color.GREEN);
// Draw top
Polygon polygon = new Polygon();
polygon.addPoint(300, 50);
polygon.addPoint(243, 125);
polygon.addPoint(357, 125);
g.fillPolygon(polygon);
// Draw middle
polygon.reset();
polygon.addPoint(300, 75);
polygon.addPoint(218, 200);
polygon.addPoint(382, 200);
g.fillPolygon(polygon);
// Draw bottom
polygon.reset();
polygon.addPoint(300, 125);
polygon.addPoint(193, 275);
polygon.addPoint(407, 275);
g.fillPolygon(polygon);
// Draw truck
g.setColor(Color.BLACK);
g.fillRect(270, 275, 60, 250);
}
}}
错误提示:
Warning: Can't read AppletViewer properties file:
C:\Users\Khaos\.hotjava\properties Using defaults.
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at GraphicalImages.paint(GraphicalImages.java:40)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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$1200(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$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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-1" java.lang.NullPointerException
at GraphicalImages.paint(GraphicalImages.java:40)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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$1200(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$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)