Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package testmisc;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.event.MouseInputListener;
- /**
- *
- * @author user
- */
- public class NavalFrame extends javax.swing.JFrame {
- /**
- * Creates new form NavalFrame
- */
- private int rows = 8;
- private int columns = 8;
- private int shipsNumber = 44;
- ArrayList <Point> ships ;
- public NavalFrame() {
- MyJButton tab[][] = new MyJButton[rows][columns];
- ships = new ArrayList<Point>();
- Random ir = new Random();
- Random jr = new Random();
- int rowLimit = rows -1;
- int columnLimit = columns -1;
- for (int i = 0; i < shipsNumber; ) {
- Point nextPoint = new Point( (int)(Math.round( ir.nextDouble()*rowLimit)),
- (int)(Math.round( jr.nextDouble()*columnLimit)));
- if(!ships.contains(nextPoint)){
- ships.add(nextPoint);
- System.out.println("="+ships.get(i));
- i++;
- }else{
- System.out.println("Crashed="+nextPoint);
- }
- }
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- getContentPane().setLayout(new java.awt.GridLayout(rows, columns));
- for (int i = 0; i < tab.length; i++) {
- for (int j = 0; j < tab[i].length; j++) {
- tab[i][j] = new MyJButton(new Point(i,j));
- this.add(tab[i][j]);
- tab[i][j].addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- ShipClicked(evt);
- }
- private void ShipClicked(MouseEvent evt) {
- MyJButton current = (MyJButton)evt.getSource();
- if(ships.contains(current.currentPoistion)){
- System.out.println("Boom!! ");
- }else{
- System.out.println("FAIL!! ");
- }
- }
- });
- }
- }
- try {
- Image img = ImageIO.read(getClass().getResource("/images.jpg"));
- for (int i = 0; i < shipsNumber; i++) {
- Point point = ships.get(i);
- tab[point.x][point.y].setIcon(new ImageIcon(img));
- }
- } catch (IOException ex) {
- Logger.getLogger(NavalFrame.class.getName()).log(Level.SEVERE, null, ex);
- }
- pack();
- this.setSize(800, 600);
- }
- /**
- * 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")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- getContentPane().setLayout(new java.awt.GridLayout());
- pack();
- }// </editor-fold>
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NavalFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new NavalFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- // End of variables declaration
- }
- class MyJButton extends JButton{
- Point currentPoistion = new Point(0, 0);
- public MyJButton(Point currentPoistion){
- super();
- this.currentPoistion = currentPoistion;
- }
- public MyJButton(Icon icon, Point currentPoistion){
- super(icon);
- this.currentPoistion = currentPoistion;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement