import java.awt.*; public class MagCan extends Panel { // Setup Control Ints int NUM_ROWS = 3, NUM_COLS = 3, BOX_WIDTH = 25, BOX_HEIGHT = 25, BOX_GAP = 5, STR_X_GAP = 15, STR_Y_GAP = 0, PUZ_GAP = 30, X_INSET = 50, Y_INSET = 25, BSTR_X = 8, BSTR_Y = 20, B2STR_X = 3; int NUM_TOTAL = NUM_ROWS * NUM_COLS; int MARG_ERR = (int)Math.floor(.35 * BOX_WIDTH); int X_SIZE = size().width; int Y_SIZE = size().height; int rows[] = new int[5]; int cols[] = new int[5]; int diags[] = new int[2]; boolean goal = false; Color goal_color = new Color(1000,20000,60535); Font goal_font = new Font("TimesRoman", Font.BOLD, 50); Font box_font = new Font("TimesRoman", Font.BOLD, 18); Font str_font = new Font("TimesRoman", Font.PLAIN, 14); Font top_font = new Font("TimesRoman", Font.BOLD, 36); FontMetrics fm = getFontMetrics(box_font); FontMetrics fm2 = getFontMetrics(str_font); String longest_string = "Row 9: 999"; String row_str[] = {"Row 1: ","Row 2: ","Row 3: ","Row 4: ","Row 5: "}; int temp, str_x, str_y, x_diff, y_diff; int grid_locs[][] = new int[25][3]; int boxes[][] = new int[25][3]; int currbox, row, xdiff, ydiff, inbox = 0; Color fgmagcan=Color.black,bgmagcan=Color.white, numbers=Color.black, boxcolor=Color.yellow, outline=Color.red; MagPuz outerparent; Point curr; MagCan(MagPuz target) { row = 5; // So can contact parent this.outerparent = target; // Set Background setBackground(bgmagcan); // This boxes[] contains info about where each box is printed // on the screen. Each one contains an x-pos, y-pos, and // the number in the box. This routine sets initial positions. update_level(3); // Setup the first current box currbox = 0; } // This handles the clicking. It takes as arguments where you click. // It first runs through the boxes to find out which box you clicked in. // It returns this box as the current box. public boolean mouseDown(Event evt, int x, int y) { for (int i = 0; i <= NUM_TOTAL; i++) { if (x > boxes[i][0] && x < boxes[i][0]+BOX_WIDTH && y > boxes[i][1] && y < boxes[i][1]+BOX_HEIGHT) { currbox = i; xdiff = (x-boxes[currbox][0]); ydiff = (y-boxes[currbox][1]); curr = new Point(x,y); inbox = 1; break; } else inbox = 0; } return true; } public boolean mouseDrag(Event evt, int x, int y) { if(inbox == 1) { curr = new Point(x,y); repaint(); } return true; } public boolean mouseUp(Event evt, int x, int y) { if(inbox == 1){ boxes[currbox][0]=(curr.x-xdiff); boxes[currbox][1]=(curr.y-ydiff); curr = null; // Check if a box has been moved into/out of a grid_loc in_grid_box(); // Add up current grid_locs addup(); // Check it its a goal_state goal_state(); repaint(); } return true; } void in_grid_box() { for(int i=0;i grid_locs[i][0]-MARG_ERR) && (boxes[currbox][1] < grid_locs[i][1]+MARG_ERR && boxes[currbox][1] > grid_locs[i][1]-MARG_ERR)) { boxes[currbox][0] = grid_locs[i][0]; boxes[currbox][1] = grid_locs[i][1]; grid_locs[i][2] = currbox; } else if(grid_locs[i][2] == currbox) grid_locs[i][2] = -1; } } // Over-ride main update so that it only re-draws the boxes, not // the rest of the screen. Needs work. /* public void update(Graphics g) { if(change == 1) { setBackground(bgjamcolor); g.setColor(bgjamcolor); g.fillRect(0,0,size().width,size().height); change = 0; } paint(g); } */ public void paint(Graphics g) { // Draw the Grid g.setColor(fgmagcan); for(int i=1;i