Results 1 to 6 of 6
  1. #1
    j0sh77 is offline Member
    Join Date
    May 2013
    Location
    Michigan, U.S.
    Posts
    4
    Rep Power
    0

    Default Converting Connect Four to Applet

    I've created a simple, working Connect Four program that'd I'd like to convert to an applet. In the Application, I have two class which you can see below.

    I'm having trouble figuring out what to change in order to get it working correctly on a web page. I have little experience in converting programs, mainly single JPanel programs and I'd like to see how one might go about this program, hopefully being able to apply this in the future.



    ConnectFour.java

    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    
    public class ConnectFour {
    	public static boolean user;
    	public static Color[][] checker = new Color[8][8];
    	public static boolean[][] disabled = new boolean[8][8];
    
    
    	public static void main(String[] args) {
    		JFrame theGUI = new JFrame();
    		theGUI.setTitle("Connect Four");
    		theGUI.setSize(500, 500);
    		theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		Container pane = theGUI.getContentPane();
    		pane.setLayout(new GridLayout(7, 7));
    
    		for(int y = 7; y >= 1; y--){
    			for(int x = 1; x <= 7; x++){
    				checker[x][y] = Color.white;
    				if(y != 1){
    					disabled[x][y] = true;
    				}
    
    				ConnectFourCP panel = new ConnectFourCP(x, y);
    				pane.add(panel);
    				ConnectFourCP.addPanel(x, y, panel);
    			}
    		}
    		theGUI.setVisible(true);
    	}
    
    }

    ConnectFourCP.java

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    public class ConnectFourCP extends JPanel{
    
    	private Color turn = Color.white; // color of the checker
    	private int boardX;
    	private int boardY;
    	static private String typeOfWin = "";
    	private int countAdjacent = 1;
    	static private int winningCoordinates[][] = new int[2][5]; //0 for x, 1 for y
    	
    	public ConnectFourCP(int x, int y){
    		setBackground(new Color(255, 215, 0));
    		addMouseListener(new PanelListener());
    		boardX = x;
    		boardY = y;
    	}
    	
    	//paint the board
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(Color.black);
    		g.drawRect(-1, -1, getWidth(), getHeight());
    		g.setColor(turn);
    		g.fillOval(getWidth() / 25, getHeight() / 25, getWidth() - getWidth() / 10, getHeight() - getHeight() / 10);
    		
    		g.setColor(Color.cyan);
    		
    		
    		if(typeOfWin == "horz"){
    			g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
    			g.drawLine(0, getHeight() / 2 + 1, getWidth(), getHeight() / 2 + 1);
    			g.drawLine(0, getHeight() / 2 - 1, getWidth(), getHeight() / 2 - 1);
    		}else if(typeOfWin == "vert"){
    			g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
    			g.drawLine(getWidth() / 2 + 1, 0, getWidth() / 2 + 1, getHeight());
    			g.drawLine(getWidth() / 2 - 1, 0, getWidth() / 2 - 1, getHeight());
    		}else if(typeOfWin == "diagUp"){
    			g.drawLine(0, getHeight(), getWidth(), 0);
    			g.drawLine(0, getHeight() + 1, getWidth(), 1);
    			g.drawLine(0, getHeight() - 1, getWidth(), -1);
    		}else if(typeOfWin == "diagDown"){
    			g.drawLine(0, 0, getWidth(), getHeight());
    			g.drawLine(0, 1, getWidth(), getHeight() + 1);
    			g.drawLine(0, -1, getWidth(), getHeight() - 1);
    		}
    	}
    	
    
    	private class PanelListener extends MouseAdapter{
    
    		public void mouseClicked(MouseEvent e){
    
    			//if the x,y is not disabled, change its color and change the color in the array
    			if(!ConnectFour.disabled[boardX][boardY]){
    				if(ConnectFour.user){
    					turn = Color.red;
    					ConnectFour.checker[boardX][boardY] = Color.red;
    					ConnectFour.user = false; //switch user
    				}else{
    					turn = Color.black;
    					ConnectFour.checker[boardX][boardY] = Color.black;
    					ConnectFour.user = true; //switch user
    				}
    				repaint();
    
    				//disable the clicked checker, if it is on the top, don't
    				ConnectFour.disabled[boardX][boardY] = true;
    				if(boardY != 7)
    					ConnectFour.disabled[boardX][boardY + 1] = false;
    			}
    
    			//check all possible connections
    			checkHorizontal(); //working!
    			checkVertical();   //working!
    			checkDiagonalUp();  //working!
    			checkDiagonalDown(); //working!
    		}
    	}
    
    	public void checkHorizontal(){
    		Color checkColor; //variable to determine the first color to check
    		for(int y = 1; y <= 7; y ++){
    			for(int x = 1; x <= 4; x ++){
    				winningCoordinates[0][1] = x;
    				winningCoordinates[1][1] = y;
    				
    				countAdjacent = 1; //variable to count how many of the same color are adjacent
    				checkColor = ConnectFour.checker[x][y]; //set checkcolor to the tested checker
    				if(checkColor != Color.white){
    					for(int add = 1; add <= 3; add ++){
    						if(checkColor == ConnectFour.checker[x + add][y]){ //check if the same color
    							winningCoordinates[0][1 + add] = x + add;
    							winningCoordinates[1][1 + add] = y;
    							countAdjacent++;
    							//System.out.println(x + ", " + y + "\tColor: " + checkColor); //only for debugging
    						}
    						else break;
    					}
    				}
    				if(countAdjacent == 4){
    					typeOfWin = "horz";
    					declareWinner(checkColor);
    				}
    			}
    		}
    	}
    
    	public void checkVertical(){
    		Color checkColor; //variable to determine the first color to check
    		for(int x = 1; x <= 7; x ++){
    			for(int y = 1; y <= 4; y ++){
    				winningCoordinates[0][1] = x;
    				winningCoordinates[1][1] = y;
    				int countAdjacent = 1; //variable to count how many of the same color are adjacent
    				checkColor = ConnectFour.checker[x][y]; //set checkcolor to the tested checker
    				if(checkColor != Color.white){
    					for(int add = 1; add <= 3; add ++){
    						if(checkColor == ConnectFour.checker[x][y + add]){ //check if the same color
    							winningCoordinates[0][1 + add] = x;
    							winningCoordinates[1][1 + add] = y + add;
    							countAdjacent++;
    							//System.out.println(x + ", " + y + "\tColor: " + checkColor); //only for debugging
    						}
    						else break;
    					}
    				}
    				if(countAdjacent == 4){
    					typeOfWin = "vert";
    					declareWinner(checkColor);
    				}
    			}
    		}
    	}
    
    	public void checkDiagonalUp(){
    		Color checkColor; //variable to determine the first color to check
    		for(int x = 1; x <= 4; x ++){
    			for(int y = 1; y <= 4; y ++){
    				winningCoordinates[0][1] = x;
    				winningCoordinates[1][1] = y;
    				
    				int countAdjacent = 1; //variable to count how many of the same color are adjacent
    				checkColor = ConnectFour.checker[x][y]; //set checkcolor to the tested checker
    				if(checkColor != Color.white){
    					for(int add = 1; add <= 3; add ++){
    						if(checkColor == ConnectFour.checker[x + add][y + add]){ //check if the same color
    							winningCoordinates[0][1 + add] = x + add;
    							winningCoordinates[1][1 + add] = y + add;
    							countAdjacent++;
    							//System.out.println(x + ", " + y + "\tColor: " + checkColor); //only for debugging
    						}
    						else break;
    					}
    				}
    				if(countAdjacent == 4){
    					typeOfWin = "diagUp";
    					declareWinner(checkColor);
    				}
    			}
    		}
    	}
    	
    	public void checkDiagonalDown(){
    		Color checkColor; //variable to determine the first color to check
    		for(int x = 1; x <= 4; x ++){
    			for(int y = 4; y <= 7; y ++){
    				winningCoordinates[0][1] = x;
    				winningCoordinates[1][1] = y;
    				
    				int countAdjacent = 1; //variable to count how many of the same color are adjacent
    				checkColor = ConnectFour.checker[x][y]; //set checkcolor to the tested checker
    				if(checkColor != Color.white){
    					for(int add = 1; add <= 3; add++){
    						if(checkColor == ConnectFour.checker[x + add][y - add]){ //check if the same color
    							winningCoordinates[0][1 + add] = x + add;
    							winningCoordinates[1][1 + add] = y - add;
    							countAdjacent++;
    							//System.out.println(x + ", " + y + "\tColor: " + checkColor); //only for debugging
    						}
    						else break;
    					}
    				}
    				if(countAdjacent == 4){
    					typeOfWin = "diagDown";
    					declareWinner(checkColor);
    				}
    			}
    		}
    	}
    
    	public void declareWinner(Color c){ //method to execute when a winner is found
    		String color = new String();
    		
    		for(int i = 1; i <= 4; i ++){
    			int x = winningCoordinates[0][i];
    			int y = winningCoordinates[1][i];
    			panel[x][y].repaint();
    		}
    		
    		if(c == Color.black) color = "black"; //Determine winner
    		else if(c == Color.red) color = "red";
    		JOptionPane.showMessageDialog(null, "The player with the " + color + " checkers wins!");
    		System.exit(0);
    	}
    	
    	private static ConnectFourCP panel[][] = new ConnectFourCP[8][8];
    	
    	static public void addPanel(int x, int y, ConnectFourCP p){
    		panel[x][y] = p;
    	}
    }

    Thanks in advance! :D

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,986
    Rep Power
    9

    Default Re: Converting Connect Four to Applet

    If you have a JPanel, you can just add it to a JApplet.

    I have a simple game that I deployed as both an application, applet, and webstart using that approach. You can check out the source for inspiration on how to do the same thing in your game: The Briny Depths by Kevin - Static Void Games
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    j0sh77 is offline Member
    Join Date
    May 2013
    Location
    Michigan, U.S.
    Posts
    4
    Rep Power
    0

    Default Re: Converting Connect Four to Applet

    Quote Originally Posted by KevinWorkman View Post
    If you have a JPanel, you can just add it to a JApplet.

    I have a simple game that I deployed as both an application, applet, and webstart using that approach. You can check out the source for inspiration on how to do the same thing in your game: The Briny Depths by Kevin - Static Void Games
    Thank you for the example. :D

    I've created a new ConnectFourApplet.java file as you did, and I've completely disregarded the ConnectFour.java (which creates the GUI), but it still does not run as I hoped.

    Converting Connect Four to Applet-applet.png

    ConnectFourApplet.java
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    @SuppressWarnings("serial")
    public class ConnectFourApplet extends JApplet{
    
    	public static boolean user;
    	public static Color[][] checker = new Color[8][8];
    	public static boolean[][] disabled = new boolean[8][8];
    
    	@Override
    	public void init(){
    
    		try {
    			SwingUtilities.invokeAndWait(new Runnable(){
    
    				@Override
    				public void run() {
    					//JFrame theGUI = new JFrame();
    					Container pane = null;
    					setContentPane(pane);
    					pane.setLayout(new GridLayout(7, 7));
    
    					//panel = new ConnectFourCP[8][8];
    					for(int y = 7; y >= 1; y--){
    						for(int x = 1; x <= 7; x++){
    							checker[x][y] = Color.white;
    							if(y != 1){
    								disabled[x][y] = true;
    							}
    							//panel[x][y] = new ConnectFourCP(x,y);
    							ConnectFourCP panel = new ConnectFourCP(x, y);
    							pane.add(panel);
    							ConnectFourCP.addPanel(x, y, panel);
    						}
    					}
    
    				}
    			});
    		}
    
    
    		catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,986
    Rep Power
    9

    Default Re: Converting Connect Four to Applet

    I would expect that code to throw a NullPointerException. You never set your pane variable equal to anything.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    j0sh77 is offline Member
    Join Date
    May 2013
    Location
    Michigan, U.S.
    Posts
    4
    Rep Power
    0

    Default Re: Converting Connect Four to Applet

    I appreciate all the help.

    I set it to getContentPane(); and I get the same error.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,986
    Rep Power
    9

    Default Re: Converting Connect Four to Applet

    You also seem to be trying to add a panel to more than one component, which you can't do.

    Your setup should be as follows:

    Setup the JPanel exactly how you would in a normal application.
    Instead of adding that JPanel to a JFrame, add it to a JApplet.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need Help Converting This to Applet Plz
    By RecursiveEndUserFromMars in forum Java Applets
    Replies: 2
    Last Post: 01-19-2011, 04:10 AM
  2. Converting java to applet...?
    By xtrapx in forum Java Applets
    Replies: 1
    Last Post: 10-16-2010, 05:38 PM
  3. Converting this Applet to an Application
    By karthus in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 04:30 AM
  4. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:07 AM
  5. Converting netbeans app to applet
    By paulious in forum New To Java
    Replies: 3
    Last Post: 10-23-2008, 11:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •