Yasin DEMİR

#herseypaylasmakicin

———————————————————————————————————

Java da gui hazırlanırken kullandığımız bir çok şeyi basitce sizler için yazdım…Buradan indirip incelemeniz çok faydalı olacaktır.(Checkbox,Borderlayout,GridLayout,RadioButton,Combobox,TextArea,ArrayList vs )

———————————————————————————————————

.

———————————————————————————————————

Puzzle Game

Project Definition: Use JFrame or JApplet and implement a Puzzle game such that, it allows user to select an image from the file system and gets input about the size of tiles.The game should inform the user when he or she completes the puzzle.
Deliverables: Full source code, Documentation.

Download here : Source code and documentation of the Puzzle Game

———————————————————————————————————

PRESENTATION OF PUZZLE GAME (WITH GUI)

Step1: Welcome message to Puzzle Game.

Step 2: Main Menu to Puzzle Game. This here have a choose picture, puzzle types (3, 5, 6, 7 e.g.) and mix&start button from Main.

Step3: Press to browse button at Main Menu provide a choose picture with file system.

Step4: After selecting picture, choose puzzle types (3, 4, 5, 6, e.g.) from Combo box.


Step5: After that press the original picture button will be show the Original picture.


Step6: After that press the Mixed Start button will be mixing picture and starting Puzzle Game.


Step 7: Puzzle Game finished or its exit, again come back the Main Menu. We will again choose picture and puzzle types, after we will start the new Puzzle Game.


ENJOY OF THE PUZZLE GAME   🙂


PUZZLE GAME

PUZZLE GAME SOURCE CODE AND CODE DEFINITIONS

Project Definitions: Use JFrame or JApplet and implement a Puzzle game such that, it allows user to select an image from the file system and gets input about the size of tiles. The game should inform the user when he or she completes the puzzle. Deliverables: Full source code, Documentation.

Request to us;

1-      JFrame or JApplet make use Puzzle Game

2-      Puzzle Game picture’ choose with file system

3-      Determine the Puzzle Game Types’ (3,5,6 e.g. tiles)

4-      Information of Puzzle Game Users

Description the written codes and Puzzle Game definitions;

String values[] = {“3”, “4”, “5”,”6″,”7″,”8″,”9″,”10″};                    TABLE 1

String path = “”;

JLabel selectpicture = new JLabel(“Select Puzzle Picture”);

JLabel selecttype = new JLabel(“Select Puzzle Types”);

JComboBox combo1 = new JComboBox(values);

JButton startbtn = new JButton(“Start”);

JLabel l1 = new JLabel();

JLabel l2 = new JLabel();

JButton button[][];

private Image source;

private Image image;

int width, height;

int n =5;

TABLE 1: Firstly, I make combo box and label buttons definitions for Main Menu. At the same time take picture will be change the icon definitions of variables. I give a puzzle types for Puzzle Game.

PuzzleGame(){        TABLE 2

JOptionPane.showMessageDialog(null, “WELCOME TO THE PUZZLE GAME”+”\n”);

JButton Browse = new JButton(“Browse”);

Browse.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

JFileChooser fc = new JFileChooser();

fc.showOpenDialog(null);

path=fc.getSelectedFile().toString();

}

});

TABLE 2: Write a message starting at the Puzzle Game. This is a meeting for Puzzle Game. After that, we see some an example PropTest in the lesson, make a Browse button and use to this way (JFileChooser) choose picture and added with file system. So; I take picture and its variable equal “path”.

TABLE 3

this.setVisible(true);

this.setSize( new Dimension(300, 100) );

this.setTitle(“PUZZLE GAME by ySndmr”);

this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

this.setLocationRelativeTo(null);

this.setLayout( new GridLayout(3,3,1,1) );

this.add( selectpicture);

this.add(l1);

add(Browse);

this.add( selecttype);

this.add(l2);

this.add(combo1);

this.add(startbtn);

Browse.addActionListener( this );

startbtn.addActionListener( this );

combo1.addActionListener(new CbLsnr());

pack();

}

TABLE 3: Here, I make buttons added on Grid layout to in the Main Menu. (Added Main Menu size, Main Menu is in the center at Pc screen, added JDialog.DISPOSE_ON_CLOSE for not lost from screen)Browse, start buttons and combo box assign for Action Listener. I use Pack() for good GUI.

public class CbLsnr implements ActionListener{                          TABLE 4

public void actionPerformed(ActionEvent e) {

JComboBox combo = (JComboBox)e.getSource();

String selected;

selected = (String) combo.getSelectedItem();

n=Integer.parseInt(selected)+2;

}

}

TABLE 4: I take choose variable from Combo box action listener’. This value added 2 and equal n variable. This reason Puzzle Game window buttons’ to be enable (false) ( button[2][2].setEnabled(false);). I will explained later in this subject

public void actionPerformed(ActionEvent arg0) {                           TABLE 5

if ( arg0.getSource().equals(startbtn)){

JFrame slidePuzzle = new JFrame();

ImageIcon imic = new ImageIcon(path);

button=new JButton[n][n];

slidePuzzle.setLayout(new GridLayout(n,n));

slidePuzzle.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

slidePuzzle.setVisible(true);

slidePuzzle.setSize(imic.getIconWidth(),imic.getIconHeight());

slidePuzzle.setTitle(“PUZZLE GAME”);

slidePuzzle.setLocationRelativeTo(null);

TABLE 5: Start button to Action listener, press to start button take a previously and assigned keep to path picture define as icon. I define an array buttons (at Puzzle Game Window) and throw in the Grid layout. I give a title Puzzle Game’ and it’s put the center in the page.

for(int i = 0 ; i<n;i++){                                              TABLE 6

for(int j = 0;j<n;j++){

button[i][j]=new JButton();

button[i][j].addActionListener(new actionlistener());

slidePuzzle.add(button[i][j]);

}

}

button[2][2].setText(“5555”);

button[2][2].setEnabled(false);

for(int i = 0 ; i<n;i++){

for(int j = 0;j<n;j++){

if(i==0 || i==n-1){

button[i][j].setEnabled(false);

button[i][j].setBackground(Color.BLACK);

}

if(j==0 || j==n-1){

button[i][j].setEnabled(false);

button[i][j].setBackground(Color.BLACK);

}

}

}

TABLE 6: I define a previously make a buttons to button array. Puzzle Game make a buttons all of them and outmost buttons is enable (false).Because, my write such puzzle game algorithm. I assigned changeable button is define empty and its button is coordinate a 2.2. I assigned color background of buttons.

source = imic.getImage();                                               TABLE 7

width = imic.getIconWidth();

height = imic.getIconHeight();

for ( int i = 0; i < n; i++) {

for ( int j = 0; j < n; j++) {

image = createImage(new FilteredImageSource(source.getSource(),

new CropImageFilter(j*width/(n), i*height/n, (width/(n))+1, height/n)));

button[i][j].setIcon(new ImageIcon(image));

}      }}

}

TABLE 7: This here source code is take a before picture and define as icon for translate, make e crop image of source code. This code I bought from Java Book.  Therefore, These are to be show a similarities.

class actionlistener implements ActionListener{                           TABLE 8

public void actionPerformed(ActionEvent e) {

JButton BtnConnect=(JButton)(e.getSource());

Dimension size = BtnConnect.getSize();

ImageIcon icon =new ImageIcon();

int XAxis = BtnConnect.getX();

int YAxis = BtnConnect.getY();

int x = XAxis / size.width;

int y = YAxis / size.height;

int row = x;

x = y;

y=row;

TABLE 8: I write it previously tell about descriptions of my algorithms. This here buttons keep the address of button array’ and keep icons and descriptions of necessary coordinate and define are variables. It’s were of an X and Y coordinate-axis. These are necessary for my Puzzle Game Algorithms.

if(button[x+1][y].getText()==”5555″){                                     TABLE 9

button[x][y].setText(button[x+1][y].getText());

button[x+1][y].setText(“”);

button [x][y].setEnabled(false);

icon= (ImageIcon) button [x][y].getIcon();

button[x][y].setIcon(button [x+1][y].getIcon());

button[x+1][y].setIcon(icon);

button[x+1][y].setEnabled(true);

}else

if(button[x-1][y].getText()==”5555″){

button[x][y].setText(button[x-1][y].getText());

button[x-1][y].setText(“”);

button [x][y].setEnabled(false);

icon= (ImageIcon) button [x][y].getIcon();

button[x][y].setIcon(button [x-1][y].getIcon());

button[x-1][y].setIcon(icon);

button[x-1][y].setEnabled(true);

}else

if(button[x][y-1].getText()==”5555″){

button[x][y].setText(button[x][y-1].getText());

button[x][y-1].setText(“”);

button [x][y].setEnabled(false);

icon= (ImageIcon) button [x][y].getIcon();

button[x][y].setIcon(button [x][y-1].getIcon());

button[x][y-1].setIcon(icon);

button[x][y-1].setEnabled(true);

}

}

}

}

}

TABLE 9: My algorithm is a very simple and very useful for Puzzle Game. I created a complete buttons in the Puzzle Game Window. Changeable buttons are one by one check it moves the right, left, up, down aspects.  Clicking one of these, after swap an icons betweens are with clicked button and changeable button. This way provides moves are buttons. I create outmost buttons are enable (false), because not existing error at corner buttons and side buttons. I passed in front of the make error with this way. In my opinion, this way is practice resolution and a useful method. Good visualization, interesting design.

———————————————————————————————————

———————————————————————————————————

Temperature conversion

( Celcius to Fahrenheit & Fahrenheit to Celcius )

———————————————————————————————————

.

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class yasindemir extends JFrame

{

private JRadioButton a = new JRadioButton(“Celcius to Fahrenheit”);

private JRadioButton b = new JRadioButton(“Fahrenheit to Celcius”);

private JTextField myCelciusTF = new JTextField(10);

private JTextField myFahrenheitTF = new JTextField(10);

private JTextField myCelciusTF1 = new JTextField(10);

private JTextField myFahrenheitTF1 = new JTextField(10);

ButtonGroup bg=new ButtonGroup();

public yasindemir()

{

JFrame frame=new JFrame(“Homework3”);

JPanel p = new JPanel();

ButtonGroup bg=new ButtonGroup();

a.addActionListener(new rbListener());

b.addActionListener(new rbListener());

p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),”Temperature conversion:”));

p .setLayout(new GridLayout(2,1));

bg.add(a);

p .add(a);

bg.add(b);

p .add(b);

a.setSelected(false);

frame.add(p);

setContentPane(p );

setTitle(“Homework3”);

pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

class rbListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

if(a.isSelected())

{

this.window1(a);

}

if(b.isSelected() )

{

this.window2(b);

}

}

private void window1(JRadioButton a)

{

JLabel k= new JLabel(“Temperature in Celcius:”);

JButton convertbnt    = new JButton(“Convert”);

JLabel l= new JLabel(“Temperature in Fahrenheit:”);

JPanel p1= new JPanel();

convertbnt.addActionListener(new btnListener1());

p1.setLayout( new FlowLayout());

p1.add(k);

p1.add(myCelciusTF);

p1.add(convertbnt);

p1.add(l);

p1.add(myFahrenheitTF);

setContentPane(p1);

setTitle(“Celcius-Fahrenheit Converter”);

setLayout(new FlowLayout());

pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class btnListener1 implements ActionListener{

public void actionPerformed(ActionEvent e){

String y = myCelciusTF.getText();

int Celcius= Integer.parseInt(y);

int Fahrenheit= ((Celcius * 9) /5) + 32;

myFahrenheitTF.setText(” ” + Fahrenheit);

}

}

private void window2(JRadioButton b)

{

JLabel k1 = new JLabel(“Temperature in Fahrenheit:”);

JButton convertbnt1    = new JButton(“Convert”);

JLabel l1 = new JLabel(“Temperature in Celsius:”);JPanel p2 = new JPanel();

convertbnt1.addActionListener(new btnListener2() );

p2.setLayout( new FlowLayout() );

p2.add(k1);

p2.add(myCelciusTF1);

p2.add(convertbnt1);

p2.add(l1);

p2.add(myFahrenheitTF1);

setContentPane(p2);

setTitle(“Celcius-Fahrenheit Converter”);

pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

class btnListener2 implements ActionListener{

public void actionPerformed(ActionEvent e){

String z = myCelciusTF1.getText();

int Fahrenheit= Integer.parseInt(z);

int Celcius= ((5*(Fahrenheit – 32))/9);

myFahrenheitTF1.setText(” ” + Celcius);

}

}

}

public static void main(String[] args)

{

yasindemir window = new yasindemir();

window.setSize(300,120);

window.setVisible(true);

}

.

———————————————————————————————————

APPLET-JRAME CONVERSION

———————————————————————————————————

.

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class javahomework extends JFrame  {

private Font f;

private JTextField plain1 = new JTextField(“Write something …”,15);

private JTextField italic1   = new JTextField(15);

private JTextField bold1   = new JTextField(15);

public javahomework()

{

JButton convertBtn = new JButton(“Convert”);

convertBtn.addActionListener(new ConvertBtnListener());

JPanel appletjframe = new JPanel();

appletjframe.setLayout(new FlowLayout());

appletjframe.add(new JLabel(“plain :”));

appletjframe.add(plain1);

appletjframe.add(convertBtn);

appletjframe.add(new JLabel(“italic :”));

appletjframe.add(italic1);

appletjframe.add(new JLabel(“bold :”));

appletjframe.add(bold1);

setContentPane(appletjframe);

setTitle(“APPLET-JRAME CONVERSION”);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

}

class ConvertBtnListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String y = plain1.getText();

italic1.setText(y);

f = new Font (“TimesRoman”,  Font.ITALIC, 14);

italic1.setFont (f);

bold1.setText(y);

f = new Font (“TimesRoman”, Font.BOLD , 14);

bold1.setFont (f);

}}

public static void main(String[] args)   {

javahomework window = new javahomework();

window.setVisible(true);

}

}

——–OUTPUT——–


.

———————————————————————————————————

Basit bir hesap makinası yapmak

———————————————————————————————————

.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class calculater extends JFrame {

JPanel p = new JPanel(new FlowLayout());
JPanel bgPanel = new JPanel(new GridLayout(6,1));

ButtonGroup bg = new ButtonGroup();
JRadioButton rAdd = new JRadioButton(“Addition ( + )”);
JRadioButton rSub = new JRadioButton(“Subtraction ( – )”);
JRadioButton rMul = new JRadioButton(“Multiplication ( * )”);
JRadioButton rDiv = new JRadioButton(“Division ( / )”);
JRadioButton rmax = new JRadioButton(“Maximum”);
JRadioButton rsqrt = new JRadioButton(“Sqrt”);

JTextField tf1 = new JTextField(15);
JTextField tf2 = new JTextField(15);
JTextArea taResult = new JTextArea(15,15);

JButton btn = new JButton(“=”);

calculater(){
bg.add(rAdd);
bg.add(rSub);
bg.add(rMul);
bg.add(rDiv);
bg.add(rmax);
bg.add(rsqrt);
bgPanel.add(rAdd);
bgPanel.add(rSub);
bgPanel.add(rMul);
bgPanel.add(rDiv);
bgPanel.add(rmax);
bgPanel.add(rsqrt);
p.add(bgPanel);
p.add(tf1);
p.add(tf2);
p.add(btn);
p.add(taResult);
btn.addActionListener(new btnListener());
setContentPane(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(“A Simple Calculator”);
setVisible(true);
pack();
}

class btnListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
float x = Float.parseFloat(tf1.getText());
float y = Float.parseFloat(tf2.getText());

if(rAdd.isSelected())
{
taResult.append(x+ ” + ” +y+ ” = ” + ( x+y ) + “\n”);
}

else if(rSub.isSelected())
{
taResult.append(x+ ” – ” +y+ ” = ” + ( x-y ) + “\n”);
}

else if(rMul.isSelected())
{
taResult.append(x+ ” * ” +y+ ” = ” + ( x*y ) + “\n”);
}

else if(rDiv.isSelected())
{
taResult.append(x+ ” / ” +y+ ” = ” + ( x/y ) + “\n”);

}
else if(rmax.isSelected())
{
taResult.append(“Maximum is :” +Math.max(x,y)+ “\n”);
}
else if(rsqrt.isSelected())
{
taResult.append(“for first number:”+Math.sqrt(x)+”for second number:”+Math.sqrt(y)+”\n”);
}
}
}

public static void main(String[] args) {
new calculater();
}
}

.

Yasin DEMİR