DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2005
    Posts
    18

    converting to an applet

    --------------------------------------------------------------------------------

    How would i convert the following program into an applet?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class minesweepGrid extends JFrame
    {
    	private Bomb[] bmbArry = new Bomb[5];
    	private JButton[][] BombGrid = new JButton[5][5];
    	private static final int WIDTH = 200;
    	private static final int HEIGHT = 200;
    	private BombHandler00 bbHandler00;
    	private BombHandler01 bbHandler01;
    	private BombHandler02 bbHandler02;
    	private BombHandler03 bbHandler03;
    	private BombHandler04 bbHandler04;
    	
    	public minesweepGrid()
    	{
    		for(int x=0; x<5; x++)
    			for(int y=0; y<5; y++)
    				BombGrid[x][y] = new JButton("");
    			bmbArry[0]=new Bomb(0,0);
    		
    		bbHandler00 = new BombHandler00();
    		BombGrid[0][0].addActionListener(bbHandler00);
    
    		bbHandler01 = new BombHandler01();
    		BombGrid[0][1].addActionListener(bbHandler01);
    
    		bbHandler02 = new BombHandler02();
    		BombGrid[0][2].addActionListener(bbHandler02);
    
    		bbHandler03 = new BombHandler03();
    		BombGrid[0][3].addActionListener(bbHandler03);
    
    		bbHandler04 = new BombHandler04();
    		BombGrid[0][4].addActionListener(bbHandler04);
    
    				setTitle("Minesweep");
    
    		Container pane = getContentPane();
    
    		pane.setLayout(new GridLayout(1,5));
    		for(int x=0; x<5; x++)
    			for(int y=0; y<5; y++)
    				pane.add(BombGrid[x][y]);
    
    		setSize(WIDTH,HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    
    	private class BombHandler00 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			char x,y;
    			boolean explode;
    			explode = false;
    			IsItABomb(0,0);
    
    		}
    		public boolean IsItABomb(int X, int Y)
    		{
    			for(int x=0;x<bmbArry.length;x++)
    			{
    			if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    				JOptionPane.showMessageDialog(null,"Sorry, you have hit a bomb...Game Over!");
    				return true;
    			}
    		return false;
    		}
    	}
    	private class BombHandler01 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			char x,y;
    			char[][] matrix = new char[5][5];
    
    			boolean explode;
    			explode = false;
    			IsItABomb(0,1);
    
    		}
    		public boolean IsItABomb(int X, int Y)
    		{
    			for(int x=0;x<bmbArry.length;x++)
    			{
    			if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    				JOptionPane.showMessageDialog(null,"Sorry, you have hit a bomb...Game Over!");
    				return true;
    			}
    		return false;
    		}
    	}
    	private class BombHandler02 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			char x,y;
    
    			boolean explode;
    			explode = false;
    			IsItABomb(0,2);
    		}
    		public boolean IsItABomb(int X, int Y)
    		{
    
    			for(int x=0;x<bmbArry.length;x++)
    			{
    			if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    				JOptionPane.showMessageDialog(null,"Sorry, you have hit a bomb...Game Over!");
    				return true;
    			}
    		return false;
    		}
    	}
    
    	private class BombHandler03 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			char x,y;
    
    			boolean explode;
    			explode = false;
    			IsItABomb(0,3);
    
    		}
    		public boolean IsItABomb(int X, int Y)
    		{
    
    			for(int x=0;x<bmbArry.length;x++)
    			{
    			if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    				JOptionPane.showMessageDialog(null,"Sorry, you have hit a bomb...Game Over!");
    				return true;
    			}
    		return false;
    		}
    	}
    
    	private class BombHandler04 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			char x,y;
    
    			boolean explode;
    			explode = false;
    			IsItABomb(0,4);
    		}
    		public boolean IsItABomb(int X, int Y)
    		{
    
    			for(int x=0;x<bmbArry.length;x++)
    			{
    			if((bmbArry[x].GetXpos() == X) && (bmbArry[x].GetYpos() == Y))
    				JOptionPane.showMessageDialog(null,"Sorry, you have hit a bomb...Game Over!");
    				return true;
    			}
    		return false;
    		}
    	}
    	public static void main(String[] args)
    	{
    		minesweepGrid mineSweep = new minesweepGrid();
    	}
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The big difference is that you do not have a "method main" - in its place, you have an init() method in which you build the elements of the applet ... this is started and stopped by the java-enabled html page with an <applet> tag in place.

    Here is a link to the java site's tutorial on applets:

    http://java.sun.com/docs/books/tutor...let/index.html

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links