GUI3x3。Project。
Overview
We have a 3 x 3 puzzle, which gives us 9 puzzle pieces that are movable on the right. They can be dragged anywhere on the canvas, but not off the canvas. On the left, we have a 3 x 3 board. After a puzzle piece is dragged onto its correct location, the puzzle piece is “locked” into the board and is no longer movable.
The reason “locked” is in quotes is because the puzzle piece is actually not locked. We created this illusion by having two different types of Pieces here: PuzzlePiece and BoardPiece. When a PuzzlePiece (movable piece on the right) is dragged onto a BoardPiece (fixed piece on the left), your program will do some verification to see if they match. If so, it’s going to hide the PuzzlePiece and reveal the BoardPiece with the same image as the PuzzlePiece.
So it may seem like the PuzzlePiece is locked in place, but it’s actually a different BoardPiece that’s displayed on the canvas. Now that we know the basic idea/hack behind this game, how do we implement it?
Useful Interfaces
Take advantage of interfaces! We have two different kinds of Piece types here, but they share a lot in common. We’ll implement the Piece interface later in PuzzlePiece and BoardPiece classes.
The Highlightable interface is used to draw borders and highlights around each piece:
1 2 3 4 5 6
|
import java.awt.Color;
public interface Highlightable #123; public abstract void showHighlight( Color color ); public abstract void hideHighlight(); #125;
|
The Hideable interface is used to hide and show pieces:
1 2 3 4
|
public interface Hideable #123; public abstract void show(); public abstract void hide(); #125;
|
The Piece interface, which extends the two interfaces above and includes a few other methods:
objectdraw.*;
1 2 3 4 5 6 7 8
|
public interface Piece extends Highlightable, Hideable #123; public abstract boolean contains(Location point); public abstract boolean equals(Object o); public abstract Location getCenter(); public abstract int getId(); public abstract void move(double dx, double dy); #125;
|
Piece Classes
You will create 2 Piece classes: class BoardPiece and class PuzzlePiece.
Each of these classes will implement the Piece interface. For example:
1
|
public class BoardPiece‾⁰ ⸼⡣⥬‽ ⁰‾‣‼ⸯ⁰☠㬠†‼††‾†♤㭴‾⁵ ‼‼†††䵳ⱥ›‾†
⁖⁴㱳⽩㹧ഠ†⁉㱡⽧㹯ൢ੪㱥⽣㹴൯ਠ㱩㉳†㵮≳㍯ⴾ∼㹬†㌨㩩†㱤⼠㉲㹯൭ਠ㰰㹴†☠㬠†⁴Ⱐ⁰‼†‾䍣⁴䍰⡦⤠⁴⁰ †䅲䵧䙧⸨⁐††㝥㍣㕥㌠㡴と⸩㰠⽨㹳ഠ੯㱮㌠㵣≫䜠ⵡ䥭≴㸠䝯䥣㱨⽴㍯㹮ഠੴ㱯㸠ㅥ〠づㅦ〠ぴ†⁵⁂⽐⽣⼠䅩㕸ⵥ⽥†⁴ね㡴†⡯⁴〬⸠ⰠㅧⱮ†♴㭴⥨㰠⽥㹧ਠ㱯㸠䥴⁴※Ɐ†⁴䥡⡣⥫†⡥⁴䝡䥴䍴⥨䙨㩬㱡⽣㸮ഩ਼㰯㹩㰾‾㵔≨≵㹣൴੯㱲㹂൯⁐㱥㹤ഠ⁵⁺⁺⁐㱣㹳൨੯⁵††⁴㱩㴠≥∺㸼യੰ‾ ‼⁰‾‼⁵㱥㹡㱳㵨≴≡㸢ㄾ㰍⼊㹬㱥 ⼠㸠㰠⼠㹯‾ ††††††††‼㱴⽲㸊ഠਠ††††††††††‼⁴㱤㵧≵∢㸾ਊ††††††††††††㰼㸾㰼†㴽∢∢㸾䥳⠼㱢‼㴍∊∠㸠☠㬯つ⸾☠㬠㰠⼠㹴⥤㬠㱣⽬㸢㱣⼢㸾㰍⼊㸠ഠਠ††††††‼⁰‾‼㱳⽰㸠ൣ੬‽•㱥⼢㹳൰†㱡⽳㹮ൣੴ㱩⽯㹰ൡ੮㰠⽣㹫㱥⽹㹯൲㰢㹰♵㬼㰯⽳㹡൮ਾ㰠㹳•㤢‾ 䥮⡳•䤾⤼⁰䥡⁴㤼 †䥣⡮⤠ⱬ†⁷‼‾⁰″⁰䤼⠾⤼ ††䤠⁴⸠†† ‼䥹 ⁰※‾䉨⽣♬㬠Ⱐ†䉐⼠☯㭵‾Ⰺ†⁰䥬
䤩†⸠㱡⽮㸠ൣ੨㱡㍧㵨≥䰠≩㹬䱩㰠⽴㍥㸠െੲ㱡㹥⁰䈾‾†
†⁺⁐†䅩Ⱐㅴ㠠†Ⱐ⁂†䰠ⱬ†⁷䉬†⁴††††††䱯⁴‼†㩬㱩⼾㸊ഠਠ㰠㸼㱬†㵩≤⁺≩㹥ൣ㰠㹤ഠ੨†㱴㹡൧††⁴㰠㹧൨੬⁴†
㰠†㵐≩≥㹳ഠ੮⁴†⁵㱳㹵㰠†㵰≬≴㸠ㅴ㱨⽥㹲㱡⽥㹤㱩⁰㴠∠∾㸍㈊㰠⼠㹩㰾†⼠㸠㰠㵳∨∠㹐㍵㱺⽺㹥㱣⽥㹴㱥㵴≨≯㹭㑥㰠⽰㸠㱩⽯㹮㱴㵨≥≩㹢㕬㱥⽉㸠㱡⽣㹩㱡⁷㵨≡≥㹣㙥㰠⼨㸺㰠⼠㹖㱩㴦≲∻㹳㜠㱣⽯㹮㱳†⽭㹥㱴⽨㸮ഠ⁴
†㱴⽨㸠൩੮†⁂⁐†㱲⁵㵡∠≬㹥ഠ੭††⁉㱦㹵㰠㴠≴≰㹵㱺⁴㵥≯∠㹤㱯⼻㹮⁴㰠㴠≳≩㹳㱩⽣㹢⁹㰠†㵩≮≵㹺㱥⽣㹬㱳㵯∠≲㹤㱣⽥㸠⁴䥥䕹䌠䕧䕬䍫䱤†㵩㰠㵬≬≹㹯㍵㰠⽡㹨㭥†㱰㴠≡∦㹲⽳⽱⁵†††㱵⽺㹩㱥⽣㸠㱹⼠㹣㱵 㵥≐∠㹦㱡⁰㵴≬≮㸠㱲⽤㹥㰠㴠≡≧㸠㱨⽥㹳†㱡†㵲≥≫㸺㱥⽡㸠⁰㱡㵰≯≲㹥㱥⼠㹯䥩䕳䌬䔠䕡㵮㰠㵥∠∠㹯㍦㰠⽴㹵㭺⁺㱬㵡≲≩㹮⽧⼠⁴⁉⁴⁴㱐⽵㹐㱩⽥㹳㰠⁴⽨㹩㱮‰㴰∠≬㹳㰠㵴≨≲㸠㱂⽯㹩㱣㴠≣∦㹬㱨⽥㹮⁴㰠㵥≤∠㹩㰠⽂㹐㱥⁗㵥∠≬㹬㱨⽥㹧†䰠䕩䅨䍥䥴䝥㴠⁴㱨㵦∠≺㹺㉬づ㱐⽩㹩㭳†㱣㵮∠≸㸠⼵⼰⁰ⁱ㰾⼍㸠㰼⼯㸊㰠†⼼㹬㱩†㵥≱∨㸩㰠㵴≷≳㸠㱡⽬㹲†㱥㵥≳≺㹺㱥⼠㹨†㱷⁴㵡≲≩㹮㱢⽯㹧㱩†㵩≳≹㸠㱣⽫㹴䱩䕱䘠䙉䔠‰㴠㰠㵧≮≥㹡㍣㕨㔠㱰⽩㸼㬯⁰㰾†㴾∍≩㸾⼍⼊†
⁴†㱥⼠㹦㰠⽴㹩㱥⼼㸯㱰†㵩∾∠㸠㰼 㵰∾≥㹲㱲⽮㹥†㱣㵨≥∼㸯㰠⼠㸾㰊‼㴠∠≶㹥㰠⽂㹐㱥㵤≯≧㸠㱣⽥㹲䉐䅥䑥䴠䅡䝥䤠㵤㰠⁷㵨≡≡㹤㉤㔠㱳⽯㹭㭰㱥㵤∠≥㹤⼠⽩†††㰠⽐㹥㱐⽩㸠㱗⽲㹥㰠㵥≴≴㹨㱩㴠≲∠㹣㱡⽮㹯㱥㵣≥≩㹮㱣⽥㹡⁹㰮㵬≬≳㸠㱥⽳㹰㱡㴠≐≥㹣㰠⽎㹴䉡䄠䑨䴠䅉䜠䥩⁵㵺⁺㱬㵎≏≣㹩㑡ぴ㱥⽤㹨㬠㱴㵲≲≸㸠⽢⽵⁴⁴⁵ⁱ⁰㱩⽭㸼㰯⽰㹰㰾†⽡㹶㱯㵡≧≵㹭㱢㴬∠∠㹣㰠⽍㹕⁍㱟⁉㵣≯∠㹉㱣⽡㹍⁁㱘 㵅≃∮㸠㱷⽥㹤†㱴㴠≡≤㹥㱷⽥㸠䤠䑍䕁䱟䕎䝍䡏⁆㵟⁐㱉†㵨∻≩㹣ㅨ〠ぴ㱡⽫㹡㭲㰠㵳≥≺㹥⽲⽯㱯⽯㸠㱎⽯㹵㰠⽵㹬㱤⼠㹡ൢ੬†⁴††㰠⽴㸠ൂ੯⁐㱳⼠㹤ഠ⁵⁺⁺㱥⽐㸠൯੮㰠⽹㹡൮੶㱡⽳㹷㱥⼠㹲ਠ㱮㹴☠㭥㰠⽴㹥൲㰠㹥䉴⁉⁴
㰠⽮㹥ੳ㰠㹯㰠†㵨≥≡㹮ਠ㱴㹧൩††㱶㹩൮੧†⁴†⁐㱣㸮ഠ†††㱢㵵∠≤㸮ഠ
‼ ⁰‾ ‼″†㱤㹲㱡㵬≥≥㸢ㄾ㱄⽲㹮㱧⁵⽺㹺㱬⽥㹣਼ ″‾ ‼⁰‾⁂⁹†㱐⽁㸠൹੯⁵†⁵††㱰⁴㵭≩∠㹷൩ੴ†⁍⁵⁐㱳㸬㰠⁵㵧∨≮㹤㵮⁍䉯䅳䑒䵬䅥䝳䥥†⭔䤠䑬䕯䱩䕣䝩䠠⩴⡬†╴䤠䕳䍡䕭䕙䌠䱡⥮㱴⼠㹥㱥⼠㹲㱥⽦㹮ൣ†⁴†⁴⁐㱴⼠㹤ഠ੦†⁴†㱤⽩㹥൲†㱢⽥㸠൬㱳⽴㸠ൡ੮㱤⼠㹴㰠⽰㹩൮ੴ㰮㹙♯㬠㱮⽥㹤ഠੴ㱯㹤㱮⽥㹡㱩㹩㱯㵲≥⁴≫㹥ੰ㰠㸠൯੦†⁷㱣㹺൬⁐†㰠㹩൮੧†‼ ⁰㰾‼㴠≣≳㹯ഠ⁹†⁰†⁴㱲㹨㰠⁐㵡≲≨㹯ㅬ㱤⽩㹬㱬⽥㸠㱐⽵㹥੩††㱤⼠㹩ൣ੨†⁐⁵⁺⁺⁐㰠㴠≴≯㹩൮ੴ‼ ⁰‾ ‼″†㱤㹥㱲㵺≬≣㹥‾㵖䉲䅦䑩䵧䄠䝵䥺⁐⭩䥥䐼䔯䰳䔾䜊䡰‾⩈⡭…⽨䥬䕩䍰䔻䕥⥯㱬⽤㸠㱢⽨㹥㱣⽫㸠൩੦†⁴†⁰⁵⁺⁺†㱰⽩㹥ഠ੨††㱰⽬㹥ਠ
⁴㰠⽷㹳൴੩㱬⽬㹧൩੮㱧⼩㹥㰠⽣㹲൲㱣㸠♳㭯㱮⼠㹨ਠ㱢㹡⁔ⁱ※㰠⽰㹡ൣ㰬㹡㱮ⁱ㴠≬≩㹴ഠਨ㱈㹩൴ਠ†⁎㱏㹯൮੍⁵⁄㰨㸮഼ਯ⁰‾ ‼⁰‾⁂⁵⁴†⁉㰦ⁱ㴠≷≵㸠൴੨⁵†⁷†⁴㰠㹩㱦⁰㴠≰∠㹨ㅡ㱳⼠㸠㱰⽥㹤㰠⽣㹥ൣੴ⁹†⁒㰠⽭㹹ഠ††⁷㰠㵴≨≮㹴ੲ††⁴†⁐⁵⁺⁺㱬㹥㱣†㵮∠∠㹢†㴵‰䱥䕬䙳䙱䕡⬠䱥䔠䅴䍥䥲䝯⨠⁴⡨†╂䥲䕤䍐䕩䔬䍥䱣⭮†㱳㵺≬∠㹩ㅳ㰠⼦㹯⤻⭯䥡䑩䕮䱤䔦䝤䡵⨻†⡩†╴䤠䕂䍯䕡䕩䍥䰨⥴㱨⽩㹥㱣⽳㸠㱡⽬㹤൹ਠ††⁴㱡⽩㸨ഩ†⁈†㱴⽨㹭ੴ㰠⽹㹱൵੯㰻⽶㹬੭㱥⽮㸠㱂⽯㹲㱩㹣♥㬠㱐⽵㹺൬㱐㹥㱣⽯㹥ഠ੩㱮㹨㱡㴠≵⁴∠㹴൯ਠ㱧㹥ഠ⁴㱲㹨ਠ†㱰㹺൬†⁰††㱡†㵩≮∠㹥൶⁹†⁂⁐㱥㸠㱡⁰㵷≥≥㸠ㅡ㱢⽬㸠㱦⼠㹡㰠⽂㹤੩†⁴⁴†㱴⽡㹳ഠੴ†⁴†㰠㵢≥≺㹺൬†⁰†⁁⁎⁄†⁴㱨㹡㱲⁵㵡≴≧㸠⁄㵳
䱥䕮䄠䍣䥡䜠⩯†⡡⽡䥡䕮䍤䔠䕱⭫…㱲※㴠≐≁㹧ㅡ㱩⽮㹥⤠…⭬䥵䑯䔻䱯䕣䝩䡧…⩲⡱⼻†䥳䔠䍤䕯䕢⥩㱧⼠㹧㱲⽥㹤㰠⽰㹬ਠ⁰††㱯⽷㹧ഠੴ†⁂㱐⽩㹥ഠ†㱳⽣㸠൩੮㰠⽴㹥൲੶㱩⽥㸦㱲⽳㹵൯㱴㹦♯㬠㱴⽯㹳൨੯㱷㹧⁴⁴䥴⁵†††⁰ †•♣㬾†⁴‼㱲⽥㹨੬㱬㹰䈻†ⱨ†⁰♹㭯ⅳ☠㭧†⁴㵮㍴㕨㕥⁹㴮†⁷䉯†䥲⁰††㕳㔠ⁱ⸦䡤※††ⱱ⁵⁄‰‸†⁵⠠⁹⁵⥥⸠㱩⽳㹤൯੮㱥㹡♥㭥⁹⁁䉥†⸬†⁵†††䥧†䱵†⁐䈠†⁰‹††䉡′ 䥨‼⁹䱯⁹䈠ⸯ⁰䔾⁰䉲⁴䤻䐠 ⁴⁵㱡⽶㸠഼ਯ㱣㹤䙥 Ⱳ†⁺⁰††⸼⁰‾†⁶⁷⠾⤼ 䱵††䑴䍥‵⁴⁵䥥䝡†⁹な㡯††⁴ⁱ⁴♴㬍‼⁔•䤾⁵Ⱪ‼⁔⁵⁵†ⱡ‼⁴⸱‱♰/code>
You may turn in your programming assignment as many times as you like. The last submission you turn in before the deadline is the one that we will collect.
Verify
To verify a previously turned in assignment,
gt; cse11verify pa5
If you are unsure your program has been turned in, use the verify command. We will not take any late files you forgot to turn in. Verify will help you check which files you have successfully submitted. It is your responsibility to make sure you properly turned in your assignment. |