1
這是我的代碼來畫多條......無法立刻
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
public class LineDrawing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createWindow();
}
});
}
private static void createWindow() {
JFrame mainFrame = new JFrame();
mainFrame.add(new myPanel());
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setBackground(Color.white);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
}
class myPanel extends JPanel {
int startX;
int startY;
int endX;
int endY;
public myPanel() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
repaint(startX, startY, endX, endY);
}
@Override
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint(startX, startY, endX, endY);
}
@Override
public void mouseClicked(MouseEvent e) {
startX = e.getX();
endX = startX;
startY = e.getY();
endY = startY;
repaint(startX, startY, endX, endY);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
repaint(startX, startY, endX, endY);
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(startX, startY, endX, endY);
repaint();
}
}
我想畫多條,同時保持前那些仍然存在的在屏幕上(框架)... 但是,當我再次點擊前行被擦或刪除了......
我試圖設計一個圖表分析的應用程序(它會告訴輸入圖形是否同構或不...
我的問題是,我無法繪製多條線,但只有一條線......
請參閱[自定義繪畫方法](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/),瞭解執行此操作的兩種常用方法。 – camickr