Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

Archive for February 11th, 2006

Shape Changer and shifter

Posted by Circuit Negma on February 11, 2006

Exam.java
===========

import java.util.*;
import java.awt.*;

public class Exam {

private Point upperCorner;

public Exam (Point aPoint){

 this.upperCorner = new Point(aPoint);
  }
 
 public void circle (Graphics g) {
   
   g.drawOval(this.upperCorner.x,this.upperCorner.y,10,10);
  }
 public void square(Graphics g) {
 
   g.drawRect(this.upperCorner.x,this.upperCorner.y,10,10);
 }
 
 }

________________________________________________

Shapes.java
==============
/*
 Trivial applet that displays a string – 4/96 PNL
 Updated stationery – 8/2000 C.Phan
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Shaps extends Applet implements ActionListener
{

 Exam aExam;
 private int a,b;
 private Button Circle,Square,Reset;
 private static final String CIRCLE = “Circle”;
 private static final String SQUARE = “Square”;
 private static final String RESET = “Reset”;
 private boolean cir,squa;
 
 public void init() {
  
  this.cir = true;
  this.a = 0;
  this.b = 50;
  this.aExam = new Exam(new Point(a,b));
  
  this.Circle = new Button(CIRCLE);
  this.Circle.addActionListener(this);
  this.Square = new Button(SQUARE);
  this.Square.addActionListener(this);
  this.Reset = new Button(RESET);
  this.Reset.addActionListener(this);
  
  Panel main = new Panel(new GridLayout(1,3));
  main.add(Circle);
  main.add(Square);
  main.add(Reset);
  add(main);
  
  repaint();
 }
 
 public void paint( Graphics g ) {
  if (cir){
  aExam.circle(g);}
  else
  aExam.square(g);
 }
 
 public void actionPerformed(ActionEvent event){
  String s = event.getActionCommand();
  
  if (s.equals(CIRCLE)){
   this.a = a+10;
   this.b = 50;
   this.aExam = new Exam(new Point(a,b));
   this.cir = true;
   this.squa = false;
   }
  else if (s.equals(SQUARE)){
   this.a = a+10;
   this.b = 50;
   this.aExam = new Exam(new Point(a,b));
   this.cir = false;
   this.squa = true;}
  else if (s.equals(RESET)){
   this.a = 0;
   this.b = 50;
   this.aExam = new Exam(new Point(a,b));
   this.cir = true;
   this.squa = false;}
  
   repaint();

}

}

_______________________________________________

 

Posted in Java | Leave a Comment »

Moving Door

Posted by Circuit Negma on February 11, 2006

Door.java
============

import java.awt.*;
import java.applet.*;
import java.util.*;

public class Door {

/**
 *
 * @param aPoint
 */
public Door (Point aPoint) {
 this.cordinate = new Point(aPoint);

public void draw(Graphics graphics){

 graphics.fillRect(cordinate.x, cordinate.y, 30, 50);
 
 }

//instance variable
private Point cordinate;
}

____________________________________________________

MovingDoor.java
===============

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class MovingDoor extends Applet implements ActionListener {
        
     private static final String RED = “Red”;
     private static final String BLUE = “Blue”;
     private Button red, blue;  
     private static final Color color1 = Color.red;
     private static final Color color2 = Color.blue;
     private Color color3;
     Door aDoor;
     private int a,b;
       
         public void init() {
        
         this.a = 0;
         this.b = 60;
         this. aDoor = new Door(new Point(a,b));
        
         this.red = new Button(RED);
         this.red.setBackground(color1);
         this.red.addActionListener(this);
        
         this.blue = new Button(BLUE);
         this.blue.setBackground(color2);
         this.blue.addActionListener(this);
        
         Panel row = new Panel(new FlowLayout(FlowLayout.CENTER,4,4));
         row.add(red);
         row.add(blue);
                 
         Panel main = new Panel(new GridLayout(1,2));
         main.add(row);
         add(main);
        
        
       }
      
 public void paint(Graphics graphics) {
 /* applet display statements go here. */
  
  
  graphics.setColor(color3);
  aDoor.draw(graphics);
  graphics.setColor(Color.black);
  graphics.drawString(“Position of Xd:> ” + a, 140, 50);
  graphics.drawString(“Position of Yd:> ” + b, 260, 50);
  setBackground(Color.white);
 }

public void actionPerformed(ActionEvent event){
  String s = event.getActionCommand();
  
  if (s.equals(BLUE) && a<440){
   this.a = this.a+20;
   this.b = 60;
   color3 = color2;
   this.aDoor  = new Door(new Point(a,b));
   System.out.println(“Position of blue button :>” +a);
   }
  else if (s.equals(RED) && a>0){
   this.a = this.a-20;
   this.b = 60;
   color3 = color1;
   this.aDoor = new Door(new Point(a,b));
   System.out.println(“Position of red button:> ” + a);
   }
   
   repaint();
   
}
}

Posted in Java | 1 Comment »

Moving Square Game

Posted by Circuit Negma on February 11, 2006

 

Asn1.java
==========

/*
 Trivial applet that displays a string
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Asn1 extends Applet implements ActionListener
{
 
// Constants
private  static  final  Color  defaultTopColor  =  Color.red;
private  static  final  Color  defaultMiddleColor  =  Color.yellow;
private  static  final  Color  defaultBottomColor  =  Color.blue;
private  static  final  String  RED  =  “Red”;
private  static  final  String  YELLOW  =  “Yellow”;
private  static  final  String  BLUE  =  “Blue”;
private  static  final  String  RESET  =  “Reset”;
private  static  final  String  TOP_LIST_LABEL  =  “Top Row Colors”;
private  static  final  String  MIDDLE_LIST_LABEL  =  “Middle Row Colors”;
private  static  final  String  BOTTOM_LIST_LABEL  =  “Bottom Row Colors”;

// Variables
private Panel mainPanel, boardPanel, listsPanel;
private Board board;
private Button resetButton;
private List topRowColorList, middleRowColorList, bottomRowColorList;

public void init() {
// initializes applet
 makeListsPanel();
 makeBoardPanel();
 makeMainPanel();
 addListeners();
}  

private List makeColorList(int itemNumber){
// Create a List with the 3 colour options; preselect the item with the given itemNumber
 List aList = new List(3, false);
 aList.add(RED);
 aList.add(YELLOW);
 aList.add(BLUE);
 aList.select(itemNumber);
 return aList;

}

private Panel makeListsPanel() {
// Make Labels and Lists for row colour choices, add them to a Panel
// The Panel uses GridLayout, 2 rows and 3 columns
 Label label1 = new Label(TOP_LIST_LABEL);
 Label label2 = new Label(MIDDLE_LIST_LABEL);
 Label label3 = new Label(BOTTOM_LIST_LABEL);
 this.topRowColorList = makeColorList(0);
 this.middleRowColorList = makeColorList(1);
 this.bottomRowColorList = makeColorList(2);
 this.listsPanel = new Panel(new GridLayout(2,3));
 this.listsPanel.add(label1);
 this.listsPanel.add(label2);
 this.listsPanel.add(label3);
 this.listsPanel.add(this.topRowColorList);
 this.listsPanel.add(this.middleRowColorList);
 this.listsPanel.add(this.bottomRowColorList);
 add(listsPanel);
 return listsPanel;
 
}

private Panel makeBoardPanel() {
// Create a Board using the default row colours, create a reset Button;
// add this Board and Button to a Panel (which uses FlowLayout)
  this.board = new Board(defaultTopColor, defaultMiddleColor, defaultBottomColor);
  this.resetButton = new Button(RESET);
  this.boardPanel = new Panel(new FlowLayout());
  this.boardPanel.add(this.board.getBoard());
  this.boardPanel.add(this.resetButton);
  add(boardPanel);
  return boardPanel;
}

private Panel makeMainPanel() {
// create and return the Panel which contains the boardPanel and the ListsPanel
// The Panel uses GridLayout, 2 rows and 1 column
 this.mainPanel = new Panel(new GridLayout(2,1));
 this.mainPanel.add(this.boardPanel);
 this.mainPanel.add(this.listsPanel);
 add(mainPanel);
 return mainPanel;
}
private  void  addListeners() {
// adds the Applet as the ActionListener for the various components
  this.board.getSquare(“1″).addActionListener(this);
        this.board.getSquare(“2″).addActionListener(this);
        this.board.getSquare(“3″).addActionListener(this);
        this.board.getSquare(“4″).addActionListener(this);
        this.board.getSquare(“5″).addActionListener(this);
        this.board.getSquare(“6″).addActionListener(this);
        this.board.getSquare(“7″).addActionListener(this);
        this.board.getSquare(“8″).addActionListener(this);
        this.board.getSquare(“9″).addActionListener(this);
        this.resetButton.addActionListener(this);
        this.topRowColorList.addActionListener(this);
        this.middleRowColorList.addActionListener(this);
        this.bottomRowColorList.addActionListener(this);
}

private  Color  makeColor(String  colString) {
// Return a Color corresponding to the given String
 if(colString.equals(RED))
            return defaultTopColor;
        if(colString.equals(YELLOW))
            return defaultMiddleColor;
        if(colString.equals(BLUE))
            return defaultBottomColor;
        else
            return null;
}

public  void  actionPerformed(ActionEvent  event) {
// Responds appropriately to whichever button has been clicked
// If Reset has been clicked, change row colours to the selected colours, else move the square that has been clicked
       
        String result = event.getActionCommand();
        if(result.equals(RESET)) {
            this.board.changeRowColor(1, makeColor(topRowColorList.getSelectedItem()));
            this.board.changeRowColor(2, makeColor(middleRowColorList.getSelectedItem()));
            this.board.changeRowColor(3, makeColor(bottomRowColorList.getSelectedItem()));
            this.board.paintSquares();
        } else {
            board.move(result);
        }
 }
}// end of Asn1 class

____________________________________________________________________________________________

Board.java
===========

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Board {

// instance variables
private Panel row1, row2, row3, boardPanel;
private Button square1, square2, square3, square4, square5;
private Button square6, square7, square8, square9;
private Color topRowColor, middleRowColor, bottomRowColor;
private int emptySquare;

// static variables
private static Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;

// define the class Board.
public Board(Color top, Color middle, Color bottom) {
  this.topRowColor = top;
  this.middleRowColor = middle;
  this.bottomRowColor = bottom;
  this.fillRows();
  this.assembleRows();
  this.paintSquares();
}

// Instance methods
private Panel makeRowPanel( ) {
// Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.
  Panel aPanel;
  aPanel = new Panel(new FlowLayout(FlowLayout.CENTER,0,0));
  return aPanel;
}

private Button makeSquare(String squareNumber) {
// Create and return a button with the given String as its Label
 Button aButton;
 aButton = new Button(squareNumber);
 return aButton;
}

public void paintSquares( ) {
// set the colours of the squares according to their row colours and set variable emptySquare to default

 this.square1.setBackground(topRowColor);
 this.square2.setBackground(topRowColor);
 this.square3.setBackground(topRowColor);
 this.square4.setBackground(middleRowColor);
 this.square5.setBackground(emptySquareColor);
 this.square6.setBackground(middleRowColor);
 this.square7.setBackground(bottomRowColor);
 this.square8.setBackground(bottomRowColor);
 this.square9.setBackground(bottomRowColor);
 this.emptySquare = defaultEmpty; 
 System.out.println(“paintSquares”);
}

private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows
 this.square1 = makeSquare(“1″);
 this.square2 = makeSquare(“2″);
 this.square3 = makeSquare(“3″);
 this.square4 = makeSquare(“4″);
 this.square5 = makeSquare(String.valueOf(defaultEmpty));
 this.square6 = makeSquare(“6″);
 this.square7 = makeSquare(“7″);
 this.square8 = makeSquare(“8″);
 this.square9 = makeSquare(“9″);
 
 this.row1 = makeRowPanel();
 this.row1.add(this.square1);
 this.row1.add(this.square2);
 this.row1.add(this.square3);
 
 this.row2 = makeRowPanel();
 this.row2.add(this.square4);
 this.row2.add(this.square5);
 this.row2.add(this.square6);
 
 this.row3 = makeRowPanel();
 this.row3.add(this.square7);
 this.row3.add(this.square8);
 this.row3.add(this.square9);
 
}

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)
 this.boardPanel = new Panel(new GridLayout(3,1));
 this.boardPanel.add(this.row1);
 this.boardPanel.add(this.row2);
 this.boardPanel.add(this.row3);
 
}

public Panel getBoard( ){
// Return a reference to the Panel into which all 3 rows have been assembled.
 return this.boardPanel;

}
public Button getSquare(String number) {
// Returns the square (i.e. Button) with the given label
  if(number.equals(“1″))
            return this.square1;
        if(number.equals(“2″))
            return this.square2;
        if(number.equals(“3″))
            return this.square3;
        if(number.equals(“4″))
            return this.square4;
        if(number.equals(“5″))
            return this.square5;
        if(number.equals(“6″))
            return this.square6;
        if(number.equals(“7″))
            return this.square7;
        if(number.equals(“8″))
            return this.square8;
        if(number.equals(“9″))
         return this.square9;
        else
            return null;
}

public void changeRowColor(int rowNumber, Color newColor) {
// Change the current colour of the row with the given number
 
 
 if(rowNumber == 1)
            this.topRowColor = newColor;
        else
        if(rowNumber == 2)
            this.middleRowColor = newColor;
        else
            this.bottomRowColor = newColor;
}

private void changeSquares(Button coloredSquare, Button emptySquare) {
// Interchanges the colours of the given squares
 emptySquare.setBackground(coloredSquare.getBackground());
    coloredSquare.setBackground(emptySquareColor);
}

public void move(String squareLabel) {
// If the move is valid, move the square with the given label into the empty space
 int number1 = Character.getNumericValue(squareLabel.charAt(0));
 int number2 = Math.abs(number1 – this.emptySquare);
 
 if (number2 == 1 || number2 == 3 ){
   this.changeSquares(getSquare(squareLabel), getSquare(String.valueOf(this.emptySquare)));
   this.emptySquare = number1;
   System.out.println(“the emptySquare is (” + this.emptySquare + “) so (” + this.emptySquare + “) is a valid move.”);}
  else
   System.out.println(“this is invalid move (” + squareLabel + “)”);
  
 } 
} // end of Board class,

Posted in Java | 1 Comment »