📄 surface.java
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * Representation of a view of bubbles for Bubblet. */public class Surface extends JPanel { // Interface variables private int NROWS; private int NCOLS; private int NCOLORS; private Bubblet game; private Surface surface; private static final int HOFFSET = 10; private static final int WSIZE = 20, HSIZE = 20, GAP = 2; private int nrows; private Grid grid; private Group group; private int size; private boolean finished; private final String[] color = {"m_red.gif", "m_green.gif", "m_blue.gif", "m_brown.gif", "m_orange.gif"}; private int scoreVal; private int groupVal; /** Construct a drawing surface. */ public Surface(int m, int n, int k, final Bubblet game) { NROWS = m; NCOLS = n; NCOLORS = k; this.game = game; this.surface = this; grid = new Grid(NROWS, NCOLS, NCOLORS); grid.fill(); // grid.display(); group = null; nrows = NROWS; finished = false; this.addMouseListener(new MouseAdapter () { public void mouseClicked (MouseEvent e) { int row = (HOFFSET + NROWS*WSIZE - e.getX()) / WSIZE; int col = (NCOLS*HSIZE - e.getY()) / HSIZE; // System.out.println(row + " " + col); // test if game is over if (finished) { NewDialog dialog = new NewDialog(game, true); dialog.setVisible(true); if (dialog.choice.equals("Yes")) { game.restart(); } return; } if (0 <= row && row < NROWS && 0 <= col && col < grid.length(row)) { // select group if (group == null) { group = new Group(grid, row, col); size = group.size(); groupVal = group.score(); // System.out.println("Size = " + size); if (size == 1) { group = null; game.setGroup(0); } else { game.setGroup(groupVal); } } else if (group.contains(row, col)) { // remove group scoreVal += groupVal; game.setScore(scoreVal); remove(); surface.revalidate(); surface.repaint(); if (gameOver()) { // System.out.print("Game over! "); game.setScore(scoreVal); game.repaint(); finished = true; // Compute bonus int rem = remainder(); int bonus = (rem <= 4 ? 100-20*rem : 0); // System.out.println("Bonus = " + bonus); // Open end-of-game dialox box EndDialog dialog = new EndDialog(game, true, scoreVal, bonus); dialog.setVisible(true); if (dialog.choice.equals("RESTART")) { game.restart(); } } } else { // unselect group group = null; game.setGroup(0); } } else { // unselect group group = null; game.setGroup(0); } surface.revalidate(); surface.repaint(); }}); // System.out.println("" + NROWS + " " + NCOLS + " " + NCOLORS); revalidate(); repaint(); } /* Remove all elements of group from grid (and view) */ public void remove() { int row, col; // System.out.println("Removing group..."); // Remove bubbles from individual rows for (row = 0; row < nrows; row++) for (col = grid.length(row)-1; col >= 0; col--) if (group.contains(row, col)) grid.remove(row, col); // Remove now empty rows int shift = 0; for (row = 0; row < nrows; row++) { // System.out.print(row + "=" + grid.length(row) + " "); if (grid.length(row) == 0) shift++; else grid.set(row-shift, row); } nrows -= shift; for (row = nrows; row < NROWS; row++) { grid.setLength(row, 0); // System.out.print(row + "=" + grid.length(row) + " "); } // System.out.println("/ " + nrows + " " + shift); group = null; } /* Return true if no two adjacent bubbles have the same color */ public boolean gameOver() { int row, col; for (row = 0; row < nrows; row++) { for (col = 0; col < grid.length(row) - 1; col++) { if (grid.get(row, col) == grid.get(row, col+1)) return false; } if (row < nrows-1) { for (col = 0; col < Math.min(grid.length(row), grid.length(row+1)); col++) if (grid.get(row, col) == grid.get(row+1, col)) return false; } } return true; } /* Return number of remaining bubbles */ public int remainder() { int rem = 0; for (int row = 0; row < nrows; row++) rem += grid.length(row); return rem; } /* Display view */ public void paintComponent(Graphics g) { super.paintComponent(g); int width = WSIZE - 2*GAP; int height = HSIZE - 2*GAP; g.drawRect(HOFFSET, 0, NROWS*WSIZE, NCOLS*HSIZE); for (int col = NCOLS-1; col >= 0; col--) { int posy = (NCOLS-1-col)*HSIZE; for (int row = NROWS - 1; row >= 0; row--) { if (col < grid.length(row)) { int posx = HOFFSET + (NROWS-1-row)*WSIZE; /* // Draw bubble as filled circle g.setColor(color[grid.get(row, col)]); g.fillOval(posx+GAP, posy+GAP, width, height); */ // Draw bubble using shaded ball image String imageName = "Balls/" + color[grid.get(row, col)]; Image image = Toolkit.getDefaultToolkit().getImage(imageName); // In applet, do: image = getImage(filename-or-url); g.drawImage(image, posx+GAP, posy+GAP, this); // Draw line bounding selected group of bubbles g.setColor(Color.black); if (group != null && group.contains(row, col)) { if (col+1 >= grid.length(row) || ! group.contains(row, col+1)) g.drawLine(posx, posy, posx+WSIZE, posy); if (row+1 >= nrows || ! group.contains(row+1, col)) g.drawLine(posx, posy, posx, posy+HSIZE); if (col-1 < 0 || ! group.contains(row, col-1)) g.drawLine(posx, posy+HSIZE, posx+WSIZE, posy+HSIZE); if (row-1 < 0 || ! group.contains(row-1, col)) g.drawLine(posx+WSIZE, posy, posx+WSIZE, posy+HSIZE); } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -