Page 227 - CTS - CSA TP - Volume 2
P. 227
COMPUTER SOFTWARE APPLICATION - CITS
setTitle(“Circle Drawing”);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel drawingPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawCircles(g);
}
};
drawingPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
centerX = e.getX();
centerY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
int radius = (int) Math.sqrt(Math.pow(e.getX() - centerX, 2) + Math.pow(e.getY() - centerY, 2));
createCircle(centerX, centerY, radius);
repaint();
}
});
add(drawingPanel);
}
private void drawCircles(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
for (Shape circle : circles) {
g2d.draw(circle);
}
}
private void createCircle(int centerX, int centerY, int radius) {
212
CITS : IT & ITES - Computer Software Application - Exercise 119 CITS : IT & ITES - Computer Software Application - Exercise 119