Monday, October 18, 2010

CHESS GAME INTERFACE AND ITS SOURCE CODE

THE CHESS BOARD
Introduction
The aim of the assignment was to write a program that would produce a chess board with all its pieces in place. The program was aimed at using the concepts of 2-dimension graphics in Java namely Graphics and Graphics2D. Initially, it was planned that the program will use all the images of a normal chess board. But due to internet service problems, I failed download other images like Queen, King, e.t.c, instead I used few available pieces to fill the board.

Snapshot




The above diagram is the output of running the attached source code below. I have deliberately used the black and blue color (instead of black and white) so that it is different from those that people have already programmed. Unfortunately, if you run it on your computer, the images of the chess board pieces will not be displayed (since they are kept in my computer).



Conclusion
By coming up with the program, I have realized that not only can I write a program that displays field and labels, but also can display real life objects such as chess. I will try as much as possible to see that I extend the program so that people should be able to play it.

Source code
1 import java.awt.*;
2 import java.awt.geom.*;
3 import javax.swing.*;

4 public class YokamChess{
5 public static void main(String[] args){
6 DrawFrame frame = new DrawFrame();
7 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8 frame.setVisible(true);
9 }
10 }

11 class DrawFrame extends JFrame{
12 public DrawFrame(){
13 setSize(700, 700);
14 setLocationRelativeTo(null);
15 setResizable(false);
16 setTitle("tanjibo's chess board");
17 DrawPanel panel = new DrawPanel();
18 Container contentPane = getContentPane();
19 contentPane.add(panel);
20 }
21 }

22 class DrawPanel extends JPanel{
23 public void paintComponent(Graphics g){
24 ImageIcon pawn = new ImageIcon("c:\\Dogs\\pp1.png");
25 ImageIcon horseZ = new ImageIcon("c:\\Dogs\\z1.png");
26 ImageIcon horseX = new ImageIcon("c:\\Dogs\\X.png");
27 ImageIcon horseN = new ImageIcon("c:\\Dogs\\N1.png");
28 ImageIcon horseP = new ImageIcon("c:\\Dogs\\P1.png");
29 ImageIcon horseJ = new ImageIcon("c:\\Dogs\\horse.png");
30 Image horse1 = pawn.getImage();
31 Image horse2 = horseZ.getImage();
32 Image horse3 = horseX.getImage();
33 Image horse4 = horseN.getImage();
34 Image horse5 = horseP.getImage();
35 Image horse6 = horseJ.getImage();

36 int x_cordinate=0;
37 int y_cordinate=0;
38 int boxSize=80;
39 boolean flag=false;
40 super.paintComponent(g);

41 Graphics2D g2 = (Graphics2D)g;
42 for(int j=0;j<8;j++){
43 if(j%2==0){
44 flag=false;
45 }
46 else if(j%2==1)
47 flag=true;
48 x_cordinate=25;

49 for(int i=0;i<8;i++){
50 if(flag==true){
51 g2.setPaint(Color.blue);
52 g2.fill(new Rectangle2D.Double(x_cordinate,y_cordinate,boxSize,boxSize));
53
int loc3=40;
54 for(int c=0;c<8;c++){
55 g2.drawImage(horse2, loc3,480, this);
56 g2.drawImage(horse4, loc3,560, this);
57 g2.drawImage(horse1, loc3+10,80, this);
58 loc3+=80;
59 }
60
int loc12=120;
61 for(int l=0;l<8;l++){
62 g2.drawImage(horse3, loc12,0, this);
63 g2.drawImage(horse5, loc12,560, this);
64 g2.drawImage(horse6, loc12-70,0, this);
65 loc12+=160;
66 }
67 flag=false;
68 }
69 else{
70 g2.setPaint(Color.black);
71 g2.fill(new Rectangle2D.Double(x_cordinate,y_cordinate,boxSize,boxSize));
72 g2.drawImage(horse5,600,560,this);
73 flag=true;
74 }
75 x_cordinate+=boxSize;
76 }
77 y_cordinate+=boxSize;
78 }
79 }
80 }

Friday, October 8, 2010

SOURCE CODE FOR BANK LOAN APPLICATION FORM (java language)

1 import javax.swing.*;
2 import java.awt.event.*;
3 import java.awt.Toolkit;
4 import java.awt.*;
5 import java.io.*;
6
7
public class LoanForm extends JFrame implements ActionListener{
8   static LoanForm myLoanForm;
9   JTextArea myField= new JTextArea(10,35);
10
11
12 
JScrollPane scroll = new JScrollPane (myField,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
13 JTextField name, amount,address, phone, email, accNum, date;
14 JCheckBox small, medium, large;
15 JButton confirmButton, clearButton, exitButton, saveButton, openButton;
16
17 JTextField field = new JTextField(10);
18 Font f = new Font("Book Antiqua", Font.BOLD, 16);
19
20
public LoanForm(){
21 setTitle("Loan Application Form");
22 setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
23
24 ImageIcon idIcon = new ImageIcon("c:\\Dogs\\z.png");
25 JButton idButton = new JButton(idIcon);
26 idButton.setBackground(Color.GREEN);
27 idButton.setBorderPainted(false);
28 idButton.setContentAreaFilled(false);
29
30
JPanel panel1 = new JPanel();
31 myField.setBackground(Color.YELLOW);
32 idButton.setBackground(Color.GREEN);
33
34
addItem(panel1, idButton , 0, 0, 1, 1, GridBagConstraints.WEST);
35 JScrollPane ss= new JScrollPane(panel1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
36 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
37 panel1.setLayout(new GridBagLayout());
38
39 JLabel instructions0 = new JLabel(" TANJIBO BANK OF MALAWI"); 40 addItem(panel1,instructions0 , 0, 1, 1, 1, GridBagConstraints.NORTH);
41 instructions0.setFont(new Font("Castellar", Font.BOLD, 20));
42
43 JLabel instructions1 = new JLabel("(your success is our priority!)");
44 instructions1.setFont(new Font("Book Antiqua", Font.ITALIC, 12));
45 addItem(panel1, instructions1, 0, 2, 1, 1, GridBagConstraints.CENTER);
46
47 addItem(panel1, new JLabel(""), 0, 3, 1, 1, GridBagConstraints.WEST);
48
49
50
JLabel instructions2 = new JLabel("LOAN APPLICATION FORM");
51 addItem(panel1,instructions2 , 0, 3, 1, 1, GridBagConstraints.NORTH);
52 instructions2.setFont(new Font("Times New Roman", Font.BOLD, 16));
53
54
55
56 JLabel instructions3 = new JLabel("Instructions:");57 addItem(panel1, instructions3, 0, 4, 1, 1, GridBagConstraints.WEST);
58 instructions3.setFont(new Font("Book Antiqua", Font.ITALIC, 16));
59
60 JLabel instructions13 = new JLabel("Apply for a maximum of K500 000 if you are in small enterprise; K1 000 000 if you are in medium enterprise;");
61 addItem(panel1, instructions13, 0, 5, 1, 1, GridBagConstraints.WEST);
62 instructions13.setFont(new Font("Book Antiqua", Font.ITALIC, 16));
63
64

65 JLabel instructions5 = new JLabel("and K5 000 000 if you are in a large enterprise ");
66 addItem(panel1, instructions5, 0, 7, 1, 1, GridBagConstraints.WEST);
67 instructions5.setFont(new Font("Book Antiqua", Font.ITALIC, 16));
68
69
JLabel instructions6 = new JLabel("Make sure you have filled the Applicant name's field and the amount (numbers without spaces or a K)");
70 addItem(panel1, instructions6, 0, 8, 1, 1, GridBagConstraints.WEST);
71 instructions6.setFont(new Font("Book Antiqua", Font.ITALIC, 16));
72
73 addItem(panel1, new JLabel(""), 0, 9, 1, 1, GridBagConstraints.WEST);
74
75 addItem(panel1, new JLabel("Name of the Applicant:"), 0, 10, 1, 1, GridBagConstraints.WEST);
76 addItem(panel1, new JLabel("Applicant's Phone Number:"), 0, 11, 1, 1, GridBagConstraints.WEST);
77 addItem(panel1, new JLabel("Applicant's Contact Address:"), 0, 12, 1, 1, GridBagConstraints.WEST);
78 addItem(panel1, new JLabel("Applicant's E-mail Address:"), 0, 13, 1, 1, GridBagConstraints.WEST);
79 addItem(panel1, new JLabel("Applicant's Account Number:"), 0,14,1,1, GridBagConstraints.WEST);
80 addItem(panel1, new JLabel("Date of Loan Application:"), 0,15,1,1, GridBagConstraints.WEST);
81
82 addItem(panel1, new JLabel(""), 0, 16, 1, 1, GridBagConstraints.WEST);
83
84
85
86
name = new JTextField(25);
87 phone = new JTextField(25);
88 address = new JTextField(25);
89 email= new JTextField(25);
90 accNum= new JTextField(25);
91 date= new JTextField(25);
92
93 name.setBackground(Color.YELLOW);
94 phone.setBackground(Color.YELLOW);
95 email.setBackground(Color.YELLOW);
96 accNum.setBackground(Color.YELLOW);
97 date.setBackground(Color.YELLOW);
98 address.setBackground(Color.YELLOW);
99
100 addItem(panel1, name, 0, 10, 2, 1, GridBagConstraints.NORTH);
101 addItem(panel1, phone, 0, 11, 2, 1, GridBagConstraints.NORTH);
102 addItem(panel1, address, 0, 12, 2, 1, GridBagConstraints.NORTH);
103 addItem(panel1, email, 0, 13, 2, 1, GridBagConstraints.NORTH);
104 addItem(panel1, accNum, 0, 14, 2, 1, GridBagConstraints.NORTH);
105 addItem(panel1, date, 0, 15, 2, 1, GridBagConstraints.NORTH);
106
107 addItem(panel1, new JLabel(""), 0, 16, 1, 1, GridBagConstraints.WEST);
108
109

110 Box sizeBox = Box.createVerticalBox();
111 small = new JCheckBox("Small Enterprise", true);
112 medium = new JCheckBox("Medium Enterprise");
113 large = new JCheckBox("Large Enterprise");
114 ButtonGroup sizeGroup = new ButtonGroup();
115 sizeGroup.add(small);
116 sizeGroup.add(medium);
117 sizeGroup.add(large);
118 sizeBox.add(small);
119 sizeBox.add(medium);
120 sizeBox.add(large);
121 sizeBox.setBorder(BorderFactory.createTitledBorder("Applicant's Enterprise"));
122 small.setBackground(Color.GREEN);
123 medium.setBackground(Color.GREEN);
124 large.setBackground(Color.GREEN);
125 addItem(panel1, sizeBox, 0, 17, 1, 1, GridBagConstraints.WEST);
126
127 addItem(panel1, new JLabel("Amount (in Malawian Kwacha:)"), 0, 18, 1, 1, GridBagConstraints.WEST);
128 amount= new JTextField(20);
129 addItem(panel1, amount, 0, 18, 2, 1, GridBagConstraints.NORTH);
130 amount.setBackground(Color.YELLOW);
131 Box buttonBox = Box.createHorizontalBox();
132 confirmButton = new JButton("Confirm");
133 clearButton = new JButton("Clear");
134 confirmButton.setBackground(Color.GREEN);
135 clearButton.setBackground(Color.GREEN);
136
137

138 buttonBox.add(confirmButton);
139 confirmButton.addActionListener(this);
140 buttonBox.add(Box.createHorizontalStrut(25));
141 buttonBox.add(clearButton);
142 clearButton.addActionListener(this);
143 addItem(panel1, new JLabel(""), 0, 19, 1, 1, GridBagConstraints.WEST);
144
145 myField.setEnabled(false);
146 saveButton = new JButton("Save");
147 openButton = new JButton("Open");
148 exitButton = new JButton("Exit");
149 openButton.setBackground(Color.GREEN);
150 saveButton.setBackground(Color.GREEN);
151 exitButton.setBackground(Color.GREEN);
152
153
154
addItem(panel1, saveButton, 0, 22, 1, 1, GridBagConstraints.WEST);
155 saveButton.addActionListener(this);
156 addItem(panel1, openButton, 0, 22, 1, 1, GridBagConstraints.EAST);
157 openButton.addActionListener(this);
158 addItem(panel1, exitButton, 0, 22, 1, 1, GridBagConstraints.CENTER);
159 exitButton.addActionListener(this);
160
161 addItem(panel1, buttonBox, 0, 20, 1, 1, GridBagConstraints.CENTER);
162 addItem(panel1, scroll, 0, 21, 1, 1, GridBagConstraints.CENTER);
163 panel1.setBackground(Color.GREEN);
164 setContentPane(ss);
165 setSize(726,800);
166 setVisible(true);
167
168 setResizable(false);
169 setLocation(300,0);
170 }
171 private int click=0;
172 public void actionPerformed(ActionEvent evnt){
173
174 if (evnt.getSource() == confirmButton){
175 if((name.getText().length() == 0)||(amount.getText().length()==0)){176 JOptionPane.showMessageDialog(LoanForm.this,
177 "You didn't enter your name or the amount ", "Warning!",
178 JOptionPane.INFORMATION_MESSAGE);
179 }
180 else if(small.isSelected()&& Double.parseDouble(amount.getText())>=500000){
182 amount.setText("");
183 JOptionPane.showMessageDialog(LoanForm.this,"You have to apply for less than K500 000","Warning!",
184 JOptionPane.INFORMATION_MESSAGE);
185 }
186
187 else if(medium.isSelected()&&(Integer.parseInt(amount.getText())>=1000000)){
188 amount.setText("");
189 JOptionPane.showMessageDialog(LoanForm.this,"You have to apply for less than K1 000 000","Warning!",
190 JOptionPane.INFORMATION_MESSAGE);
191 }
192
193 else if(large.isSelected()&&(Integer.parseInt(amount.getText())>=5000000)){
194 amount.setText("");
195 JOptionPane.showMessageDialog(LoanForm.this,"You have to apply for less than K5 000 000","Warning!",
196 JOptionPane.INFORMATION_MESSAGE);
197 }
198 else{
199
200 display();
201 clear2();
202
203 }
204
205 }
206
207 else if(evnt.getSource()==exitButton){
208 exitButton.addActionListener (new ChangesListener());
209 }
210
211 else if(evnt.getSource()==clearButton)
212 clear();
213 else if (evnt.getSource()==saveButton){
214 if(myField.getText().length() == 0){
215 JOptionPane.showMessageDialog(LoanForm.this,
216 "There is no information displayed ", "Warning!",
217 JOptionPane.INFORMATION_MESSAGE);
218 }
219 else{
220 saveButton.addActionListener (new SaveListener());
221 }
222 }
223 else if (evnt.getSource()==openButton)
224 openButton.addActionListener(new OpenListener());
225 }
226
227 private void addItem(JPanel p, JComponent c,int x, int y, int width, int height, int align){
228 GridBagConstraints gridBag = new GridBagConstraints();
229 gridBag.gridx = x;
230 gridBag.gridy = y;
231 gridBag.gridwidth = width;
232 gridBag.gridheight = height;
233 gridBag.weightx = 10;
234 gridBag.weighty = 10;
235 gridBag.insets = new Insets(8, 8, 5, 0);
236 gridBag.anchor = align;
237 gridBag.fill = GridBagConstraints.NONE;
238 p.add(c, gridBag);
239 }
240
241 public static void main(String [] args){
242 myLoanForm=new LoanForm();
243 }
244
245 public void clear() {
246 name.setText("");
247 address.setText("");
248 phone.setText("");
249 amount.setText("");
250 email.setText("");
251 accNum.setText("");
252 date.setText("");
253 myField.setText("");
254 }
255 public void clear2() {
256 name.setText("");
257 address.setText("");
258 phone.setText("");
259 amount.setText("");
260 email.setText("");
261 accNum.setText("");
262 date.setText("");
263 }
264
265 public void display(){
266
267 myField.append("NAME:\t\t"+name.getText()+"\n");
268 myField.append("PHONE NUMBER:\t"+phone.getText()+"\n");
269 myField.append("CONTACT ADDRESS:\t"+address.getText()+"\n");
270 myField.append("AMOUNT \t\tK"+amount.getText()+"\n");
271 myField.append("E-MAIL ADDRESS:\t"+email.getText()+"\n");
272 myField.append("ACCOUNT NUMBER:\t"+accNum.getText()+"\n");
273 myField.append("DATE:\t\t"+date.getText()+"\n");
274 // myField.append(small.getText());
275 myField.setFont(f);
276 }
277
278 public class OpenListener implements ActionListener{
279 public void actionPerformed(ActionEvent evnt){
280 JFileChooser fileChooser = new JFileChooser();
281 int result = fileChooser.showOpenDialog(myLoanForm);
282 if(result==JFileChooser.APPROVE_OPTION){
283 File inFile = null;
284 inFile=fileChooser.getSelectedFile();
285 try{
286 FileInputStream fileStream = new FileInputStream(inFile);
287 DataInputStream dataStream = new DataInputStream(fileStream);
288 String displayText = dataStream.readUTF();
289 myField.setText(displayText);
290 fileStream.close();
291 }
292 catch(IOException ioExcept){
293 JOptionPane.showMessageDialog(null, "File Not Found");
294 }
295 }
296 }
297 }
298
299 public class ChangesListener implements ActionListener{
300 public void actionPerformed(ActionEvent event){
301
302 int reply = JOptionPane.showConfirmDialog(null,"Do you want to save the changes?","Tanjibo's Loan Application Form",JOptionPane.INFORMATION_MESSAGE);
303 if(reply==JOptionPane.YES_OPTION)
304 save();
305
306 else if(reply==JOptionPane.NO_OPTION)
307 System.exit(0);
308 else
309 {}
310 }
311 }
312 public class SaveListener implements ActionListener{
314 public void actionPerformed(ActionEvent evnt){
315 save();
316 }
317 }
318 public void save(){
319 JFileChooser fileChooser = new JFileChooser();
320 int result = fileChooser.showSaveDialog(myLoanForm);
321 if(result==JFileChooser.APPROVE_OPTION){
322 File outFile = null;
323 outFile=fileChooser.getSelectedFile();
324 if (outFile.exists()){
325 JOptionPane.showMessageDialog(null, "File already exists");
326 }
327 else
328 try{
329 FileOutputStream fileStream = new FileOutputStream(outFile); 330 DataOutputStream dataStream = new DataOutputStream(fileStream);
331 dataStream.writeUTF(myField.getText());
332 fileStream.close();
333 }
334 catch(IOException e){
335 JOptionPane.showMessageDialog(null, "Error in Opening");
336 }
337 }
338 else if(result==JFileChooser.ERROR_OPTION){
339 JOptionPane.showMessageDialog(null, "ERROR OCCURS IN SAVING");
340 }
341
342 }
343 }