patternjavaMinor
My first OOP piece... how have I done?
Viewed 0 times
ooppiecefirstdonehowhave
Problem
This is my first OOP coding project and I would appreciate any constructive criticism as to how I could have used the benefits of Java and OOP to better effect. How would you have implemented this differently? I have been learning Java for less than a week but I hope to go on to make Android apps. See here for this applet working and also source code:
http://fleawhale.net/java/SlidePuzz2/
SlidePuzz2.java
```
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import java.util.*;
import javax.swing.Timer;
public class SlidePuzz2 extends Applet implements MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
static Integer animal;
static Integer grid;
static int piece;
static Board board;
static Element playButton;
static Element loadingText;
static boolean startGame;
Timer aniTimer;
static String[] imgUrls = { "7028/6814220663_4813a81531",
"7162/6814220921_acb3aa92ee", "7029/6814221019_41323ace8b" };
static String imgUrlFlickr = "http://farm8.staticflickr.com/";
public void init() {
startGame = false;
setSize(420, 420);
addMouseMotionListener(this);
addMouseListener(this);
new ElemText(50, 50, "Select animal:");
new ElemOption(65, 80, "mouse", "animal");
new ElemOption(65, 110, "cat", "animal");
new ElemOption(65, 140, "dog", "animal");
new ElemText(250, 50, "Select grid:");
new ElemOption(265, 80, "3x3", "grid");
new ElemOption(265, 110, "4x4", "grid");
new ElemOption(265, 140, "5x5", "grid");
new ElemOption(265, 170, "6x6", "grid");
new ElemOption(265, 200, "7x7", "grid");
playButton = new ElemOption(40, 280, "Play Game >>", "play", 36, false);
}
public void game() {
startGame = false;
animal = (Integer)Element.options
http://fleawhale.net/java/SlidePuzz2/
SlidePuzz2.java
```
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import java.util.*;
import javax.swing.Timer;
public class SlidePuzz2 extends Applet implements MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
static Integer animal;
static Integer grid;
static int piece;
static Board board;
static Element playButton;
static Element loadingText;
static boolean startGame;
Timer aniTimer;
static String[] imgUrls = { "7028/6814220663_4813a81531",
"7162/6814220921_acb3aa92ee", "7029/6814221019_41323ace8b" };
static String imgUrlFlickr = "http://farm8.staticflickr.com/";
public void init() {
startGame = false;
setSize(420, 420);
addMouseMotionListener(this);
addMouseListener(this);
new ElemText(50, 50, "Select animal:");
new ElemOption(65, 80, "mouse", "animal");
new ElemOption(65, 110, "cat", "animal");
new ElemOption(65, 140, "dog", "animal");
new ElemText(250, 50, "Select grid:");
new ElemOption(265, 80, "3x3", "grid");
new ElemOption(265, 110, "4x4", "grid");
new ElemOption(265, 140, "5x5", "grid");
new ElemOption(265, 170, "6x6", "grid");
new ElemOption(265, 200, "7x7", "grid");
playButton = new ElemOption(40, 280, "Play Game >>", "play", 36, false);
}
public void game() {
startGame = false;
animal = (Integer)Element.options
Solution
After a quick look:
- I would use anonymous inner classes for the listeners. One advantage is that you can inherit from Adapters
- in Swing, usually you should overwrite
paintComponent, notpaint
ArrayList locationsshould be generic
- some methods like
gameare too long.
Context
StackExchange Code Review Q#8772, answer score: 4
Revisions (0)
No revisions yet.