Java尝试捕获错误?

时间:2015-11-20 14:27:02

标签: java try-catch

所以我正在研究一个项目的代码,我想让我的代码成一条线,然后再换一条新线7次。问题是我的程序尽管在for循环运行之间有延迟,但我一次完成所有7个。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawMain extends JComponent{
private static class Line{
    final int x1; 
    final int y1;
    final int x2;
    final int y2;   
    final Color color;
    public Line(int x1, int y1, int x2, int y2, Color color) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.color = color;
    }               
}
private final LinkedList<Line> lines = new LinkedList<Line>();
public void addLine(int x1, int x2, int x3, int x4) {
    addLine(x1, x2, x3, x4, Color.black);
}
public void addLine(int x1, int x2, int x3, int x4, Color color) {
    lines.add(new Line(x1,x2,x3,x4, color));        
    repaint(); 
}
public void clearLines() {
    lines.clear();
    repaint();
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Line line : lines) {
        g.setColor(line.color);
        g.drawLine(line.x1, line.y1, line.x2, line.y2);
    }
}
public static void main(String[] args) {
    JFrame testFrame = new JFrame(); //create new frame called testFrame
    testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//remove testFrame on close
    final DrawMain comp = new DrawMain(); //???
    comp.setPreferredSize(new Dimension(320, 200));//set size of testframe
    testFrame.getContentPane().add(comp, BorderLayout.CENTER);//Testfame new mid frame for drawing
    JPanel buttonsPanel = new JPanel();//creates buttons panel
    JButton newLineButton = new JButton("New Line");//new line button creator
    JButton clearButton = new JButton("Clear");//clear button creator
    buttonsPanel.add(newLineButton);//add button
    buttonsPanel.add(clearButton);//add button
    testFrame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);//makes sourthern frame with buttons

    newLineButton.addActionListener(new ActionListener(){//Searches for button click

        public void actionPerformed(ActionEvent e) {     //random action start on button click
       //   for(int i = 0; i <= 7; i++){                 // for loop runs line creator 7 times
            int x1 = (int) (Math.random()*320);          //Random
            int x2 = (int) (Math.random()*320);          //Random
            int y1 = (int) (Math.random()*200);          //Random
            int y2 = (int) (Math.random()*200);          //Random
        Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());//random color         
        for(int i = 0; i <= 7; i++) {
            comp.addLine(x1 + i * 5, y1 + i * 5, x2, y2, randomColor);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        }                                                
    });                                                  
    clearButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Thread.sleep(1000);                     
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            comp.clearLines();
        }
    });
    testFrame.pack();
    testFrame.setVisible(true);
    }
}

0 个答案:

没有答案