簡體   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