我是枚举的忠实粉丝。这是一种使用枚举的方法。这还包括您的代码(在另一个类中)以及将它们并排比较的驱动程序。HTH。
package test;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LearnGraphics {
public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
Quadrant q = Quadrant.valueOf(cord_x, cord_y);
float x = q.horiz.rectX*size, y = q.vert.rectY*size;
g.drawRect(Math.round(x), Math.round(y), size / 4, size / 8);
}
public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {
Quadrant q = Quadrant.valueOf(cord_x,cord_y);
float x0 = q.horiz.lineX0*size, y = q.vert.lineY*size, x1 = q.horiz.lineX1*size;
g.drawLine(Math.round(x0), Math.round(y), Math.round(x1), Math.round(y));
}
public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
Quadrant q = Quadrant.valueOf(cord_x,cord_y);
float x = q.horiz.ovalX*size, y = q.vert.ovalY*size;
g.drawOval(Math.round(x), Math.round(y), size / 4, size / 8);
}
public static void invertColor(Graphics g){
Color c = g.getColor();
g.setColor(new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue()));
}
public static void main(String[] arg){
JFrame jf = new JFrame("Learn Enums (NEW WAY)");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final int size=400;
@SuppressWarnings("serial")
JPanel jp = new JPanel(){
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
drawRectangle(g, size, "left", "top");
g.setColor(Color.ORANGE);
drawOval(g, size, "left", "bottom");
g.setColor(Color.GREEN);
drawLine(g, size, "right", "bottom");
}
};
jf.setContentPane(jp);
jf.setBounds(120, 120, size, size);
jf.setVisible(true);
JFrame owjf = new JFrame("Learn Graphics (OLD WAY)");
owjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@SuppressWarnings("serial")
JPanel owjp = new JPanel(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
OldWays.drawRectangle(g, size, "left", "top");
g.setColor(Color.ORANGE);
OldWays.drawOval(g, size, "left", "bottom");
g.setColor(Color.GREEN);
OldWays.drawLine(g, size, "right", "bottom");
};
};
owjf.setContentPane(owjp);
owjf.setBounds(120+50+size, 120, size, size);
owjf.setVisible(true);
}
}
enum Quadrant{
top_left(Vert.top,Horiz.left),
top_right(Vert.top,Horiz.right),
bottom_left(Vert.bottom,Horiz.left),
bottom_right(Vert.bottom,Horiz.right);
private Quadrant(Vert v, Horiz h){
vert=v;
horiz=h;
}
final Vert vert;
final Horiz horiz;
public static Quadrant valueOf(String horiz, String vert){
return valueOf(vert+"_"+horiz);
}
enum Vert{
top( 1/8f, 1/6f, 1/8f),
bottom(3/4f, 5/6f, 3/4f);
private Vert(float rectY, float lineY, float ovalY){
this.rectY=rectY;
this.lineY=lineY;
this.ovalY=ovalY;
}
final float rectY, lineY, ovalY;
}
enum Horiz{
left( 1/12f, 2/3f, 11/12f, 1/12f),
right( 1/3f, 1/12f, 1/3f, 2/3f);
private Horiz(float rectX, float lineX0, float lineX1, float ovalX){
this.rectX=rectX;
this.lineX0=lineX0;
this.lineX1=lineX1;
this.ovalX=ovalX;
}
final float rectX, lineX0, lineX1, ovalX;
}
}
class OldWays{
/** INCLUDED SO YOU CAN TEST THE VERSIONS AGAINST EACH OTHER **/
public static void drawRectangle(Graphics g, int size, String cord_y,
String cord_x) {
int longside = size / 4;
int shortside = size / 8;
int x = 0;
int y = 0;
if (cord_x.equals("top") && cord_y.equals("right")) {
x = size - (size / 3);
y = size / 8;
}
if (cord_x.equals("top") && cord_y.equals("left")) {
x = size / 12;
y = size / 8;
}
if (cord_x.equals("bottom") && cord_y.equals("right")) {
x = size - (size / 3);
y = size - (size / 4);
}
if (cord_x.equals("bottom") && cord_y.equals("left")) {
x = size / 12;
y = size - (size / 4);
}
System.out.printf("ow.drawRectangle(%d, %d, %d, %d);\n", x, y, longside, shortside);
g.drawRect(x, y, longside, shortside);
}
public static void drawLine(Graphics g, int size, String cord_y,
String cord_x) {
int x = 0;
int y = 0;
int x1 = 0;
int y1 = 0;
if (cord_x.equals("top") && cord_y.equals("right")) {
x = size / 12;
y = size / 6;
x1 = size / 3;
y1 = size / 6;
}
if (cord_x.equals("top") && cord_y.equals("left")) {
x = (size / 2) + (size / 6);
y = size / 6;
x1 = size - (size / 12);
y1 = size / 6;
}
if (cord_x.equals("bottom") && cord_y.equals("right")) {
x = size / 12;
y = size - (size / 6);
x1 = size / 3;
y1 = size - (size / 6);
}
if (cord_x.equals("bottom") && cord_y.equals("left")) {
x = (size / 2) + (size / 6);
y = size - (size / 6);
x1 = size - (size / 12);
y1 = size - (size / 6);
}
System.out.printf("ow.drawLine(%d, %d, %d, %d);\n", x, y, x1, y1);
g.drawLine(x, y, x1, y1);
}
public static void drawOval(Graphics g, int size, String cord_y,
String cord_x) {
int longside = size / 4;
int shortside = size / 8;
int x = 0;
int y = 0;
if (cord_x.equals("top") && cord_y.equals("right")) {
x = size - (size / 3);
y = size / 8;
}
if (cord_x.equals("top") && cord_y.equals("left")) {
x = size / 12;
y = size / 8;
}
if (cord_x.equals("bottom") && cord_y.equals("right")) {
x = size - (size / 3);
y = size - (size / 4);
}
if (cord_x.equals("bottom") && cord_y.equals("left")) {
x = size / 12;
y = size - (size / 4);
}
System.out.printf("ow.drawOval(%d, %d, %d, %d);\n", x, y, longside, shortside);
g.drawOval(x, y, longside, shortside);
}
}
注意:有时当您创建的枚举不仅仅是一个值时——在这种情况下,它们附加了值——枚举代码本身可能看起来有点混乱,但看看客户端代码有多干净!