Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

This lab involves completion of your MineSweeper classfrom last week's lab.

In order to get your marks for this week's lab, you must submit thefollowing files by the deadline. If you have written additionalclasses, then submit these as well. All the classes you submit will berecompiled with the SimpleCanvas class.

  • MineSweeper.java

Make sure that you submit exactly the file above; do not submit.zip files or anything else. As always, make sure thatyou have checked that your methods have exactly thesame names and capitalization as the specification or themarker will be unable to mark your program correctly.

(Optional) Add Mouse Action

During the execution of a Java program that contains graphical userinterface components a large number of things are happening"behind the scenes". In particular, whenever the user manipulates the keyboard or the mouse a variety of events aregenerated, and sent to the object where the event occurred.

Java's "registration model" allows any object that is interestedto register to be notified when specific events occur. Then whenan event occurs, all of the registered "listeners" are informedand can take suitable action.

Let's see how all this will work with the MineSweeper.

  1. Declare that MineSweeper is a MouseListener

    Firstly we have to indicate to the compiler that the MineSweeperobject is going to want to process mouse events - in other words, that itwants to become a MouseListener. The full name of the MouseListener class is java.awt.event.MouseListenerso it is best to start by putting a suitable import statementat the top of the file.

    import java.awt.event.*;

    Then alter the source code for MineSweeper to say

    public class MineSweeper implements MouseListener

    The phrase implements MouseListener says to the compiler that this class promises to implement the special methodsthat characterize a MouseListener.

  2. Implement the five special methods

    The five special methods that define a MouseListener aremouseClicked, mousePressed, mouseEntered,mouseExited and mouseReleased. These will becalled when the appropriate mouse event occurs on the component thatthis object is "listening" to.

    To satisfy the compiler, these methods need not do anything, but they must be present. Later we can supply real code only for the ones wewant to use.

    public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}
  3. Download the latest SimpleCanvas

    A new version of is available that allowsmouse listeners to register as interested in mouse events. Downloadthis and replace your existing SimpleCanvas.

  4. Register the MineSweeper object as being interested in mouse events that occur on the SimpleCanvas

    Somewhere in the constructor for the MineSweeper you needto register the MineSweeper with the SimpleCanvasthat it contains.

    sc.addMouseListener(this);

    This essentially says that this (ie the MineSweeper)is interested in "listening to" any mouse events that mightoccur on the SimpleCanvas.

    The way this is implemented is that when a mouse event occurs on theSimpleCanvas, then every registered listener isautomatically notified by having the appropriate method called. So, whenever the userclicks a mouse in the SimpleCanvas area, the canvas checksall its mouse listeners, and one-by-one calls each of their mouseClicked methods. The information about the location and other details of the event are passed to the listeners as the parameterof type MouseEvent.

  5. Replace the mouseClicked method to do somethingsensible.

    When the mouse is clicked, what does the MineSweeper wantto do? Firstly it needs to translate from the location of the mouseclick to determine which cell it occurred in. The MouseEventobject has two methods getX() and getY() whichhold the location of the pointer when the mouse was clicked.

    public void mouseClicked(MouseEvent e) {  int x = e.getX();  int y = e.getY();  // calculate the cell number}

    In addition, the MouseEvent has a method getButtonwhich allows you to determine which button was clicked. So if we copythe professional MineSweeper, we might consider a clickwith button 1 (the left-button) to be a command to "dig" anda click with button 3 (the right-button) to be a command to "mark".

    Of course, once this method has located the cell co-ordinates and decidedwhich method to call, it would then simply make the appropriate dig or mark call, and the existing code woulddo all the necessary updates.

In this fashion, you can add a mouse interface to the program withoutaltering any of the underlying logic.