/** * A Sample Animation with a Restart button * * @author Fr Chris Thiel, OFMCap * @version 9 May 2007 */ import java.awt.event.*; import java.applet.Applet; import java.awt.*; import javax.swing.*; public class SampleAnimation extends Applet implements ActionListener { Timer timer; Image virtualMem; Graphics gBuffer; int appletWidth; int appletHeight; int frame; int lastFrame; Button myButton; /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) { if (frame < lastFrame) { frame++; } else { frame=0; } } else { //restart button pressed frame=0; if (timer.isRunning()) { timer.stop(); myButton.setLabel("Start"); } else { timer.start(); myButton.setLabel("Stop"); } } repaint(); } // OUR CLASS IS A SUBCLASS OF APPLET /** * this init() method will be run at the beginning * and where we create the Listeners */ public void init() { appletWidth = getWidth(); appletHeight = getHeight(); frame=0; timer=new Timer(10, this); lastFrame=appletWidth; virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); myButton= new Button("Do It Again"); //the class is its own button listener myButton.addActionListener (this); add(myButton); } public void paint(Graphics g) { //We draw using the object methods of // the graphics buffer, not what is // currently on the screen gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight);gBuffer.setColor(Color.black); gBuffer.setColor(Color.black); gBuffer.drawString("Frame "+frame, 20,20); gBuffer.setColor(Color.blue); gBuffer.fillOval(frame, 200, 50,50); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method } }
/** * A Simple Draw with a clear button * * @author Fr Chris Thiel, OFMCap * @version 9 May 2007 */ import java.awt.event.*; import java.applet.Applet; import java.awt.*; public class Draw_with_Button_Sample extends Applet implements MouseMotionListener, MouseListener { // instance variables - replace the example below with your own Image virtualMem; Graphics gBuffer; int oldX, oldY, newX, newY; int appletWidth; int appletHeight; // MOUSE LISTENER IS AN INTERFACE so it requires // all these methods, even if you do nothing: public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) { newX = e.getX();//new newY = e.getY();//new oldX = newX; oldY = newY; repaint(); } // MOUSE MOTION LISTENER IS AN INTERFACE: // these methods are needed of all MouseMotionListeners, // even if we don't do anything: public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { newX = e.getX();//new newY = e.getY();//new oldX = newX; oldY = newY; repaint(); } // OUR CLASS IS A SUBCLASS OF APPLET /** * this init() method will be run at the beginning * and where we create the Listeners */ public void init() { appletWidth = getWidth(); appletHeight = getHeight(); virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); // Finally, we alter the init method to // add our MouseListener and // MouseMotionListener to *this* sub class of Applet: addMouseMotionListener(this); // new addMouseListener(this); //new Button myButton= new Button("Clear"); // Here I am using an anonymous class // for the action Listener myButton.addActionListener ( new ActionListener () { public void actionPerformed(ActionEvent e) { gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); oldX=-1; oldY=-1; repaint(); } } ); add(myButton); } public void paint(Graphics g) { //We draw using the object methods of // the graphics buffer, not what is // currently on the screen gBuffer.setColor(Color.black); gBuffer.drawString("Move inside the applet to draw", 20,20); gBuffer.setColor(Color.blue); gBuffer.fillRect(oldX,oldY,2,2); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method } }
/** * SimpleTicTackToe here. * * @author Fr Chris Thiel OFMCap * @version 9 May 2007 */ import java.awt.event.*; import java.applet.Applet; import java.awt.*; public class SimpleTicTackToe extends Applet implements MouseListener, ActionListener { /** * SimpleTicTackToe variables */ int board[]; int turn; int appletWidth; int appletHeight; int x1, x2, y1, y2, cell; boolean gameOver; Image virtualMem; Graphics gBuffer; /** * MOUSE LISTENER IS AN INTERFACE so it requires * all these methods, even if you do nothing: */ public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseClicked(MouseEvent e) { int x = e.getX();//new int y = e.getY();//new int location=9; if (x < x1 && yx1 && x x2 && y < y1 ) location=2; if (x < x1 && y > y1 && y < y2) location=3; if (x > x1 && x y1 && y < y2) location=4; if (x > x2 && y > y1 && y < y2) location=5; if (x < x1 && y >y2) location=6; if (x > x1 && x y2) location=7; if (x > x2 && y > y2 ) location=8; if (board[location]==0 && !gameOver) { board[location]=turn; turn=(-1)*turn; } repaint(); } /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed */ public void actionPerformed(ActionEvent e) { //restart button pressed for(int i=0; i<9; i++)board[i]=0; turn=-1; //x is first gameOver=false; repaint(); } /** * Init */ public void init() { board=new int[9]; for(int i=0; i<9; i++)board[i]=0; turn=-1; //x is first gameOver=false; appletWidth = getWidth(); appletHeight = getHeight(); x1=appletWidth/3; x2=x1+appletWidth/3; y1=appletHeight/3; y2=y1+appletHeight/3; cell=x1/2; virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); addMouseListener(this); Button restartButton= new Button("Start Over"); restartButton.addActionListener (this); add(restartButton); } private String gameStatus() { String m; if (turn<0) m="Blue's Turn"; else m="Red's Turn"; int w[]; w=new int[8]; w[0]=board[0]+board[1]+board[2]; w[1]=board[3]+board[4]+board[5]; w[2]=board[6]+board[7]+board[8]; w[3]=board[0]+board[3]+board[6]; w[4]=board[1]+board[4]+board[7]; w[5]=board[2]+board[5]+board[8]; w[6]=board[0]+board[4]+board[8]; w[7]=board[2]+board[4]+board[6]; for (int i=0; i<8; i++) { if (w[i]==3) { gameOver=true; m="Red Wins"; } if (w[i]==-3) { gameOver=true; m="Blue Wins"; } } return m; } public void paint(Graphics g) { gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); gBuffer.setColor(Color.black); gBuffer.drawString(gameStatus(), 20,appletHeight-20); gBuffer.drawLine(x1,20,x1, appletHeight-20); gBuffer.drawLine(x2,20,x2, appletHeight-20); gBuffer.drawLine(20,y1, appletWidth-20,y1); gBuffer.drawLine(20,y2, appletWidth-20,y2); for (int i=0; i<9; i++) { if (board[i]<0) // x { gBuffer.setColor(Color.blue); gBuffer.fillRect( cell/2+(i%3)*x1, cell/2+y1*(i/3), cell,cell); } if (board[i]>0) // o { gBuffer.setColor(Color.red); gBuffer.fillOval( cell/2+(i%3)*x1, cell/2+y1*(i/3),cell,cell); } } g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method } }
Usually the sound file format for java is uLaw or ".au" files. If you have trouble, convert your sound files to this format using a sound editor application
/** * A Sample Animation with a Restart button * * @author Fr Chris Thiel, OFMCap * @version 9 May 2007 */ import java.awt.event.*; import java.applet.*; import java.awt.*; import javax.swing.*; public class SampleSoundAnimation extends Applet implements ActionListener { Timer timer; Image virtualMem; Graphics gBuffer; int appletWidth; int appletHeight; int frame; int lastFrame; Button myButton; AudioClip bubbleSound; /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) { if (frame < lastFrame) { frame++; } else { frame=0; } } else { //restart button pressed frame=0; if (timer.isRunning()) { timer.stop(); myButton.setLabel("Start"); if(bubbleSound!=null)bubbleSound.stop(); } else { timer.start(); myButton.setLabel("Stop"); if(bubbleSound!=null) bubbleSound.loop(); } } repaint(); } // OUR CLASS IS A SUBCLASS OF APPLET /** * this init() method will be run at the beginning * and where we create the Listeners */ public void init() { appletWidth = getWidth(); appletHeight = getHeight(); frame=0; timer=new Timer(10, this); lastFrame=appletHeight; virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); myButton= new Button("Do It Again"); //the class is its own button listener myButton.addActionListener (this); add(myButton); //Audio bubbleSound = getAudioClip(getDocumentBase(), "bubbles.wav"); } public void paint(Graphics g) { //We draw using the object methods of // the graphics buffer, not what is // currently on the screen gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight);gBuffer.setColor(Color.black); gBuffer.setColor(Color.black); gBuffer.drawString("Frame "+frame, 20,20); gBuffer.setColor(Color.blue); gBuffer.fillOval(20,appletHeight-frame, 80,80); gBuffer.fillOval(120,appletHeight- 3*frame-80, 30,30); gBuffer.fillOval(240, appletHeight-2*frame+40, 50,50); gBuffer.fillOval(360, 2*appletHeight-3*frame+40, 20,20); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method } }