import java.awt.*;
import java.awt.event.*;
import javax.swing.Action;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class testout extends JFrame{
public JLabel l1;
//declaring images
ImageIcon a= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b1.png");
ImageIcon b= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b2.png");
ImageIcon c= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b3.png");
ImageIcon d= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b4.png");
ImageIcon e= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b5.png");
ImageIcon f= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b6.png");
ImageIcon g= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b7.png");
ImageIcon h= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b8.png");
ImageIcon i= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b9.png");
ImageIcon j= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b10.png");
ImageIcon k= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b11.png");
ImageIcon l= new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b12.png");
public testout(){
super("Checkers v0.1 beta");//name of window
try{
JFrame checkerboard = new JFrame();
checkerboard.setSize(400, 400);
checkerboard.setLayout(new GridLayout(8,8));
checkerboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkerboard.setResizable(false);
JLabel b1= new JLabel();
b1.setBackground(Color.black);
checkerboard.add(b1);
JLabel l1=new JLabel();
l1.setIcon(a); //sets icon for label
checkerboard.add(l1);
//mouse event
JLabel b2=new JLabel();
checkerboard.add(b2);
b2.setBackground(Color.BLACK);
JLabel l2=new JLabel();
l2.setIcon(b);
checkerboard.add(l2);
JLabel b3=new JLabel();
checkerboard.add(b3);
b3.setBackground(Color.BLACK);
JLabel l3=new JLabel();
l3.setIcon(c);
checkerboard.add(l3);
JLabel b4=new JLabel();
checkerboard.add(b4);
b4.setBackground(Color.BLACK);
JLabel l4=new JLabel();
l4.setIcon(d);
checkerboard.add(l4);
JLabel l5=new JLabel();
l5.setIcon(e);
checkerboard.add(l5);
JLabel b5=new JLabel();
checkerboard.add(b5);
b5.setBackground(Color.BLACK);
JLabel l6=new JLabel();
l6.setIcon(f);
checkerboard.add(l6);
JLabel b6=new JLabel();
checkerboard.add(b6);
b6.setBackground(Color.BLACK);
JLabel l7=new JLabel();
l7.setIcon(g);
checkerboard.add(l7);
JLabel b7=new JLabel();
checkerboard.add(b7);
b7.setBackground(Color.BLACK);
JLabel l8=new
JLabel(); l8.setIcon(h); checkerboard.add(l8); JLabel b8=new
JLabel(); checkerboard.add(b8); b8.setBackground(Color.BLACK);
JLabel b9= new JLabel(); checkerboard.add(b9);
b9.setBackground(Color.BLACK); JLabel l9=new JLabel();
l9.setIcon(i); checkerboard.add(l9);
JLabel b10=new JLabel();
checkerboard.add(b10); b10.setBackground(Color.BLACK); JLabel
l10=new JLabel(); l10.setIcon(j); checkerboard.add(l10); JLabel
b11=new JLabel(); checkerboard.add(b11);
b11.setBackground(Color.BLACK); JLabel l11=new JLabel();
l11.setIcon(k); checkerboard.add(l11);
JLabel b12=new JLabel();
checkerboard.add(b12); b12.setBackground(Color.BLACK); JLabel
l12=new JLabel(); l12.setIcon(l); checkerboard.add(l12);
JLabel
l13=new JLabel("13"); checkerboard.add(l13); JLabel b13=new
JLabel("13"); checkerboard.add(b13); b13.setBackground(Color.BLACK);
JLabel l14=new JLabel("14"); checkerboard.add(l14); JLabel b14=new
JLabel("14"); checkerboard.add(b14); b14.setBackground(Color.BLACK);
JLabel l15=new JLabel("15"); checkerboard.add(l15); JLabel b15=new
JLabel("15"); checkerboard.add(b15); b15.setBackground(Color.BLACK);
JLabel l16=new JLabel("16"); checkerboard.add(l16); JLabel b16= new
JLabel("16"); checkerboard.add(b16); b16.setBackground(Color.BLACK);
JLabel b17=new JLabel("17"); checkerboard.add(b17);
b17.setBackground(Color.BLACK); JLabel l17=new JLabel("17");
checkerboard.add(l17);
JLabel b18=new JLabel("18");
checkerboard.add(b18); b18.setBackground(Color.BLACK); JLabel
l18=new JLabel("18"); checkerboard.add(l18);
JLabel b19=new
JLabel("19"); checkerboard.add(b19); b19.setBackground(Color.BLACK);
JLabel l19=new JLabel("19"); checkerboard.add(l19);
JLabel b20=new
JLabel("20"); checkerboard.add(b20); b20.setBackground(Color.BLACK);
JLabel l20=new JLabel("20"); checkerboard.add(l20);
JLabel l21=new
JLabel("21"); checkerboard.add(l21); JLabel b21=new JLabel("21");
checkerboard.add(b21); b21.setBackground(Color.BLACK);
JLabel
l22=new JLabel("22"); checkerboard.add(l22); JLabel b22=new
JLabel("22"); checkerboard.add(b22); b22.setBackground(Color.BLACK);
JLabel l23=new JLabel("23"); checkerboard.add(l23); JLabel b23= new
JLabel("23"); checkerboard.add(b23); b23.setBackground(Color.BLACK);
JLabel l24=new JLabel("24"); checkerboard.add(l24); JLabel b24=new
JLabel("24"); checkerboard.add(b24); b24.setBackground(Color.BLACK);
JLabel b25=new JLabel("25"); checkerboard.add(b25);
b25.setBackground(Color.BLACK); JLabel l25=new JLabel("25");
checkerboard.add(l25);
JLabel b26=new JLabel("26");
checkerboard.add(b26); b26.setBackground(Color.BLACK); JLabel
l26=new JLabel("26"); checkerboard.add(l26);
JLabel b27=new
JLabel("27"); b27.setBackground(Color.BLACK); checkerboard.add(b27);
JLabel l27=new JLabel("27"); checkerboard.add(l27);
JLabel b28=new
JLabel("28"); checkerboard.add(b28); b28.setBackground(Color.BLACK);
JLabel l28=new JLabel("28"); checkerboard.add(l28);
JLabel l29=new
JLabel("29"); checkerboard.add(l29); JLabel b29=new JLabel("29");
checkerboard.add(b29); b29.setBackground(Color.BLACK);
JLabel
l30=new JLabel("30"); checkerboard.add(l30); JLabel b30= new
JLabel("30"); checkerboard.add(b30); b30.setBackground(Color.BLACK);
JLabel l31=new JLabel("31"); checkerboard.add(l31); JLabel b31=new
JLabel("31"); checkerboard.add(b31); b31.setBackground(Color.BLACK);
JLabel l32=new JLabel("32"); checkerboard.add(l32); JLabel b32=new
JLabel("32"); checkerboard.add(b32); b32.setBackground(Color.BLACK);
checkerboard.setVisible(true); //event handlers
moves move=new moves(); //listener
l1.addMouseListener(move);
l1.addMouseMotionListener(move);
l12.addMouseMotionListener(move);
l12.addMouseListener(move);
} catch(Exception w){ }
}
public class moves implements MouseListener,MouseMotionListener{
public void mouseClicked(MouseEvent z){
if(l1.contains(z.getX(),z.getY()))updateLocation(z);
}
public void mousePressed(MouseEvent z){ }
public void mouseDragged(MouseEvent z){ updateLocation(z); }
public void mouseReleased(MouseEvent z){ }
public void mouseEntered(MouseEvent z){}
public void mouseExited(MouseEvent z){}
public void mouseMoved(MouseEvent z){ }
public void updateLocation(MouseEvent z){
l1.setLocation(z.getX(), z.getY());
if(l1.contains(z.getX(),z.getY())) {
} else {
}
repaint();
}
}
}
答案 0 :(得分:1)
也许你要学习的最重要的编程原则是不要重复自己(DRY)。请注意您的代码是多么重复。
在第一个块中,您声明了12个变量(名为a,b,c等),这些变量仅在成员名称和文件名上有所不同。您应该使用集合(如数组)和带循环的重复代码替换重复的数据结构。例如:
private ImageIcon[] icons;
// Then, in your constructor
for (int i = 0; i < 12; i++) {
icons[i] = new ImageIcon("C:\\Users\\wamae\\Documents\\NetBeansProjects\\checkers\\src\\checkers\\b" + (i + 1).toString() + ".png");
}
注意如何用4替换12行代码。考虑用循环和集合替换其余的重复代码和数据。否则,您可能会发现代码太大而无法管理且耗时太长而无法更改。