Java基于鼠标点击绘制三种形状?

时间:2014-03-03 19:12:51

标签: java swing jpanel awt mouseevent

我刚接触Java并且在使用鼠标侦听器事件时遇到了一些麻烦。我想要做的是在点击上画一个点,然后在点击时绘制一条从该点延伸的线,最后点击的最后一点连接成三角形。截至目前,我正在努力让点和线工作。我认为我现在的方式很接近;在顶角绘制圆圈而不是用户点击的位置,但在点击时,将从圆圈所在的位置绘制一条线。尝试使用布尔值来决定什么时候应该绘制但是不成功。谢谢你的帮助。

主要类

package TriangleDraw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TriangleDrawMain {

    public static void main(String[] args) {
        //create a frame or window

       JFrame frame = new JFrame();
       //set window size
       frame.setSize(500, 500);
       //set the title
       frame.setTitle("Triangle Draw");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //add panel to frame and make it visible
       MouseComponent component = new MouseComponent();
       frame.add(component);
       frame.setVisible(true);

    }
}

绘图类

package TriangleDraw;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.geom.*;

public class MouseComponent extends JPanel implements MouseListener{
        boolean drawPoint = true;
        boolean drawLine = false;
        boolean drawTriangle = false;

    public MouseComponent(){
        super();
        pointX = 0;
        pointY = 0;   
        oldX = 0;
        oldY = 0;
        addMouseListener(this);
    }

    int pointX, pointY, oldX,oldY;

        @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(drawPoint == true){
        g.drawOval(pointX,pointY,10,10);
        drawPoint = false;
        drawLine = true;
        oldX = pointX;
        oldY = pointY;
        }
        if(drawLine == true){
            g.drawLine(pointX, pointY, oldX, oldY);
        }


    }

    public void mouseClicked(MouseEvent mouse){

        pointX = mouse.getX();
        pointY = mouse.getY();

        repaint();
    }
    public void mouseEntered(MouseEvent mouse){ }   
    public void mouseExited(MouseEvent mouse){ }
    public void mousePressed(MouseEvent mouse){ }
    public void mouseReleased(MouseEvent mouse){ }
}

2 个答案:

答案 0 :(得分:1)

我不会在paintComponent中更改状态,你不知道何时会调用它(你在哪里分配oldX和oldY)。我建议创建两个Point对象,并在每个其他mouseClicked事件中切换两个点对象。

如果在mouseClicked之间多次调用paintComponent,则最终将oldX和OldY等于pointX和pointY。

答案 1 :(得分:0)

将drawPoint作为false启动。并更改变量名称以保存点。

然后,您在点击中想要这样的内容:

public void mouseClicked(MouseEvent mouse){
    if(!drawPoint)
    {
        pointX = mouse.getX();
        pointY = mouse.getY();
        drawPoint = true;
    }    
    else if(!drawLine)
    {
        lineX = mouse.getX();
        lineY = mouse.getY();
        drawLine = true;
    }
    repaint();
}

然后你的画很简单:

public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(drawLine){
            g.drawLine(pointX, pointY, lineX, lineY);
        }
        else if(drawPoint){
            g.drawOval(pointX-5,pointY-5,10,10);
        }
}

在绘制循环中,首先检查drawLine,然后在mouseClicked中首先检查drawPoint,这一点非常重要。

一旦你掌握了这里发生的事情,你可以自己尝试一下drawTriangle!