ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Download | Log | Files | Refs | README

Robot.java (3690B)


      1 /*
      2 Copyright 2008, 2009 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of Robottino.
      5 
      6     Robottino is free software; you can redistribute it and/or modify
      7     it under the terms of the GNU General Public License as published by
      8     the Free Software Foundation; either version 2 of the License, or
      9     (at your option) any later version.
     10 
     11     Robottino is distributed in the hope that it will be useful,
     12     but WITHOUT ANY WARRANTY; without even the implied warranty of
     13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14     GNU General Public License for more details.
     15 
     16     You should have received a copy of the GNU General Public License
     17     along with Robottino; if not, write to the Free Software
     18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 
     22 import javax.swing.*;
     23 import java.awt.event.*;
     24 import java.awt.*;
     25 
     26 public class Robot {
     27 	public static Main main = new Main();
     28 	boolean leftArm, rightArm, leftLeg, rightLeg;
     29 	String nomeRobot;
     30 	Robot precedenteRobot, prossimoRobot;
     31 	RobotFrame robotFrame;
     32 	Robot() {
     33 		this( false, false, false, false, "", main.list.ultimoRobot, null );
     34 	}
     35 	Robot( String nome ) {
     36 		this ( false, false, false, false, nome, main.list.ultimoRobot, null );
     37 	}
     38 	Robot( boolean la, boolean ra, boolean ll, boolean rl, String nome, Robot prec, Robot prox ) {
     39 		leftArm = la;
     40 		rightArm = ra;
     41 		leftLeg = ll;
     42 		rightLeg = rl;
     43 		nomeRobot = nome;
     44 		precedenteRobot = prec;
     45 		prossimoRobot = prox;
     46 		robotFrame = new RobotFrame( nome );
     47 		robotFrame.setSize( 350, 550 );
     48 		robotFrame.setVisible( true );
     49 		robotFrame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
     50 		robotFrame.addWindowListener(
     51 			new WindowAdapter() {
     52 				public void windowClosing( WindowEvent e ) {
     53 					int selezione = JOptionPane.showOptionDialog( Robot.this.robotFrame, "Eliminare il robottino " + Robot.this.getName() + " ?", "Robottino v4.0 - Cancellazione robottino", JOptionPane.YES_NO_OPTION,  JOptionPane.QUESTION_MESSAGE, null, new String[] { "Si", "No" }, "No" );
     54 					if ( selezione == JOptionPane.YES_OPTION ) {
     55 						main.list.deleteRobot( Robot.this.getName() );
     56 						robotFrame.dispose();
     57 					}
     58 					else {
     59 						try {
     60 							Thread.currentThread().sleep( 50 );
     61 						} catch ( Exception e2 ) {}
     62 						robotFrame.repaint();
     63 					}
     64 				}
     65 				public void windowActivated( WindowEvent e ) {
     66 					robotFrame.repaint();
     67 				}
     68 			}
     69 		);
     70 		robotFrame.addComponentListener(
     71 			new ComponentAdapter() {
     72 				public void componentMoved( ComponentEvent e ) {
     73 					try {
     74 						Thread.currentThread().sleep( 50 );
     75 					} catch ( Exception e2 ) {}
     76 					robotFrame.repaint();
     77 				}
     78 				public void componentResized( ComponentEvent e ) {
     79 					try {
     80 						Thread.currentThread().sleep( 50 );
     81 					} catch ( Exception e2 ) {}
     82 					robotFrame.repaint();            
     83 				}
     84 			}
     85 		);
     86 	}
     87 	void setLA( boolean la ) {
     88 		setPositions( la, this.rightArm, this.leftLeg, this.rightLeg );
     89 	}
     90 	void setRA( boolean ra ) {
     91 		setPositions( this.leftArm, ra, this.leftLeg, this.rightLeg );
     92 	}
     93 	void setLL( boolean ll ) {
     94 		setPositions( this.leftArm, this.rightArm, ll, this.rightLeg );
     95 	}
     96 	void setRL( boolean rl ) {
     97 		setPositions( this.leftArm, this.rightArm, this.leftLeg, rl );
     98 	}
     99 	void setPositions( boolean la, boolean ra, boolean ll, boolean rl ) {
    100 		leftArm = la;
    101 		rightArm = ra;
    102 		leftLeg = ll;
    103 		rightLeg = rl;
    104 	}
    105 	void setName( String nome ) {
    106 		nomeRobot = nome;
    107 	}
    108 	boolean getLA() {
    109 		return leftArm;
    110 	}
    111 	boolean getRA() {
    112 		return rightArm;
    113 	}
    114 	boolean getLL() {
    115 		return leftLeg;
    116 	}
    117 	boolean getRL() {
    118 		return rightLeg;
    119 	}
    120 	String getName() {
    121 		return nomeRobot;
    122 	}
    123 	Robot getNext() {
    124 		return prossimoRobot;
    125 	}
    126 }