我正在尝试编写一个程序,用它们之间的一条线连接两个点。首先,我想说,我知道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
}
}
如果你的意图只是画一条线,那么你可以简单地使用 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();
}
}
代码中使用计数器来检查"点击次数"。第一次点击表示初始点,第二次点击表示终点。如果单击%2!=0,则根据该点设置初始坐标,否则获得第二个坐标并绘制线条。我想这足以让你继续。这是你的任务,所以试着弄清楚:)