import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; @SuppressWarnings("serial") public class Tens extends JPanel implements MouseListener, MouseMotionListener { private ArrayList tiles, selected; private String message; public static final int SIZE = 50; public static final int WIDTH = 500; public static final int HEIGHT = 500; public static final int GAP =3; private Font font; public void resetTiles() { int n = 40 ; tiles = new ArrayList(n); selected = new ArrayList(2); message ="Remove all " + n + " tiles by clicking 2 that add up to 10, anywhere else to reset"; for (int i = 0; i < n; i++) { // construct a new tile and add it to the tiles ArrayList // YOUR CODE HERE; } repaint(); } public Tens() // constructor { message = ""; font = new Font("NewTimesRoman", Font.BOLD, 12); resetTiles(); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { setBackground(new Color(125,220,200) ); super.paintComponent(g); // Adjust location of boxes according to the current panel size int width = this.getWidth() - SIZE; for( int i = 0; i < tiles.size(); i++ ) { Tile t = tiles.get(i); int x = (i) % (width/SIZE); //column int y = (i) / (width/SIZE); //row t.setLocation(SIZE/2 + x*SIZE+x*GAP, SIZE/2 + y*SIZE+y*GAP); t.setColumn(x); t.setRow(y); } for( Tile t:tiles) { t.draw(g); } g.setColor(Color.BLACK); g.setFont(font); g.drawString(message, 20, 20); } public static void main(String[] args) { JFrame window = new JFrame(); window.setSize(WIDTH, HEIGHT); Container c = window.getContentPane(); c.add(new Tens()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } /** * Precondition: there must be 2 selected tiles * Postcondition: All tiles are off and selected is empty */ public void remove() { Tile first = selected.remove(0); Tile second = selected.remove(0); // Now we need to have to see if there are two selected, // that if the tiles add up to 10, they are removed, // and if they do not, message will have a error message // and all the tiles are tuned off. /// /// YOUR CODE HERE /// repaint(); } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); for(Tile t:tiles) { if (t.contains(x,y)) { // This tile has bee clicked. // If this the seconded tile, turn it on, and see if is a legal move (ie they add up to 10) // otherwise turn the tile on or off, the user my be deselecting a tile int numSelected = selected.size(); if( numSelected == 0 ) { t.on(); selected.add(t); } else if(numSelected == 1) { if(t ==selected.get(0) ) { selected.remove(t); t.off(); } else { selected.add(t); remove(); } } repaint(); return; } } // If user didn't click on a tile, we reset the game resetTiles(); repaint(); } public void mousePressed(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { // hilight a tile if mouse is hovering int x=e.getX(); int y=e.getY(); //message = "("+x+", "+y+")"; for(Tile t: tiles) t.setHighlight(t.contains(x, y)) ; repaint(); } }