Circuit Negma

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

Moving Square Example

Posted by Circuit Negma on February 6, 2006


/*

MovingSquare.java

*/

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

public class MovingSquare extends Applet implements ActionListener
{
 
 private static final String INCRE = “Increase”;
 private static final String DECRE = “Decrease”;
 private Button increase,decrease;
    Square aSquare;
    private int a,b,c,d;
   
 
 public void init() {
  
  this.a = 60;
  this.b = 40;
  this.aSquare =  new Square(new Point(a,b));
  
  this.increase = new Button(INCRE);
  this.increase.addActionListener(this);
  
  this.decrease = new Button(DECRE);
  this.decrease.addActionListener(this);
  
  Panel row = new Panel (new FlowLayout(FlowLayout.CENTER,4,4));
  row.add(increase);
  row.add(decrease);
  
  Panel main = new Panel (new GridLayout(1,2));
  main.add(row);
  add(main);
  
  repaint();
  }
 
 public void paint( Graphics g ) {
  
  g.drawString(“Width: “+a,30,50);
  g.drawString(“Hight: “+b,100,50);
  g.setColor(Color.blue);
  aSquare.draw(g);
 }
 
 public void actionPerformed(ActionEvent event){
  String s = event.getActionCommand();
  
  if (s.equals(INCRE) && a<160){
   this.a = a+20;
   this.b = b+20;
   this.aSquare = new Square(new Point(a,b));}
  else if (s.equals(DECRE) && a>60){
    this.a = a-20;
    this.b = b-20;
    this.aSquare = new Square(new Point(a,b));}
   
   repaint();
 }

}

_____________________________________________________________

/*

Square.java

*/

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

public class Square {

private Point upperCorner,aArea;

public Square (Point aPoint) {

 this.upperCorner = new Point(aPoint);
 
 }
 
 public void draw(Graphics g) {
  g.fillRect(20,60,this.upperCorner.x,this.upperCorner.y);
  
 }
 
 } 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: