View difference between Paste ID: sHAeB772 and CmtVWDKf
SHOW: | | - or go back to the newest paste.
1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5
package testmisc;
6
7
import java.awt.Image;
8
import java.awt.Point;
9
import java.awt.event.MouseEvent;
10
import java.io.IOException;
11
import java.util.ArrayList;
12
import java.util.Random;
13
import java.util.logging.Level;
14
import java.util.logging.Logger;
15
import javax.imageio.ImageIO;
16
import javax.swing.Icon;
17
import javax.swing.ImageIcon;
18
import javax.swing.JButton;
19
import javax.swing.event.MouseInputListener;
20
21
/**
22
 *
23
 * @author user
24
 */
25-
        JButton tab[][] = new JButton[rows][columns];
25+
26-
        ArrayList <Point> ships = new ArrayList<Point>();
26+
27
    /**
28
     * Creates new form NavalFrame
29
     */
30
    
31
    private int rows = 8;
32
    private int columns = 8;
33
    private int shipsNumber = 44;
34
    ArrayList <Point> ships ;
35
    
36
    public NavalFrame() {        
37
        MyJButton tab[][] = new MyJButton[rows][columns];
38
        ships = new ArrayList<Point>();
39
        Random ir = new Random();
40
        Random jr = new Random();
41
        
42
        int rowLimit = rows -1;
43
        int columnLimit = columns -1;
44
        for (int i = 0; i < shipsNumber; ) {
45
            Point nextPoint = new Point( (int)(Math.round( ir.nextDouble()*rowLimit)), 
46
                    (int)(Math.round( jr.nextDouble()*columnLimit)));
47
            if(!ships.contains(nextPoint)){
48
                ships.add(nextPoint);
49
                System.out.println("="+ships.get(i));
50-
                tab[i][j] = new JButton();
50+
51
            }else{
52
                System.out.println("Crashed="+nextPoint);
53
            }
54
        }
55
        
56
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
57
        getContentPane().setLayout(new java.awt.GridLayout(rows, columns));
58-
                tab[point.x][point.y].setIcon(new ImageIcon(img));
58+
59
        
60
        for (int i = 0; i < tab.length; i++) {
61
            for (int j = 0; j < tab[i].length; j++) {
62
                tab[i][j] = new MyJButton(new Point(i,j));
63
                this.add(tab[i][j]);
64
                    tab[i][j].addMouseListener(new java.awt.event.MouseAdapter() {
65
                        public void mouseClicked(java.awt.event.MouseEvent evt) {
66
                            ShipClicked(evt);
67-
   
67+
                        }   
68
                        private void ShipClicked(MouseEvent evt) {
69
                            MyJButton current = (MyJButton)evt.getSource();
70
                            if(ships.contains(current.currentPoistion)){
71
                                System.out.println("Boom!! ");
72
                            }else{
73
                                    System.out.println("FAIL!! ");
74
                            }
75
                            
76
                        }
77
            });
78
            }
79
        }
80
        try {
81
            Image img = ImageIO.read(getClass().getResource("/images.jpg"));
82
            for (int i = 0; i < shipsNumber; i++) {
83
                Point point = ships.get(i);
84
                tab[point.x][point.y].setIcon(new ImageIcon(img));            
85
            }
86
        } catch (IOException ex) {
87
            Logger.getLogger(NavalFrame.class.getName()).log(Level.SEVERE, null, ex);
88
        }
89
       pack();
90
       this.setSize(800, 600);
91
    }
92
93
    
94
95
    /**
96
     * This method is called from within the constructor to initialize the form.
97
     * WARNING: Do NOT modify this code. The content of this method is always
98
     * regenerated by the Form Editor.
99
     */
100
    @SuppressWarnings("unchecked")
101
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
102
    private void initComponents() {
103
104
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
105
        getContentPane().setLayout(new java.awt.GridLayout());
106
107
        pack();
108
    }// </editor-fold>                        
109
110
    /**
111
     * @param args the command line arguments
112
     */
113
    public static void main(String args[]) {
114
        /* Set the Nimbus look and feel */
115
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
116
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
117
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
118
         */
119
        try {
120
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
121
                if ("Nimbus".equals(info.getName())) {
122
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
123
                    break;
124
                }
125
            }
126
        } catch (ClassNotFoundException ex) {
127
            java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
128
        } catch (InstantiationException ex) {
129
            java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
130
        } catch (IllegalAccessException ex) {
131
            java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
132
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
133
            java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
134
        }
135
        //</editor-fold>
136
137
        /* Create and display the form */
138
        java.awt.EventQueue.invokeLater(new Runnable() {
139
            public void run() {
140
                new NavalFrame().setVisible(true);
141
            }
142
        });
143
    }
144
    // Variables declaration - do not modify                     
145
    // End of variables declaration                   
146
}
147
148
class MyJButton extends JButton{
149
    
150
    Point currentPoistion = new Point(0, 0);
151
    
152
    public MyJButton(Point currentPoistion){
153
        super();
154
        this.currentPoistion = currentPoistion;
155
    }
156
    
157
    public MyJButton(Icon icon, Point currentPoistion){
158
        super(icon);
159
        this.currentPoistion = currentPoistion;
160
    }        
161
}