当前位置: 代码迷 >> J2SE >> 帮忙看上代码异常,好像是空指针
  详细解决方案

帮忙看上代码异常,好像是空指针

热度:6963   发布时间:2013-02-25 00:00:00.0
帮忙看下代码错误,好像是空指针

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import static java.awt.BorderLayout.*;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

/**
 * Description:
 * <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */
public class HandDraw
{
//画图区的宽度 
private final static int AREA_WIDTH = 500;
//画图区的高度
private final static int AREA_HEIGHT = 400;
//下面的preX、preY保存了上一次鼠标拖动事件的鼠标座标
private int preX = -1;
private int preY = -1;
//定义一个右键菜单用于设置画笔颜色
PopupMenu pop = new PopupMenu();
MenuItem redItem = new MenuItem("红色");
MenuItem greenItem = new MenuItem("绿色");
MenuItem blueItem = new MenuItem("蓝色");
//定义一个BufferedImage对象
static BufferedImage image = new BufferedImage(AREA_WIDTH , AREA_HEIGHT , 
BufferedImage.TYPE_INT_RGB);
//获取image对象的Graphics
Graphics g = image.getGraphics();
private Frame f = new Frame("简单手绘程序");
private DrawCanvas drawArea = new DrawCanvas();
//用于保存需要绘制什么图形的字符串属性
private String shape = "";
//用于保存画笔颜色
private Color foreColor = new Color(255, 0 ,0);

// 画笔样式的监听事件
private ButtonStrokeAction buttonStrokeAction = null;
private Label drawWidth = null;

// 画笔端点的装饰
private Label drawCap = null;

public void init()
{  


// 画笔颜色选择器
Panel paneStroke = new Panel(new GridLayout(1, 13));

// 控制画笔样式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[1] = new Button("3");
buttonStroke[2] = new Button("5");
buttonStroke[3] = new Button("7");
buttonStroke[4] = new Button("9");
buttonStroke[5] = new Button("11");
buttonStroke[6] = new Button("13");
buttonStroke[7] = new Button("15");
buttonStroke[8] = new Button("17");
buttonStroke[9] = new Button("■");
buttonStroke[10] = new Button("●");
drawWidth = new Label("3", Label.CENTER);
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i < buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);

buttonStroke[i].addActionListener(buttonStrokeAction);
if(i <= 8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
}
if(i == 8){
paneStroke.add(drawCap);
}
}





//定义右键菜单的事件监听器。
ActionListener menuListener = new ActionListener()
  相关解决方案