您好我想将图表打印到applet窗口中,但在重新调整窗口大小之前图表是不可见的。
这是我的问题:我的代码有什么问题,我该如何解决这个问题?另外,我收到了 以下行上的空指针异常
loop:for(int i=0; i <count.length; i++){
这是我的变量的问题,但我无法在此阶段修复它。我是一个初学者。
public class Simple extends Applet implements ActionListener, KeyListener {
Button analyze, reset, load;
TextArea input, output;
Panel panel, drawArea;
JFileChooser fc;
FileReader readFile;
BufferedReader bufferedFile;
String line = "";
int count[];
int length;
int i ;
int x = 550;
int y = 320;
public void init() {
setSize(800,600);
setLayout (new BorderLayout());
panel = new Panel(new GridLayout(0,3));
panel.add(analyze = new Button("Analyze"));
panel.add(load = new Button("Load"));
panel.add(reset = new Button("Reset"));
add(panel,BorderLayout.SOUTH);
panel = new Panel(new GridLayout(2,1));
panel.setPreferredSize(new Dimension(200,0));
panel.add(input = new TextArea());
panel.add(output = new TextArea());
add(panel,BorderLayout.WEST);
/*
panel.add(drawArea = new Panel());
add(drawArea,BorderLayout.CENTER);
drawArea.getGraphics();
*/
analyze.addActionListener(this);
analyze.setEnabled(false);
load.addActionListener(this);
reset.addActionListener(this);
input.addKeyListener(this);
output.setEditable(false);
fc = new JFileChooser();
setBackground(Color.cyan);
}
public void start() {
System.out.println("starting...");
}
public void stop() {
System.out.println("stopping...");
}
public void destroy() {
System.out.println("preparing to unload...");
}
@Override
public void keyPressed(KeyEvent e) {
String inputGetText = input.getText();
if (inputGetText != null) {
analyze.setEnabled(true);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public void actionPerformed(ActionEvent event){
if (event.getSource()==analyze) {
input.setEditable(false);
analyze.setEnabled(false);
wordProcess();
}
if (event.getSource()==load){
fc.showOpenDialog(load);
File file = fc.getSelectedFile();
try {
readFile = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedFile = new BufferedReader(readFile);
try {
String nLine = bufferedFile.readLine();
while (nLine!=null){
line+=nLine + "\n";
nLine = bufferedFile.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
input.setText(line);
analyze.setEnabled(true);
}
if (event.getSource()==reset){
input.setText(null);
input.setEditable(true);
output.setText(null);
//x = 50;
line = "";
analyze.setEnabled(false);
}
}
public void wordProcess(){
for(String retva1: input.getText().replaceAll("[^a-zA-Z]+"," ").split(" "))
if(length < retva1.length())
length = retva1.length();
count = new int [length+1];
for(String retva2: input.getText().replaceAll("[^a-zA-Z]+"," ").split(" ")){
count[retva2.length()]++;
}
String show = "";
float average = 0;
float sum = 0;
float sum2 = 0;
loop: for(int i = 0; i <count.length; i++){
if (count[i]==0||i==0){
continue loop;
}else{
show += (count[i]+" words of length "+i) + "\n";
sum2 +=i*count[i];
average +=count[i];
}
output.setText("There are: "+"\n"+show);
}
sum = sum2/average;
String s2 = new Float(sum2).toString();
String a = new Float(average).toString();
output.append("\n"+"The mean word length: "+sum);
output.append("\n"+s2);
output.append("\n"+a);
}
public void paint(Graphics g) {
g.drawLine(250, 50, 250, 320);
g.drawLine(250, 320, 520, 320);
g.drawLine(250, 340, 250, 610 );
g.drawLine(250, 610, 520, 610);
loop: for(int i=0; i <count.length; i++){
if (count[i]==0||i==0){
continue loop;
}else{
int wordFreqency = count[i];
int wordLenght = i;
g.fillRect( x , y-(wordFreqency*5), 10, wordFreqency*5);
String scaleIndexX = new Integer(i).toString();
g.drawString(scaleIndexX, x, 335);
String scaleIndexY = new Integer(count[i]).toString();
g.drawString(scaleIndexY, x, y-(wordFreqency*5+5));
System.out.println(x);
int move = 0;
if (move<wordLenght){
x+=20;
}
}
}
}
}
答案 0 :(得分:1)
count数组变量最初为null,仅当actionPerformed方法调用wordProcess()
时才在按钮上创建。所以你的绘图例程试图使用null变量。解决方案:在尝试使用if进行绘制之前,使用if控件检查count是否为null。
if (count != null) {
// can use count here
}
顺便说一句:你使用Swing而不是AWT会好得多。
此外,在获得绘制图表所需的所有信息后,请在actionPerformed方法中调用repaint()
。