//Worksheet 5 //Introduction to Events //Responding to Mouse clicks //The aim of this exercise is to make the system respond to mouse clicks and to: //1 display the co-ordinates of the mouse //2 count the number of clicks. //Modify the Demo code as follows import java.applet.*; import java.awt.*; import java.awt.event.*; public class MouseRedrawDemo extends Applet implements MouseListener { int nclicks, x, y, cx=100, cy=400, rings; Font f = new Font ( " Monoplane", Font.BOLD, 14); public void init () { addMouseListener(this); setBackground(new Color ( 204, 51, 102)); } public void paint (Graphics g) { g.setFont(f); g.setColor(new Color(112, 204, 255)); g.fillRect(x,y,400,100); g.setColor(Color.black); g.drawRect(x,y,400,100); g.drawString( " The number of mouse clicks is " + nclicks, x+50, y+30); g.drawString( " Mouse coordinates are " + x + ", " + y, x+50, y+70); for(rings=0;rings < 120; rings+=40){ g.setColor(new Color(112, 204, 255)); g.fillOval(cx+rings,cy,50,50); g.setColor(Color.black); g.drawOval(cx+rings,cy,50,50); if(rings < 80){ g.setColor(new Color(112, 204, 255)); g.fillOval(cx+(rings+25),cy+25,50,50); g.setColor(Color.black); g.drawOval(cx+(rings+25),cy+25,50,50); } } } public void mouseClicked(MouseEvent e) { x = e.getX(); y = e.getY(); nclicks++; repaint(); } public void mouseReleased(MouseEvent e) { // Method not being used } public void mousePressed(MouseEvent e) { // Method not being used } public void mouseEntered(MouseEvent e) { // Method not being used } public void mouseExited(MouseEvent e) { // Method not being used } } /* Note the size of the window is fixed by the dimensions in the html file (unlike the appletviewer where the screen can be expanded) Problems 1 Write an applet the draws five interlocking rings as shown below Include the hidden lines. This is a simple form of the Olympic rings. Add colour to the rings. 2 The actual Olypmic rings are not in a single row. Redraw the rings so that the 2nd and 4th rings are below the other three. Try to add thickness to the rings. 3 Write a program that produces the verses for the 12 days of n Christmas. */