我试图做一个程序,将连接两个点,我点击我的JPanel。我试图用线连接两点。每当我点击使用鼠标并且数值中没有错误时,我显示my(x1,y1)和(x2,y2)坐标的值。但是当显示该行时,该行似乎不遵循我指定的坐标,而是在不同的位置输出并且失真。大多数线条似乎被某些东西切割,我认为这是由线条创建的矩形,因为我使用了setBounds()。另外,我在我的paintComponent函数中添加了一个System.out.println(“”),我注意到它多次打印(每次点击后增加1),即使它应该只打印一次。谁能帮我这个?谢谢!paintComponent()输出错误
这里有两个,其向错误的类:
CLASS#1:
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Arch. Don Saborrido
*/
public class twixtBoard extends JPanel implements java.awt.event.MouseListener{
int x1 = 0, x2, y1 = 0, y2;
DrawLine line;
//DrawLine line;
public twixtBoard(){
//requestFocus();
setLayout(null);
setVisible(false);
setBounds(0,0,600,450);
setOpaque(false);
setFocusable(false);
addListener();
}
public void addListener(){
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if (x1 == 0 || y1 == 0){
x1 = e.getX();
y1 = e.getY();
System.out.println(x1 + " " + y1);
}
else{
x2 = e.getX();
y2 = e.getY();
//System.out.println(x2 + " " + y2);
line = new DrawLine(x1, y1, x2, y2);
line.setBounds(x1, y1, x2, y2);
System.out.println("" + line.getLocation());
//line.setOpaque(false);
add(line);
x1 = x2;
y1 = y2;
repaint();
}
}
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
///throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
CLASS#2(油漆类):
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Arch. Don Saborrido
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
//import java.awt.geom.Line2D;
import javax.swing.JPanel;
public class DrawLine extends JPanel{
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
//Line2D line;
Stroke[] s = new Stroke[] {new BasicStroke(10.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)};
//new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
//new BasicStroke(25.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER)
GeneralPath path = new GeneralPath();
public DrawLine(int start_x, int start_y, int end_x, int end_y){
x1 = start_x;
y1 = start_y;
x2 = end_x;
y2 = end_y;
System.out.println(x1+ " " + y1+ " " + x2+ " " + y2+ " ");
}
@Override
protected void paintComponent(Graphics g) {
System.out.println("entered paint");
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(s[0]);
path.moveTo(x1,y1);
System.out.println("x1 = " + x1 + " y1 = " + y1);
path.lineTo(x2,y2);
System.out.println("x2 = " + x2 + " y2 = " + y2);
System.out.println("" + path.getBounds2D());
g2.draw(path);
//line = new Line2D.Float(x1, y1, x2, y2);
//if(x1 != x2 && y1 != y2)
//g2.draw(line);
}
}
crossposted http://www.daniweb.com/software-development/java/threads/407781 – mKorbel
请学习java命名约定并坚持他们 – kleopatra