Danh mục tài liệu

Java Programming for absolute beginner- P26

Số trang: 20      Loại file: pdf      Dung lượng: 311.06 KB      Lượt xem: 18      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Programming for absolute beginner- P26:Hello and welcome to Java Programming for the Absolute Beginner. You probablyalready have a good understanding of how to use your computer.These days it’s hard to find someone who doesn’t, given the importanceof computers in today’s world. Learning to control your computer intimatelyis what will separate you from the pack! By reading this book, you learnhow to accomplish just that through the magic of programming.
Nội dung trích xuất từ tài liệu:
Java Programming for absolute beginner- P26 JavaProgAbsBeg-12.qxd 2/25/03 8:58 AM Page 458 458 there’s no mine in here, it reveals the cell and dispatches a MineCellEvent.REVEALED event. If there is a mine in here, it sets the color to col- Java Programming for the Absolute Beginner ors[MINE] and dispatches a MineCellEvent.DETONATED event. Another anony- mous inner class listens for MouseEvents. The superclass, JPRButton3D, doesn’t do anything with right mouse clicks, but this MouseAdapter does. It either flags or unflags this cell based on whether the cell is already flagged and dispatches the corresponding event. Another thing the MineCell class needed to take care of was to prevent action events from mine cells that are flagged. You don’t want to let the player click a flagged cell and blow up, right? Nor do you want the cell to be animated. You want it to be concrete that if this cell is flagged, you can’t click it with the left mouse button, period. To accomplish this, the MineCell class overrides the processMouseEvent(MouseEvent) method. If this cell is not flagged or if you’re right-clicking it, just go ahead and let the MouseEvent pass, but if this cell is flagged and you’re trying to left-click it, stop it dead in its tracks: public void processMouseEvent(MouseEvent e) { if (!flagged || e.getModifiers() == MouseEvent.BUTTON3_MASK) super.processMouseEvent(e); } Here is the full source code listing for MineCell.java: /* * MineCell * Defines one cell of the MinePatrol Game. */ import java.awt.*; import java.awt.event.*; import java.util.Vector; import jpr.lightweight.JPRButton3D; public class MineCell extends JPRButton3D { protected int contents; public final static int EMPTY = 0; public final static int MINE = 9; //These colors are indexed by the contents Color[EMPTY] is for //revealed cells and colors[MINE] is for detonated cells protected Color[] colors; protected boolean hidden, detonated, flagged; //acts as the background color when the cell becomes visible protected static Image flagImg, mineImg, explodeImg; protected Vector listeners; public MineCell() { this(EMPTY); } TEAM LinG - Live, Informative, Non-cost and Genuine!Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-12.qxd 2/25/03 8:58 AM Page 459 459 public MineCell(int contains) { super(); Chapter 12 colors = new Color[] {getBackground(), Color.blue, Color.cyan, Color.green, Color.magenta, Color.yellow, Color.orange, Color.red, Color.black, Color.red.darker().darker()}; setFont(new Font(Arial, Font.BOLD, 16)); resetContents(contains); l ...