Help!刚写了个画图程序,运行出错!大家帮我看下出了什么问题
主程序:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2007 </p>
* <p> Company: </p>
* @author not attributable
* @version 1.0
*/
public class WallFrame extends Applet implements ActionListener,MouseListener,MouseMotionListener{
Point start;
Point last;
int flag;
int i;
private Line line1;
private rectangle rectangle1;
private Graphics2D g2D;
private boolean isStandalone = false;
JPanel jPanel1 = new JPanel();
JPanel jPanel3 = new JPanel();
//new GridLayout(6,0,5,5)
JPanel jPanel4 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JButton newfile = new JButton( "新建 ");
JButton open = new JButton( "打开 ");
JButton save = new JButton( "保存 ");
JButton saveAs = new JButton( "另存为 ");
JButton exit = new JButton( "退出 ");
JButton line = new JButton( "直线 ");
JButton rectangle = new JButton( "矩形 ");
JButton color = new JButton( "颜色 ");
//GridBagLayout gridBagLayout1 = new GridBagLayout();
GridLayout gridLayout1 = new GridLayout();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
FlowLayout flowLayout1 = new FlowLayout();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
//Initialize the applet
public void init() {
//jPanel1 f=new jPanel1( "paint ");
//Container f=jPanel1.getcont
jPanel1.addMouseListener(new WallFrame());
jPanel1.addMouseMotionListener(new WallFrame());
}
public void paint(Graphics g) {
Graphics2D g2D=(Graphics2D)g;
super.paint(g);
line1.draw(g2D);
rectangle1.draw(g2D);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals( "line "))
flag=1;
else if(e.getActionCommand().equals( "rectangle ")){
flag=2;
}
}
public void mousePressed(MouseEvent e) {
start=e.getPoint();
int modifier=e.getModifiers();
if((modifier&e.BUTTON1_MASK)!=0 ){
g2D=(Graphics2D)getGraphics();
//g2D.setXORMode(getBackground());
}
}
public void mouseDragged(MouseEvent e) {
last=e.getPoint();
int modifier=e.getModifiers();
if((modifier&e.BUTTON1_MASK)!=0 ){
if(flag==1)
{
line1.draw(g2D);
line1.modify(start,last);
}
//rectangle1.modify(start,last);
else if(flag==2){
//line1.draw(g2D);
rectangle1.draw(g2D);
rectangle1.modify(start,last);
}
}
}
public void mouseReleased(MouseEvent e){
int modifier=e.getModifiers();
/*if(line1!=null&&rectangle1!=null){
line1=null;
rectangle1=null;
}*/
if(g2D!=null){
g2D.dispose();
g2D=null;
}
start=last=null;
}
public void mouseMoved(MouseEvent e) {
Point currentCursor = e.getPoint();
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public WallFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(flowLayout1);
this.setLayout(borderLayout2);
jPanel1.setBackground(Color.white);
jLabel1.setText( "X: ");
jLabel2.setText( "Y: ");
jTextField1.setText( " ");
newfile.addActionListener(new WallFrame_newfile_actionAdapter(this));
open.addActionListener(new WallFrame_open_actionAdapter(this));
save.addActionListener(new WallFrame_save_actionAdapter(this));
saveAs.addActionListener(new WallFrame_saveAs_actionAdapter(this));
rectangle.addActionListener(new WallFrame_rectangle_actionAdapter(this));
line.addActionListener(new WallFrame_line_actionAdapter(this));
this.add(jPanel3, BorderLayout.EAST);
jPanel3.setLayout(new GridLayout(6,2,5,5));
//jpanel1 f=new jPanle11( "paint ");
jPanel3.add(jLabel1, null);
jPanel3.add(jTextField1, null);
jPanel3.add(jLabel2, null);
this.add(jPanel4, BorderLayout.NORTH);
jPanel4.add(newfile, null);
jPanel4.add(open, null);
jPanel4.add(save, null);
jPanel4.add(saveAs, null);
jPanel4.add(rectangle, null);
jPanel4.add(line, null);
jPanel4.add(exit, null);
jPanel4.add(color, null);
this.add(jPanel1, BorderLayout.CENTER);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information ";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
void newfile_actionPerformed(ActionEvent e) {
i=1;
}
void open_actionPerformed(ActionEvent e) {
i=2;
}
void save_actionPerformed(ActionEvent e) {
i=3;
}
void saveAs_actionPerformed(ActionEvent e) {
i=4;
}
void rectangle_actionPerformed(ActionEvent e) {
i=5;
}
void line_actionPerformed(ActionEvent e) {
i=6;
}
}
class WallFrame_newfile_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_newfile_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.newfile_actionPerformed(e);
}
}
class WallFrame_open_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_open_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.open_actionPerformed(e);
}
}
class WallFrame_save_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_save_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.save_actionPerformed(e);
}
}
class WallFrame_saveAs_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_saveAs_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.saveAs_actionPerformed(e);
}
}
class WallFrame_rectangle_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_rectangle_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.rectangle_actionPerformed(e);
}
}
class WallFrame_line_actionAdapter implements java.awt.event.ActionListener {
WallFrame adaptee;
WallFrame_line_actionAdapter(WallFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.line_actionPerformed(e);
}
}
[解决办法]
辅助类:import java.awt.*;
import java.awt.geom.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2007 </p>
* <p> Company: </p>
* @author not attributable
* @version 1.0
*/
public class Line {
public Line(Point start,Point end){
line=new Line2D.Double(start,end);
}
public void draw(Graphics2D g2D){
g2D.draw(line);
}
public void modify(Point start,Point last){
line.x2=last.x;
line.y2=last.y;
line.x1=start.x;
line.y2=start.y;
}
private Line2D.Double line;
}
辅助类:
package huitu;
import java.awt.*;
import java.awt.geom.*;
/**
* <p> Title: </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2007 </p>
* <p> Company: </p>
* @author not attributable
* @version 1.0
*/
public class rectangle {
public rectangle(Point start,Point end){
rectangle = new Rectangle2D.Double(
Math.min(start.x,end.x),Math.min(start.y,end.y) ,
Math.abs(start.x - end.x), Math.abs(start.y - end.y));
}
public void draw(Graphics2D g2D){
g2D.draw(rectangle);
}
public void modify(Point start,Point last){
rectangle.x=Math.min(start.x,last.x);
rectangle.y=Math.min(start.y,last.y);
rectangle.width=Math.abs(start.x-last.x );
rectangle.height=Math.abs(start.y-last.y);
}
private Rectangle2D.Double rectangle;
}
运行报错:
java.lang.NullPointerException
at huitu.WallFrame.paint(WallFrame.java:71)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
[解决办法]
你能说一下你要实现什么样的效果
[解决办法]
-.-这么长的代码.要找只啄木鸟来抓错了
[解决办法]
看样子是一个很简单的NullPointerException,lz用debug进去看看吧。
java.lang.NullPointerException
at huitu.WallFrame.paint(WallFrame.java:71)
[解决办法]
单不走调啊,大家上班都忙成马
[解决办法]
我在paint方法里加个g2D.drawLine(dimention d),连这种都画不出来,还别说响应鼠标按扭事件了,
[解决办法]
代码太长了,帮你顶一下!!!