Vous êtes sur la page 1sur 15

PROGRAMMING I

M0864
Dosen :

D3091 EKO PRABOWO HERU KURNIANTO, S.SI., M.ENG

NIM Nama Kelas

: 1201004355 : Andri maulan suryakusumah : 12PAA

20 Juli 2011

1.

Buatlah program GUI untuk menghasilkan output seperti pada gambar berikut ini !

/* * NewJFrame.java * * Created on Jul 16, 2011, 3:37:35 AM */ /** * * @author andry */ public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); }

@SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jPanel4 = new javax.swing.JPanel(); jPanel5 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jPanel6 = new javax.swing.JPanel(); jPanel8 = new javax.swing.JPanel();

jPanel7 = new javax.swing.JPanel(); jPanel9 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setLayout(new java.awt.GridLayout(3, 0, 50, 0)); jPanel2.setLayout(new java.awt.GridLayout()); org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 150, Short.MAX_VALUE) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 23, Short.MAX_VALUE) ); jPanel2.add(jPanel3); jLabel1.setText("user ID"); jPanel2.add(jLabel1); jPanel2.add(jTextField1); jPanel1.add(jPanel2); jPanel4.setLayout(new java.awt.GridLayout()); org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup( jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 150, Short.MAX_VALUE) ); jPanel5Layout.setVerticalGroup( jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 23, Short.MAX_VALUE) );

jPanel4.add(jPanel5); jLabel2.setText("password"); jPanel4.add(jLabel2); jPanel4.add(jTextField2); jPanel1.add(jPanel4); jPanel6.setLayout(new java.awt.GridLayout()); org.jdesktop.layout.GroupLayout jPanel8Layout = new org.jdesktop.layout.GroupLayout(jPanel8); jPanel8.setLayout(jPanel8Layout); jPanel8Layout.setHorizontalGroup( jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 150, Short.MAX_VALUE) ); jPanel8Layout.setVerticalGroup( jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 23, Short.MAX_VALUE) ); jPanel6.add(jPanel8); org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7); jPanel7.setLayout(jPanel7Layout); jPanel7Layout.setHorizontalGroup( jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 150, Short.MAX_VALUE) ); jPanel7Layout.setVerticalGroup( jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(0, 23, Short.MAX_VALUE) ); jPanel6.add(jPanel7); jPanel9.setLayout(new java.awt.GridLayout()); jButton1.setText("ok"); jPanel9.add(jButton1);

jButton2.setText("reset"); jPanel9.add(jButton2); jPanel6.add(jPanel9); jPanel1.add(jPanel6); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 451, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap(35, Short.MAX_VALUE)) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); }

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JPanel jPanel7; private javax.swing.JPanel jPanel8; private javax.swing.JPanel jPanel9; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; }

2.

Buatlah program GUI papan TacTicToe dengan memanfaatkan file gambar cross.gif

dan not.gif

seperti di bawah ini! (File terlampir)

import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class TicTacToe extends JApplet implements ActionListener { Container con = getContentPane(); JLabel ticTacToe = new JLabel("Tic Tac Toe"); JPanel displayPanel = new JPanel(new GridLayout(3,3)); JButton button1 = new JButton(" "); JButton button2 = new JButton(" "); JButton button3 = new JButton(" "); JButton button4 = new JButton(" "); JButton button5 = new JButton(" "); JButton button6 = new JButton(" "); JButton button7 = new JButton(" "); JButton button8 = new JButton(" "); JButton button9 = new JButton(" "); int count; String letter; boolean win; public void init() { con.setLayout (new FlowLayout()); //add buttons to the container

con.add(ticTacToe); con.add(button1); con.add(button2); con.add(button3); con.add(button4); con.add(button5); con.add(button6); con.add(button7); con.add(button8); con.add(button9); //Add The Action Listener To The Buttons button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); } public void actionPerformed(ActionEvent e) { count++; //Decide who's turn it is if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9) { letter = "X"; } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10) { letter = "O"; } /*Display X's or O's on the buttons*/ if(e.getSource() == button1) { button1.setText(letter); button1.setEnabled(false); } else if(e.getSource() == button2) { button2.setText(letter);

} else if(e.getSource() == button3) { button3.setText(letter); } else if(e.getSource() == button4) { button4.setText(letter); } else if(e.getSource() == button5) { button5.setText(letter); } else if(e.getSource() == button6) { button6.setText(letter); } else if(e.getSource() == button7) { button7.setText(letter); } else if(e.getSource() == button8) { button8.setText(letter); } else if(e.getSource() == button9) { button9.setText(letter); } if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "") { win = true; } else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "") { win = true; } else if(button7.getText() == button8.getText()

&& button8.getText() == button9.getText() && button7.getText() != "") { win = true; } //vertical wins else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "") { win = true; } else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "") { win = true; } else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "") { win = true; } //diagonal wins else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "") { win = true; } else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "") { win = true; } else { win = false; } if(win == true) { JOptionPane.showMessageDialog(null, letter + "wins"); } else if(count == 9 && win == false) {

JOptionPane.showMessageDialog(null, "Tie Game!"); } } }

3.

Buatlah program untuk menghasilkan tampilan silinder seperti di bawah! Gunakan class Graphic dan beberapa method darinya!

import java.awt.Graphics; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * tabung.java * * Created on Dec 28, 2010, 11:41:09 AM */ /**

* * @author user */ public class tabung extends javax.swing.JFrame { public void paint (Graphics g) { g.drawOval(100, 100, 100, 50); g.drawOval(100, 200, 100, 50); g.drawLine(100, 125, 100, 225); g.drawLine(200, 125, 200, 225); } /** Creates new form tabung */ public tabung() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new tabung().setVisible(true); }

}); } // Variables declaration - do not modify // End of variables declaration }

4.

Buatlah program untuk menghasilkan tampilan segi banyak (Segi Lima) seperti di bawah ini! Gunakan class : polygon, Graphics dan method : add, drawPolygon!
Petunjuk : Buatlah lingkaran dengan titik pusat di tengah window dan radius 80% dari tinggi atau lebar window. Tentukan 5 (lima) titik sudut segibanyak dengan membagi sudut lingkaran penuh 3600 menjadi lima bagian yang sama yaitu masing-masing 720, selanjutnya dengan jari-jari, cosinus, dan sinus masing-masing sudut dapat dihitung absis dan ordinat dari kelima titik sudut tersebut. Langkah berikutnya masukkan

absis dan ordinat tersebut ke dalam polygon dengan method add. Terakhir gunakan obyek class Graphic dan method drawPolygon untuk menggambar segilimanya. Beres

public class Main extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Polygon pol = new Polygon(); for(int i = 0 ; i>5 ; i++) pol.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 5)), (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 5)));

g.drawPolygon(pol); }

public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("segi banyak"); frame.setSize(350,200); frame.addWindowListener(new WindowAdapter()

{ public void windowsclosing(WindowEvent e) { System.exit(0); } }); Container ContentPane = frame.getContentPane(); ContentPane.add(new Main()); frame.setVisible(true); } }

Maaf pak gak bisa nongolin gambar nya padahal udah di frame.setVisible(true);