繁体   English   中英

我如何在通过鼠标单击创建的两点之间绘制一条线

[英]How can i draw a line between two points created as a result of a mouse click

我正在尝试编写一个程序,将两个点之间用一条线连接起来。 首先,我想说的是,我知道drawline()函数,但不允许使用它(这是硬件分配)。 所以我的问题是,我设法编写代码来连接两个点,但是由于某种原因,每当我运行程序时,(0,0)像素始终处于打开状态,并且在我的第一个鼠标上单击从(0,0)到首次点击坐标。 有人可以帮我弄清楚如何在不打开(o,o)像素的情况下运行该应用程序吗? 我想做的另一件事是将每一行分开(两次单击=行,另外两次单击=另一行),这也是我在努力实现的事情。

希望我的解释足够好,任何帮助都会很棒! 这是我的代码:

package com.mycompany;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MousePanel extends JPanel implements MouseListener,ActionListener{

    private int x,y,x2,y2,a=1;
    public MousePanel(){
        super();
        addMouseListener(this);   
    }

    public void paint(Graphics g){
        int w = x2 - x ;
        int h = y2 - y ;
        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
        if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
        if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
        if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
        int longest = Math.abs(w) ;
        int shortest = Math.abs(h) ;
        if (!(longest>shortest)) {
            longest = Math.abs(h) ;
            shortest = Math.abs(w) ;
            if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
            dx2 = 0 ;            
        }
        int numerator = longest >> 1 ;
        for (int i=0;i<=longest;i++) {
            g.fillRect(x,y,1,1);
            numerator += shortest ;
            if (!(numerator<longest)) {
                numerator -= longest ;
                x += dx1 ;
                y += dy1 ; 
            } else {
                x += dx2 ;
                y += dy2 ;
            }
        }
    }

    public void mouseClicked(MouseEvent mouse){     
            x=x2;
            y=y2;
            x2 = mouse.getX();
            y2 = mouse.getY();
            repaint();
        }

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

    public static void main(String arg[]){
        JFrame frame = new JFrame("MousePanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,640);

        MousePanel panel = new MousePanel();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

尝试在代码中使用计数器以检查“点击次数” 第一次点击表示起点,第二次点击表示终点。 如果单击%2!= 0,则根据该点设置初始坐标,否则获取第二个坐标并绘制线。 我想这足以让您继续。 它是您的任务,所以请尝试解决:)

如果您只是想画一条线,则可以简单地使用drawLine()方法,而不是将矩形点排列成一条线。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MousePanel extends JPanel implements MouseListener,ActionListener{

    private int x,y,x2,y2,a=1;
    private int count=0;

    public MousePanel(){
        super();
        addMouseListener(this);   
    }

    public void paint(Graphics g){

        if(count==2){
            g.drawLine(x, y, x2, y2);

        count=0;
        x=0;
        y=0;
        x2=0;
        y2=0;
        }
    }

    public void mouseClicked(MouseEvent mouse){   

        count++;

        if(count==1){
            x=mouse.getX();
            y=mouse.getY();
        }

        if(count==2){
            x2 = mouse.getX();
            y2 = mouse.getY();
        }

        repaint();
        }

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

    public static void main(String arg[]){
        JFrame frame = new JFrame("MousePanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,640);

        MousePanel panel = new MousePanel();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

但是,您的代码也没有错。 但是您需要添加计数器来存储点值,如上面的代码所示。

只需替换mouseClicked()方法即可。 正如你已经定义的变量a用它来计算出该按钮的点击次数。

public void mouseClicked(MouseEvent mouse) {
    if (a == 1) {
        a = 0;
        x = x2 = mouse.getX();
        y = y2 = mouse.getY();
    } else {
        a = 1;
        x = x2;
        y = y2;
        x2 = mouse.getX();
        y2 = mouse.getY();
        repaint();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM