Monday, June 29, 2015

Java again source and ttut WITH JAVA MIDLET

http://www.cs.armstrong.edu/liang/intro9e/test.html


JAVA MIDLET


https://webfiles.uci.edu/kamdark/www/j2me.html


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask; 

public class BouncingBall extends MIDlet
{
 Ball b1,b2;
 BallCanvas bc;
 Display display;
 
 public BouncingBall()
 {
  System.out.println("Boucing ball");
 }
 public void destroyApp(boolean a)
 {
 }
 public void startApp()
 {
  try{

  bc = new BallCanvas();
  display = Display.getDisplay(this);
  
  Ball b1 = new Ball(90,30,5,5,1,10,250,255,255,bc.getGraphics(),bc.getWidth(),bc.getHeight(),bc);  
  Ball b2 = new Ball(100,20,15,1,3,20,150,150,250,bc.getGraphics(),bc.getWidth(),bc.getHeight(),bc);
  Ball b3 = new Ball(10,10,30,2,1,25,250,100,250,bc.getGraphics(),bc.getWidth(),bc.getHeight(),bc);
  //Ball b4 = new Ball(10,90,10,1,4,15,50,250,50,bc.getGraphics(),bc.getWidth(),bc.getHeight(),bc);  
  display.setCurrent(bc);
  }
  catch(Exception e){System.out.println(e);}
 }
 public void pauseApp()
 {
  
 }

 public class BallCanvas extends Canvas
 {
  Graphics g1;
  public void paint(Graphics g)
  {
   g1=g;
   //System.out.println("Inside");
 ///  g1.setColor(0,0,0);
  // g1.fillRect(0,0,getWidth(),getHeight());  
  }
  Graphics getGraphics()
  {
   return(g1);
  }
 }
 
public class Ball
{
 private int ox,oy,radius,speed;
 private int incx,incy;
 private int red,green,blue;
 private int maxx,maxy;
 private boolean xb,yb,flag;
 TimerTask t;
 Timer timer;
 Graphics g;
 BallCanvas bc;
 private int prev1,prev2;
 
 public Ball(int x,int y,int r,int ix,int iy,int s,int rd,int gr,int b,Graphics g1,int mx,int my,BallCanvas bc1)
 {
  System.out.println("Ball");
  maxx=mx;
  maxy=my;
  incx=ix;
  incy=iy;
  red=rd;
  green = gr;
  blue = b;
  xb=false;
  yb=false;
  radius = r;
  ox=x;
  oy=y;
  flag = false;
  bc = bc1;
  g1 = g;
  timer = new Timer();
  timer.schedule(new Bounce(),(long)0,(long)s);
 }
 public void paint()
 {
  //System.out.println(xb + " " + yb);
  if((ox+radius)>=maxx)
   xb=true;
  if(ox<=0)
   xb=false;
  if((oy+radius)>=maxy)
   yb=true;
  if(oy<=0)
   yb=false;

  if(xb==false)
   {prev1=ox;ox+=incx;}
  else
   {prev1=ox;ox-=incx;}
  if(yb==false)
   {prev2=oy;oy+=incy;}
  else
   {prev2=oy;oy-=incy;}
  bc.repaint();
  if(flag==false)
  {
   bc.getGraphics().setColor(0,0,0);
   bc.getGraphics().fillRect(0,0,bc.getWidth(),bc.getHeight());
   flag = true;
  }
  bc.getGraphics().setColor(0,0,0);
  //bc.getGraphics().fillRect(prev1,prev2,radius,radius); 
  bc.getGraphics().fillArc(prev1,prev2,radius,radius,0,360);
  bc.getGraphics().setColor(red,green,blue);
  bc.getGraphics().fillArc(ox,oy,radius,radius,0,360);
  try{
   wait(20);
  }catch(Exception e){}
 }  
 public class Bounce extends TimerTask
 {
  public void run()
  {
   paint();
  }
 }

}
}



---

///next one 


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class BouncingBall extends MIDlet {
  public void startApp() {

    Displayable d = new BouncingBallCanvas ();

    d.addCommand(new Command("Exit", Command.EXIT, 0));
    d.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable s) {

        notifyDestroyed();
      }
    } );

    Display.getDisplay(this).setCurrent(d);
  }

  public void pauseApp() { }

  public void destroyApp(boolean unconditional) { }

} // end class BouncingBall



class BouncingBallCanvas extends Canvas {
     boolean isFirst = true;
     int width = getWidth();
     int height = getHeight();
     int x, y, prevX, prevY;
     Timer timer;
     Mover mover;

     int bgColor = 0xffffff; // Background color
     int bS = 20;  // Size of ball
     int dx = 5;  // Movement displacement for x
     int dy = 5;  // Movement displacement for y
     boolean vertical = true; // Flag determining whether the movement is vertical or horizontal

     boolean prevVertical = true; // Stores the previous value of vertical
     int rbColor[] = { 0xff0000, 0xffa500, 0xffff00, 0xadff2f, 0x32cd32, 0x0000ff, 0x9400d3 };
     int ri = 0; // Index of rbColor
     int ballColor = rbColor[ri]; // Color of ball


     public BouncingBallCanvas () {
        timer = new Timer();
        mover = new Mover(this);
        timer.schedule( mover, 100, 100 );

        // Calculate the starting position (x,y) of the ball

            ...

        // Stores the position of the ball at (prevX, prevY)
            ...
     }

     protected void paint(Graphics g) {
        g.setColor(bgColor);

        if (isFirst) {

            // Clear screen
            ...
            isFirst=false;
        }
        else {
            // Clear previous drawn ball with background color
            ...
        }

        // Draw the ball with the specified color at (x, y)

            ...
     }

     public void move() {
        // Stores the position of the ball at (prevX, prevY)
            ...

        if (vertical) {
           if (y < 0 || y > height-bS) dy = -dy;

           y += dy;
        }
        else {
           // Update the value of x when the movement is horizontal  
               ...
        }

        repaint();
     }

     protected void keyPressed(int keyCode) {

        prevVertical = vertical;
        prevX = x;
        prevY = y;

        switch (getGameAction(keyCode)) {
           case UP:
               // Toggle the flag "vertical"
               ...

               break;
           case DOWN:
               // Toggle the flag "vertical"
               ...
               break;
           case LEFT:
               // Decrease the index (ri) in the array rbColor and update the color of the ball

               ...
               break;
           case RIGHT:
               // Increase the index (ri) in the array rbColor and update the color of the ball
               ...
               break;
       }


       if (vertical != prevVertical) {
          if (vertical) {
             x = width / 2 - bS / 2;
             y = 0;
          }
          else {
             // Set the initial value of (x, y) when the movement is horizontal

               ...
          }
       }

       repaint();
    }

} // end class BouncingBallCanvas


class Mover extends TimerTask {
    BouncingBallCanvas c;

    public Mover(BouncingBallCanvas c) { this.c = c; }


    public void run(){
       c.move();
    }

} // end class Mover

No comments:

Post a Comment