如何在 Java Swing 中的两点之间拖动和画线
How to drag and draw line between 2 points in Java Swing
我想用鼠标拖动在两个 xy 坐标之间画一条线,但无法绘制任何东西
它是一个使用 swing 和 awt 的 gui 应用程序,我目前让鼠标使用鼠标事件记录初始和最终 xy 位置,这些事件存储在数组中作为 [x1,y1,x2,y2]
,但是,无法绘制线条他们之间。
drawline 是它自己调用到 main 中的函数
编辑:
说我有 2 classes;
public class mainApp extends JFrame implements ActionListener, Runnable {
private JPanel jpanel = new JPanel();
private mainApp(String title) throws HeadlessException {
super(title);
}
private void createGUI() {
// TODO
// ...
// cannot call unless is static
drawStraightLine.drawLine(jpanel);
this.pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {}
@Override
public void run() {createGUI();}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new mainApp("drawline"));
}
}
public class drawStraightLine extends JPanel {
public static void drawLine(JPanel jpanel) {
// content which conceivably works
// mouselisteners and repaint()
public void paintComponent (Graphics g){
super.paintComponent(g);
if (check != null) {
Color purple = new Color(128, 0, 128);
g.setColor(purple);
g.drawLine(x1, y1, x2, y2);
}
}
}
我无法调用 drawline(jpanel) 除非它是一个静态函数,但将其设置为静态会导致 mouselisteners 和 repaint 变得无效。
而只要 Graphics g 在函数内部而不是直接在 class 中,它将成为无效符号(忽略检查和 xy 值作为占位符)
您不需要数组,甚至 X & Y.you 可以使用 mouseEvent 的 getPoint() 方法。
试试这个:
public static void main(String args[]) throws Exception {
JFrame f = new JFrame("Draw a Line");
f.setSize(300, 300);
f.setLocation(300, 300);
f.setResizable(false);
JPanel p = new JPanel() {
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor("put your color here");
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
};
f.add(p);
f.setVisible(true);
}
我想用鼠标拖动在两个 xy 坐标之间画一条线,但无法绘制任何东西
它是一个使用 swing 和 awt 的 gui 应用程序,我目前让鼠标使用鼠标事件记录初始和最终 xy 位置,这些事件存储在数组中作为 [x1,y1,x2,y2]
,但是,无法绘制线条他们之间。
drawline 是它自己调用到 main 中的函数
编辑: 说我有 2 classes;
public class mainApp extends JFrame implements ActionListener, Runnable {
private JPanel jpanel = new JPanel();
private mainApp(String title) throws HeadlessException {
super(title);
}
private void createGUI() {
// TODO
// ...
// cannot call unless is static
drawStraightLine.drawLine(jpanel);
this.pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {}
@Override
public void run() {createGUI();}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new mainApp("drawline"));
}
}
public class drawStraightLine extends JPanel {
public static void drawLine(JPanel jpanel) {
// content which conceivably works
// mouselisteners and repaint()
public void paintComponent (Graphics g){
super.paintComponent(g);
if (check != null) {
Color purple = new Color(128, 0, 128);
g.setColor(purple);
g.drawLine(x1, y1, x2, y2);
}
}
}
我无法调用 drawline(jpanel) 除非它是一个静态函数,但将其设置为静态会导致 mouselisteners 和 repaint 变得无效。
而只要 Graphics g 在函数内部而不是直接在 class 中,它将成为无效符号(忽略检查和 xy 值作为占位符)
您不需要数组,甚至 X & Y.you 可以使用 mouseEvent 的 getPoint() 方法。 试试这个:
public static void main(String args[]) throws Exception {
JFrame f = new JFrame("Draw a Line");
f.setSize(300, 300);
f.setLocation(300, 300);
f.setResizable(false);
JPanel p = new JPanel() {
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor("put your color here");
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
};
f.add(p);
f.setVisible(true);
}