0% found this document useful (0 votes)
53 views

View - Java: Nama: Muhammad Difky Daniartha Nim: 2015354028 Kelas: 3B TRPL

The document contains code for a puzzle game application written in Java. It includes classes for the view (View.java), controller (ControllerABC.java), and button model (ModelButton.java). The controller class handles button clicks and shuffling the letters, while the view class sets up the GUI and button panel. The button model class defines buttons that can swap positions with adjacent buttons.

Uploaded by

Thanatos Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

View - Java: Nama: Muhammad Difky Daniartha Nim: 2015354028 Kelas: 3B TRPL

The document contains code for a puzzle game application written in Java. It includes classes for the view (View.java), controller (ControllerABC.java), and button model (ModelButton.java). The controller class handles button clicks and shuffling the letters, while the view class sets up the GUI and button panel. The button model class defines buttons that can swap positions with adjacent buttons.

Uploaded by

Thanatos Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Nama : Muhammad Difky Daniartha

Nim : 2015354028
Kelas : 3B TRPL

View.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class View extends JFrame{


private JButton btnKeluar,btnUlang;
private JPanel panelTengah;
public View() {
//TODO Auto-generated constructor stub
super("Puzzel ABC");
JLabel lblJudul=new JLabel("Game Puzzle ABC");
lblJudul.setHorizontalAlignment(SwingConstants.CENTER);
lblJudul.setFont(new Font("TimesRoman",Font.BOLD,24));
this.getContentPane().add(lblJudul,BorderLayout.NORTH);

btnKeluar=new JButton("Keluar");
btnKeluar.setFont(new Font("TimesRoman",Font.PLAIN,16));
btnUlang=new JButton("Ulang");
btnUlang.setFont(new Font("TimesRoman",Font.PLAIN,16));
JPanel panelBawah=new JPanel();
panelBawah.setLayout(new GridLayout(1,2,4,4));
panelBawah.add(btnUlang);
panelBawah.add(btnKeluar);
this.getContentPane().add(panelBawah,BorderLayout.SOUTH);

panelTengah=new JPanel();
panelTengah.setLayout(new GridLayout(4,4,0,0));
this.getContentPane().add(panelTengah,BorderLayout.CENTER);
setSize(500,600);
setVisible(true);
}
public JButton getBtnKeluar() {
return btnKeluar;
}
public JButton getBtnUlang() {
return btnUlang;
}
public JPanel getPanelTengah() {
return panelTengah;
}

ControllerABC.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

public class ControllerABC extends View {


private ModelButton [][] btnMain;
private String huruf[]=
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",""};
private ArrayList<String>hurufAsal;
private Random r;
public ControllerABC() {
//TODO Auto-generated constructor stub
btnMain=new ModelButton[4][4];
int index=0;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
btnMain[i][j]= new ModelButton(huruf[index]);
btnMain[i][j].addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cekgame();
}
});
btnMain[i][j].setBtnKiri(new ModelButton("*"));
btnMain[i][j].setBtnKanan(new ModelButton("*"));
btnMain[i][j].setBtnAtas(new ModelButton("*"));
btnMain[i][j].setBtnBawah(new ModelButton("*"));
index++;
getPanelTengah().add(btnMain[i][j]);
}
}
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
if(j<3) {
btnMain[i][j].setBtnKanan(btnMain[i][j+1]);
}
if(j>0) {
btnMain[i][j].setBtnKiri(btnMain[i][j-1]);
}
if(i<3) {
btnMain[i][j].setBtnBawah(btnMain[i+1][j]);
}
if(i>0) {
btnMain[i][j].setBtnAtas(btnMain[i-1][j]);
}
}
}
acak();
getBtnUlang().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
acak();
}
});
getBtnKeluar().addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public void acak() {
hurufAsal = new ArrayList<String>();
r=new Random();
for(int i=0;i<huruf.length;i++) {hurufAsal.add(huruf[i]);}
int index;
int x=0;
for(int i=16;i>0;i--) {
index=r.nextInt(i);
huruf[x]=hurufAsal.get(index); //menggubah isi array menjadi
huruf acak
hurufAsal.remove(index);
x++;
}
index=0;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
btnMain[i][j].setText(huruf[index]);
index++;
}
}
}
public void cekgame() {
String hasil="";
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
hasil=hasil+btnMain[i][j].getText();
}
}
if(hasil.equals("ABCDEFGHIJKLMNO")) {
JOptionPane.showMessageDialog(getParent(), "Game!!!");
}
}
public static void main(String[] args) {
new ControllerABC();
}
}

ModelButton.java
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class ModelButton extends JButton {


private ModelButton btnKiri,btnKanan,btnAtas,btnBawah;
public ModelButton(String s) {
//TODO Auto-generated constructor stub
setText(s);
setFont(new Font("TimesRoman", Font.BOLD,44));
addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(getBtnKiri().getText().equals("")) {
getBtnKiri().setText(getText());
setText("");
}else if(getBtnKanan().getText().equals("")) {
getBtnKanan().setText(getText());
setText("");
}else if(getBtnAtas().getText().equals("")) {
getBtnAtas().setText(getText());
setText("");
}else if(getBtnBawah().getText().equals("")) {
getBtnBawah().setText(getText());
setText("");
}
}
});
}
public ModelButton getBtnKiri() {
return btnKiri;
}
public void setBtnKiri(ModelButton btnKiri) {
this.btnKiri = btnKiri;
}
public ModelButton getBtnKanan() {
return btnKanan;
}
public void setBtnKanan(ModelButton btnKanan) {
this.btnKanan = btnKanan;
}
public ModelButton getBtnAtas() {
return btnAtas;
}
public void setBtnAtas(ModelButton btnAtas) {
this.btnAtas = btnAtas;
}
public ModelButton getBtnBawah() {
return btnBawah;
}
public void setBtnBawah(ModelButton btnBawah) {
this.btnBawah = btnBawah;
}

OUT PUT

You might also like