面板上的Java GUI绘制线不重新绘制

时间:2013-10-28 19:56:11

标签: java user-interface

问题是,我试图通过保存鼠标按下的所有点,鼠标拖动以及最终触发鼠标释放事件来在面板上绘制线条。我在Point []点中每行保存它们。这些Point []中的每一个都添加到Point []的ArrayList中。问题是,当我重新绘制线条时,它只打印出最后一行并在线条中留下很多空格,即使它们应该连接2个点,无论它们有多远。我可能错过了一些重要的东西或者对错误的理解。

有人能看出我做错了吗?

代码在这里:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Kimp extends JFrame {

private ArrayList<Point[]> pointsArray = new ArrayList<>();
private Point points[] = new Point[10000];
private int pointCounter = 0;

public Kimp () {

     panel paintArea = new panel();
     add(paintArea, BorderLayout.CENTER);
}

private class panel  extends JPanel {

    public panel () {
        HandlerClass handler = new HandlerClass();
        this.addMouseListener(handler);
        this.addMouseMotionListener(handler);
    }

    @Override
     void paintComponent(Graphics g) {
        super.paintComponent(g);
        try {




            for (Point[] p : pointsArray) {
                for(int i = 0; i < p.length; i++) {                         
                    if (p[i].x == 0) {
                       continue;
                    } else {
                        if (p[i + 1].x == 0) {
                            g.setColor(Color.BLUE);
                            g.drawLine(p[i].x, p[i].y, p[i].x, p[i].y);
                        } else {
                            g.setColor(Color.BLUE);
                            g.drawLine(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
                        }
                    } 
                } 
            }
            points = preFill(points);
        } catch (NullPointerException e) {

        }                  
    }
}

private class HandlerClass implements MouseListener , MouseMotionListener {

    @Override
    public void mouseDragged(MouseEvent e) {
        points[pointCounter++] = e.getPoint();
        //System.out.println("At mouseDragged");
        //System.out.println(pointCounter);
        //System.out.println(e.getPoint());
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //System.out.println("At mousePressed");
        //System.out.println(pointCounter);
        //System.out.println(e.getPoint());
        points[pointCounter] = e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //System.out.println("At mouseReleased: " + points.length);         

        pointCounter = 0;
        pointsArray.add(points);
        System.out.println("pointsArray: " + pointsArray.size());
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}

public static Point[] preFill (Point[] points) {
    for (int i = 0; i < points.length; i++) {
        points[i] = new Point(-999,-999);
    }
    return points;
}

}

跑步者:

import javax.swing.JFrame;

public class Runner {

    public static void main(String[] args) {

        Kimp kimp = new Kimp();
        kimp.setTitle("Kimp");
        kimp.setSize(500, 500);
        kimp.setResizable(false);
        kimp.setLocation(400, 100);
        kimp.setVisible(true);
        kimp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

编辑:更新了有关行中空格的代码(已修复)。仍然无法理解为什么它不会绘制整个行,只有最后一行......

1 个答案:

答案 0 :(得分:2)

要更正空格问题,请更改以下行:

g.drawLine(p[i].x, p[i].y, p[++i].x, p[++i].y);

为:

g.drawLine(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);