View - Java: Nama: Muhammad Difky Daniartha Nim: 2015354028 Kelas: 3B TRPL
View - Java: Nama: Muhammad Difky Daniartha Nim: 2015354028 Kelas: 3B TRPL
View - Java: Nama: Muhammad Difky Daniartha Nim: 2015354028 Kelas: 3B TRPL
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;
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;
@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;
@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