document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *  Final Project PBO
  3.  *  Class MenuFrame
  4.  *  
  5.  *  @author Aimar Wibowo
  6.  *  @author Timotius Wirawan
  7.  *  @version 11 Januari 2021
  8.  */
  9.  
  10. import java.awt.*;
  11. import java.awt.geom.*;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.ObjectOutputStream;
  19. import java.awt.event.*;
  20. import javax.sound.sampled.AudioInputStream;
  21. import javax.sound.sampled.AudioSystem;
  22. import javax.sound.sampled.Clip;
  23. import javax.sound.sampled.LineUnavailableException;
  24. import javax.swing.*;
  25. import java.util.*;
  26.  
  27. public class Tower extends JPanel implements MouseListener,MouseMotionListener{
  28.     ImagePanel imagePanel= new ImagePanel();
  29.     int moveCount=0;
  30.     int numberOfpeg;
  31.     Stack<Rectangle2D.Double> stack[]= new Stack[3];
  32.     int disk[]={2,3,4,5,6,7,8,9,10,11,12,13,14};
  33.    
  34.     Stack <Color>colorOfDisk[]=new Stack[3];
  35.     Rectangle2D.Double top=null;
  36.     Color topColor= null;
  37.     double ax,ay,height,width;
  38.     boolean dragable=false;
  39.     boolean begin=false;
  40.    
  41.     JLabel bestMove,move,noOfPeg;
  42.     JLabel bestMovetxt,moveTxt,noOfPegTxt;
  43.    
  44.     JButton level=new JButton(imagePanel.imagelevel);
  45.     JButton back= new JButton(imagePanel.imageBack);
  46.     JButton restart= new JButton(imagePanel.imageRestart);
  47.     JButton rules = new JButton(imagePanel.imageHelp);
  48.     JButton save = new JButton(imagePanel.imageOption);
  49.     JButton exit = new JButton(imagePanel.imageexit);
  50.     JLabel levno=new JLabel();
  51.     JPanel Separator1= new JPanel();
  52.     JPanel Separator2= new JPanel();
  53.     JPanel Separator3= new JPanel();
  54.     JPanel Separator4= new JPanel();
  55.    
  56.     JLabel poleA  = new JLabel("Pole A");
  57.     JLabel poleB = new JLabel("Pole B");
  58.     JLabel poleC = new JLabel("Pole C");
  59.    
  60.     public Tower(int l){
  61.         this.setLayout(null);
  62.         bestMove = new JLabel("Best Move");
  63.         move = new JLabel("Player Move");
  64.         noOfPeg= new JLabel("Number of Disks");
  65.        
  66.         noOfPegTxt = new JLabel("0");
  67.         bestMovetxt= new JLabel("0");
  68.         moveTxt= new JLabel("0");
  69.        
  70.         Font labelFont = new Font("Arial",Font.PLAIN, 20);
  71.         noOfPeg.setFont(labelFont);
  72.         move.setFont(labelFont);
  73.         bestMove.setFont(labelFont);
  74.        
  75.         Font resultFont = new Font("Arial",Font.BOLD,30);
  76.         noOfPegTxt.setFont(resultFont);
  77.         moveTxt.setFont(resultFont);
  78.         bestMovetxt.setFont(resultFont);
  79.        
  80.         JPanel panel1 = new JPanel();
  81.         panel1.setLayout(null);
  82.         panel1.setBounds(30, 120,250, 120);
  83.         noOfPeg.setBounds(50,10,150,40);
  84.         noOfPegTxt.setBounds(110,60,150,40);
  85.         panel1.add(noOfPeg);
  86.         panel1.add(noOfPegTxt);
  87.         this.add(panel1);
  88.        
  89.         JPanel panel2 = new JPanel();
  90.         panel2.setLayout(null);
  91.         panel2.setBounds(325,120,250, 120);
  92.         bestMove.setBounds(70,10,150,40);
  93.         bestMovetxt.setBounds(110,60,150,40);
  94.         panel2.add(bestMovetxt);
  95.         panel2.add(bestMove);
  96.         this.add( panel2);
  97.        
  98.         JPanel panel3 = new JPanel();
  99.         panel3.setLayout(null);
  100.         panel3.setBounds(620,120,250, 120);
  101.         move.setBounds(70,10,150,40);
  102.         moveTxt.setBounds(110,60,150,40);
  103.         panel3.add(move);
  104.         panel3.add(moveTxt);
  105.         this.add( panel3);
  106.         level.setBounds(30,40,150, 45);
  107.         level.setBorder(null);
  108.         this.add(level);
  109.         back.setBounds(539,40,65, 45);
  110.         back.setBorder(null);
  111.         this.add(back);
  112.         restart.setBounds(606,40,65, 45);
  113.         restart.setBorder(null);
  114.         this.add(restart);
  115.         rules.setBounds(673,40,65, 45);
  116.         rules.setBorder(null);
  117.         this.add(rules);
  118.         save.setBounds(740,40,65, 45);
  119.         save.setBorder(null);
  120.         this.add(save);
  121.         exit.setBounds(807,40,65, 45);
  122.         exit.setBorder(null);
  123.         this.add(exit);
  124.         Font labelNo = new Font("Arial",Font.PLAIN, 25);
  125.         levno.setForeground(Color.white);
  126.         level.setLayout(null);
  127.         levno.setBounds(110,12, 30, 20);
  128.         levno.setFont(labelFont);
  129.         level.add(levno);
  130.        
  131.         Separator1.setBounds(0,573,20, 17);
  132.         this.add(Separator1);
  133.         Separator1.setBackground(Color.GRAY);
  134.         Separator1.setBorder(null);
  135.         Separator2.setBounds(279,573,39, 17);
  136.         this.add(Separator2);
  137.         Separator2.setBackground(Color.GRAY);
  138.         Separator2.setBorder(null);
  139.         Separator3.setBounds(577,573,39, 17);
  140.         this.add(Separator3);
  141.         Separator3.setBackground(Color.GRAY);
  142.         Separator3.setBorder(null);
  143.         Separator4.setBounds(874,573,20, 17);
  144.         this.add(Separator4);
  145.         Separator4.setBackground(Color.GRAY);
  146.         Separator4.setBorder(null);
  147.        
  148.         Font pole = new Font("Arial",Font.BOLD,30);
  149.         poleA.setForeground(Color.white);
  150.         poleA.setBounds(105,600, 100, 30);
  151.         poleA.setFont(pole);
  152.         this.add(poleA);
  153.         poleB.setForeground(Color.white);
  154.         poleB.setBounds(405,600, 100, 30);
  155.         poleB.setFont(pole);
  156.         this.add(poleB);
  157.         poleC.setForeground(Color.white);
  158.         poleC.setBounds(705,600, 100, 30);
  159.         poleC.setFont(pole);
  160.         this.add(poleC);
  161.        
  162.         begin = true;
  163.         System.out.println("inside Tower: " + l);
  164.         init(l);
  165.         this.addMouseListener(this);
  166.         this.addMouseMotionListener(this);
  167.         addTowerActionListener();
  168.     }
  169.    
  170.     public void addTowerActionListener(){
  171.         exit.addActionListener(new ActionListener() {
  172.             @Override
  173.             public void actionPerformed(ActionEvent e) {
  174.                 GameMain.buttonmusic();
  175.                 System.exit(0);
  176.             }
  177.         });
  178.         back.addActionListener(new backPage1());
  179.         back.addActionListener(new ActionListener() {
  180.             @Override
  181.             public void actionPerformed(ActionEvent e) {
  182.                 GameHold.sevisiblex();
  183.                 init(1);
  184.                 moveCount=0;
  185.                 String str = Integer.toString(moveCount);
  186.                 moveTxt.setText(str);
  187.                 GameMain.music();
  188.             }
  189.         });
  190.        
  191.         restart.addActionListener(new ActionListener() {
  192.            
  193.             @Override
  194.             public void actionPerformed(ActionEvent e) {
  195.                 GameMain.buttonmusic();
  196.                 init(Integer.parseInt(levno.getText()));
  197.                 moveCount=0;
  198.                 String str = Integer.toString(moveCount);
  199.                 moveTxt.setText(str);
  200.             }
  201.         });
  202.        
  203.         rules.addActionListener(new ActionListener() {
  204.             @Override
  205.             public void actionPerformed(ActionEvent e) {
  206.                 GameMain.buttonmusic();
  207.                 System.out.println("Rules");
  208.                 HowToPlay newFrame = new HowToPlay();
  209.                 newFrame.setTitle("How To Play");
  210.                 newFrame.setSize(800,600);
  211.                 newFrame.setLocationRelativeTo(null);
  212.                 newFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  213.                 newFrame.setVisible(true); 
  214.                 newFrame.setResizable(false);
  215.             }
  216.         });
  217.  
  218.         save.addActionListener(new ActionListener() {
  219.            
  220.             @Override
  221.             public void actionPerformed(ActionEvent e) {
  222.                 GameMain.buttonmusic();
  223.                 System.out.println("saved");
  224.                 saveMethod();  
  225.             }
  226.         });
  227.     }
  228.    
  229.    
  230.     public static void music(){
  231.         try {
  232.             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sounds/move.wav").getAbsoluteFile());
  233.             Clip clip = AudioSystem.getClip();
  234.             clip.open(audioInputStream);
  235.             clip.start();
  236.    
  237.         } catch(Exception ex) {
  238.             System.out.println("Error with playing sound.");
  239.             ex.printStackTrace();
  240.         }
  241.     }
  242.    
  243.     public class ImagePanel extends JPanel{
  244.         private ImageIcon imageIcon = new ImageIcon("images/untitle.jpg");
  245.         private ImageIcon imagelevel = new ImageIcon("images/level.jpg");
  246.         private ImageIcon imageBack = new ImageIcon("images/backgame.jpg");
  247.         private ImageIcon imageRestart = new ImageIcon("images/restart.jpg");
  248.         private ImageIcon imageHelp = new ImageIcon("images/helpgame.jpg");
  249.         private ImageIcon imageexit = new ImageIcon("images/exitgame.jpg");
  250.         private ImageIcon imageOption = new ImageIcon("images/optiongame.jpg");
  251.         private Image image = imageIcon.getImage();
  252.         protected void paintComponent(Graphics g) {
  253.             super.paintComponent(g);
  254.             g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
  255.         }
  256.      }
  257.  
  258.     public void init(int level){
  259.         stack[0]=new Stack<Rectangle2D.Double>();
  260.         stack[1]=new Stack<Rectangle2D.Double>();
  261.         stack[2]=new Stack<Rectangle2D.Double>();
  262.         moveCount=0;
  263.         int noOfPeg=disk[level-1];
  264.         levno.setText(Integer.toString(level));
  265.         Color pegColor[]={Color.black,Color.red,Color.yellow,Color.cyan,Color.blue,Color.green,Color.orange,Color.pink,Color.MAGENTA,Color.lightGray};
  266.        
  267.         colorOfDisk[0]=new Stack<Color>();
  268.         colorOfDisk[1]=new Stack<Color>();
  269.         colorOfDisk[2]=new Stack<Color>();
  270.  
  271.         for(int i=0;i<noOfPeg;i++){
  272.             Rectangle2D.Double rect=new Rectangle2D.Double();
  273.             System.out.println("getWidth: " + getWidth());
  274.             double x = getWidth()/6;   
  275.             if(x==0)
  276.                 x=150;
  277.             System.out.println("x: " + x);
  278.             double wr= noOfPeg*25-20*i;
  279.             rect.setFrame (x-wr/2,553-i*20,wr,20);
  280.             stack[0].push(rect);
  281.             colorOfDisk[0].push(pegColor[i]);
  282.         }
  283.    
  284.         top=null;
  285.         topColor=null;
  286.         ax=0.0;
  287.         ay=0.0;
  288.         height=0.0;
  289.         width = 0.0;
  290.         dragable= false;
  291.         repaint();
  292.        
  293.         String str = Integer.toString(noOfPeg);
  294.         noOfPegTxt.setText(str);
  295.        
  296.         int best= (int) (Math.pow(2,noOfPeg)-1);
  297.         str = Integer.toString(best);
  298.         bestMovetxt.setText(str);
  299.         System.out.println("inside init: " + level);
  300.         System.out.println("No of disk: " + noOfPeg);
  301.     }
  302.    
  303.     public void saveMethod(){
  304.         try{
  305.             DataOutputStream output= new DataOutputStream(new FileOutputStream("images/input.txt"));
  306.             output.writeInt(Integer.parseInt(levno.getText()));
  307.             output.close();
  308.             DataInputStream input =new DataInputStream(new FileInputStream("images/input.txt"));
  309.             while (true) {
  310.                 System.out.println(input.readInt());
  311.             }
  312.            
  313.         }
  314.         catch(IOException ex){
  315.             System.out.println("Error in Reading Data");
  316.         }
  317.     }
  318.    
  319.     public void paintComponent(Graphics g){
  320.         Graphics2D g1=(Graphics2D)g;
  321.         g1.setColor(Color.GRAY);
  322.         g1.fillRect(0,0, getWidth(),getHeight());
  323.  
  324.         int baseX = getWidth()/6;
  325.         int baseY1 = getHeight()-10*40;
  326.         int baseY2 = getHeight()-90;
  327.        
  328.         g1.setColor(Color.WHITE);
  329.         g1.setStroke(new BasicStroke(15));
  330.        
  331.         g1.drawLine(baseX, baseY1, baseX, baseY2);
  332.         g1.drawLine(3*baseX, baseY1, 3*baseX, baseY2);
  333.         g1.drawLine(5*baseX, baseY1, 5*baseX, baseY2);
  334.        
  335.         g1.drawLine(0,baseY2,getWidth(),baseY2);
  336.         g1.setStroke(new BasicStroke(1));
  337.         g1.setColor(topColor);
  338.        
  339.         if(dragable==true && top!=null)
  340.               g1.fill(top);
  341.        
  342.         drawTower(g1,0);
  343.         drawTower(g1,1);
  344.         drawTower(g1,2);
  345.     }
  346.    
  347.     private void drawTower(Graphics2D g,int n){
  348.         if(stack[n].empty()==false){
  349.             for(int i=0;i<stack[n].size();i++){
  350.                 g.setColor(colorOfDisk[n].get(i));
  351.                 g.fill(stack[n].get(i));
  352.             }
  353.         }
  354.     }
  355.    
  356.     private int currentTower(Point p){
  357.         Rectangle2D.Double rA= new Rectangle2D.Double();
  358.         Rectangle2D.Double rB= new Rectangle2D.Double();
  359.         Rectangle2D.Double rC= new Rectangle2D.Double();
  360.        
  361.         rA.setFrame(0,0,getWidth()/3,getHeight());
  362.         rB.setFrame(getWidth()/3,0,getWidth()/3,getHeight());
  363.         rC.setFrame(2*getWidth()/3,0,getWidth()/3,getHeight());
  364.        
  365.         if(rA.contains(p))
  366.             return 0;
  367.         else if(rB.contains(p))
  368.             return 1;
  369.         else if(rC.contains(p))
  370.             return 2;
  371.         else
  372.             return -1; 
  373.     }
  374.    
  375.     public void mousePressed(MouseEvent e){
  376.         Point p= e.getPoint();
  377.         //System.out.println(p.getY());
  378.         int n= currentTower(p);
  379.         if(!stack[n].empty()){
  380.             top=stack[n].peek();
  381.             if(top.contains(p)){
  382.                 top=stack[n].pop();
  383.                 topColor=colorOfDisk[n].pop();
  384.                 ax=top.getX();
  385.                 ay=top.getY();
  386.                 width=p.getX()-ax;
  387.                 dragable=true;
  388.             }
  389.             else{
  390.                 top=null;
  391.                 topColor=Color.BLACK;
  392.                 dragable=false;
  393.             }
  394.         }
  395.     }
  396.  
  397.     @Override
  398.     public void mouseDragged(MouseEvent ev) {
  399.         // TODO Auto-generated method stub
  400.         int cx=ev.getX();
  401.         int cy=ev.getY();
  402.         if(top!=null && dragable==true){
  403.             top.setFrame(cx-width,cy-height,top.getWidth(),top.getHeight());
  404.             repaint();
  405.          }
  406.     }
  407.  
  408.     @Override
  409.     public void mouseMoved(MouseEvent arg0) {
  410.         // TODO Auto-generated method stub
  411.        
  412.     }
  413.  
  414.     @Override
  415.     public void mouseClicked(MouseEvent arg0) {
  416.         // TODO Auto-generated method stub
  417.        
  418.     }
  419.  
  420.     @Override
  421.     public void mouseEntered(MouseEvent arg0) {
  422.         // TODO Auto-generated method stub
  423.        
  424.     }
  425.  
  426.     @Override
  427.     public void mouseExited(MouseEvent arg0) {
  428.         // TODO Auto-generated method stub
  429.        
  430.     }
  431.  
  432.     @Override
  433.     public void mouseReleased(MouseEvent ev){
  434.         if(top!=null && dragable==true){
  435.             int tower=currentTower(ev.getPoint());
  436.             double x,y;
  437.             if(!stack[tower].empty()){
  438.                 if(stack[tower].peek().getWidth()>top.getWidth()){
  439.                     y=stack[tower].peek().getY()-20;
  440.                     moveCount++;
  441.                     System.out.println(moveCount);
  442.                     String str = Integer.toString(moveCount);
  443.                     moveTxt.setText(str);
  444.                     music();
  445.                 }
  446.                 else{
  447.                     JOptionPane.showMessageDialog(this,"Wrong Move","Tower Of Hanoi",JOptionPane.ERROR_MESSAGE);
  448.                     tower=currentTower(new Point((int)ax,(int)ay));
  449.                     if(!stack[tower].empty())
  450.                         y=stack[tower].peek().getY()-20;
  451.                     else
  452.                         y=getHeight()-118;
  453.                }
  454.             }
  455.             else{
  456.                 y=getHeight()-118;
  457.                 moveCount++;
  458.                 System.out.println(moveCount);
  459.                 String str = Integer.toString(moveCount);
  460.                 moveTxt.setText(str);
  461.                 music();
  462.             }
  463.             x=(int)(getWidth()/6+(getWidth()/3)*tower-top.getWidth()/2);
  464.             top.setFrame(x,y,top.getWidth(),top.getHeight());
  465.             stack[tower].push(top);
  466.             colorOfDisk[tower].push(topColor);
  467.            
  468.             top=null;
  469.             topColor=Color.black;
  470.             dragable = false;
  471.             repaint();
  472.         }
  473.        
  474.         if(stack[0].empty() && stack[1].empty()){
  475.             int best = Integer.parseInt(bestMovetxt.getText());
  476.             int playerMove = Integer.parseInt(moveTxt.getText());
  477.             System.out.println("Best: " + best);
  478.             System.out.println("Player Move: " + playerMove);
  479.             if(best==playerMove){
  480.                 int l = Integer.parseInt(levno.getText());
  481.                 GameMain.buttonsuccess();
  482.                 String msg = "You Successfully Complete this level\\nNext Level: " + (l+1);
  483.                 JOptionPane.showMessageDialog(this,msg,"Tower Of Hanoi",JOptionPane.INFORMATION_MESSAGE);
  484.                
  485.                 System.out.println("Level: " + l);
  486.                 l++;
  487.                 levno.setText(Integer.toString(l));
  488.                 init(l);
  489.                 moveTxt.setText("0");
  490.             }
  491.             else{
  492.                 int extra=playerMove-best;
  493.                 GameMain.buttonfail();
  494.                 String msg="Failed !! You Took "+ extra + " Extra Moves";
  495.                 JOptionPane.showMessageDialog(this,msg,"Tower Of Hanoi",JOptionPane.INFORMATION_MESSAGE);
  496.                 JOptionPane.showMessageDialog(this,"Try Again!!!","Tower Of Hanoi",JOptionPane.INFORMATION_MESSAGE);
  497.                 init(Integer.parseInt(levno.getText()));
  498.                 moveCount=0;
  499.                 String str = Integer.toString(moveCount);
  500.                 moveTxt.setText(str);
  501.             }
  502.         }
  503.        
  504.         if(Integer.parseInt(levno.getText())>=10){
  505.             JOptionPane.showMessageDialog(this,"Congratulation !! You Complete all the level!!! ","Tower of Hanoi",JOptionPane.INFORMATION_MESSAGE);
  506.             //GameHold.sevisiblex();
  507.             init(1);
  508.             moveCount=0;
  509.             String str = Integer.toString(moveCount);
  510.             moveTxt.setText(str);
  511.            
  512.         }
  513.     }
  514. }
');