P. Pfahler, E. Stümpel Programmieren in Java, Winter 1997/98 1. Übungsblatt, 24.10.1997 Abgabe: 31.10.1997, 18h Aufgabe 1: ========== Anhängend finden Sie ein Java Applet für ein einfaches 3x4-Schiebespiel. Aus dem Programm sind die Kommentare entfernt worden. Kommentieren Sie dieses Programm neu und ändern Sie es in ein 4x4-Schiebespiel. Dieses Applet kann außer in einem Java-fähigen Web-Browser auch mit dem "appletviewer" ausgeführt werden. In beiden Fällen brauchen Sie eine HTML-Datei, die etwa so aussieht: Slider Applet Hier kommt das Applet slider": ------------------------------- //------------------------------------------------------ // (Slider - Jan 12, 1991) // by Julie Ton // Modified : Slider.java - October 20, 1996 // Modified : Slider.java - August 15, 1997 (peter) // Modified : Slider.java - October 24, 1997 (g. mein) //------------------------------------------------------- import java.awt.*; import java.applet.Applet; public class slider extends Applet { static final int MAX_ROW = 4; static final int MAX_COL = 3; static final int IMAGE_WIDTH = 100; static final int IMAGE_HEIGHT = 100; static int Piece [] = { 2, 12, 6, 5, 4, 9, 7, 8, 3, 10, 11, 1}; static final int BLANK = -1; static final int MAX_PIECES = MAX_ROW * MAX_COL; static int Blank_Index; static int Hilite_Up ; static int Hilite_Down ; static int Hilite_Left ; static int Hilite_Right ; public void init() { repaint(); for (int i = 0; i < MAX_PIECES; i++) { if (Piece [i] == (MAX_PIECES)) { Blank_Index = i; break; } } Set_Hilite_Pieces (); } void Set_Hilite_Pieces () { int row = Blank_Index / MAX_COL; int col = Blank_Index % MAX_COL; Hilite_Up = row > 0 ? Blank_Index - MAX_COL : BLANK; Hilite_Down = row < MAX_ROW-1 ? Blank_Index + MAX_COL : BLANK; Hilite_Left = col > 0 ? Blank_Index - 1 : BLANK; Hilite_Right = col < MAX_COL - 1 ? Blank_Index + 1 : BLANK; } public void paint (Graphics g) { int col, row, index; int x, y; g.drawRect (0,0,IMAGE_WIDTH * MAX_COL + 2, IMAGE_HEIGHT * MAX_ROW + 2); index = 0; for (row = 0 ; row < MAX_ROW ; row++) { for (col = 0 ; col < MAX_COL ; col++, index++) { x = col * IMAGE_WIDTH + 2; y = row * IMAGE_HEIGHT + 2; if (index == Hilite_Up || index == Hilite_Down || index == Hilite_Left || index == Hilite_Right) g.setColor (Color.blue); else if (index == Blank_Index) g.setColor (Color.yellow); else g.setColor (Color.black); g.drawRect (x, y, IMAGE_WIDTH - 2, IMAGE_HEIGHT - 2); g.fillRect (x, y, IMAGE_WIDTH - 2, IMAGE_HEIGHT - 2); g.setColor (Color.orange); g.setFont(new Font("Courier", Font.BOLD, 16)); int xoff; if (Piece [index] < 10) xoff = 3; else if (Piece [index] < 100) xoff = 6; else xoff = 9; x = x + (IMAGE_WIDTH / 2) - xoff; y = y + (IMAGE_HEIGHT / 2) + (IMAGE_HEIGHT / 6); if (index == Blank_Index) g.drawString(" ", x, y); else g.drawString(String.valueOf (Piece [index]), x, y); } } } public boolean mouseUp (Event evt, int x, int y) { int col = x / IMAGE_WIDTH; int row = y / IMAGE_HEIGHT; if (col < 0 || col >= MAX_COL || row < 0 || row >= MAX_ROW) return false; int selected_index = col + (row * MAX_COL); if (Valid_Move (selected_index)) { int temp = Piece [selected_index]; Piece [selected_index] = Piece [Blank_Index]; Piece [Blank_Index] = temp; Blank_Index = selected_index; Set_Hilite_Pieces (); repaint (); } return true; } boolean Valid_Move (int selected_index) { return (selected_index == Hilite_Up || selected_index == Hilite_Down || selected_index == Hilite_Right || selected_index == Hilite_Left); } }