Page 229 - CTS - CSA TP - Volume 2
P. 229
COMPUTER SOFTWARE APPLICATION - CITS
private ArrayList<Shape> lines = new ArrayList<>();
private Shape currentLine;
private int startX, startY;
public LineDrawingGUI() {
setTitle(“Line Drawing”);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel drawingPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawLines(g);
}
};
drawingPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
createLine(startX, startY, e.getX(), e.getY());
repaint();
}
});
add(drawingPanel);
}
private void drawLines(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
214
CITS : IT & ITES - Computer Software Application - Exercise 119 CITS : IT & ITES - Computer Software Application - Exercise 119