hi there,
I am a student at the University of Portsmouth in the UK in my third and last year. I am doing my dissertation as a game using applets for a client at the university of which there consits four prototypes. One of the main specification points is that it should work on an apple mac, therefore I am using Swing applets. however with my first prototype it works fine on windows in windows Explorer, firefox and Netscape but i cant get it to work on Mac os 9 or X even with the latest version of java installed on the machine. Can anyone tell me why this is so, I would be very gratefull.
As I cant post the whole applet here as a file download I will just put some code snippets of the main program classes.
Applet ------------------------------
/**
* Class JugglingBalls - A sub class of JApplet that combines the GameInterface and GamePanel into a single application
*
* @author (Liam Morren)
* @version (v1.0)
*/
import java.awt.*;
import javax.swing.*;
public class JugglingBalls extends JApplet
{
// instance variables -----------------------------------------------------------------------------------------------------------------------------------
private GamePanel gamePanel; // The game panel including game and render loop
private GameInterface gameInterface; // The swing interface to modify the game scene components
// JApplet methods -------------------------------------------------------------------------------------------------------------------------------------
/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
gamePanel = new GamePanel();
gameInterface = new GameInterface(gamePanel.getGameScene());
Container content = getContentPane(); // The container for components in the applet
setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
content.add(gamePanel);
content.add(gameInterface);
}
/**
* Returns information about this applet.
* An applet should override this method to return a String containing
* information about the author, version, and copyright of the JApplet.
*
* @return a String representation of information about this JApplet
*/
public String getAppletInfo()
{
// provide information about the applet
return "Title: JugglingBalls \nAuthor: Liam Morren \nA simple application showing balls juggling in the air.";
}
}
Interface -------------------------------------
/**
* Class GameInterface - Lets you manipulate the game components using swing components
*
* @author (Liam Morren)
* @version (v1.0)
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class GameInterface extends JPanel
{
// instance variables -------------------------------------------------------------------------------------------------------------------------------------
private GameScene gameScene; // Reference to the game scene
private JTabbedPane gameTabs; // Tabs for Game, Balls and Background
// Scene panel
JButton pause, start, reset; // Buttons
// Balls panel
JRadioButton redBall, greenBall, blueBall, archBall, figureOfEightBall; // Radio buttons
JCheckBox canSplit; // Check box
JComboBox setAllPaths, setAllTypes, newBall, selectBall; // Drop down boxes
JButton deleteBall; // Button
Ball currentBall; // Current ball being changed
// Background panel
JRadioButton black, white, red, green, blue, hide, show, stop, move, arch, figureOfEight; // Radio buttons
// Constructors -------------------------------------------------------------------------------------------------------------------------------------------
/**
* Constructor for objects of class GameInterface
*/
public GameInterface(GameScene gameSceneIn)
{
gameScene = gameSceneIn; // Reference to the game scene
gameTabs = new JTabbedPane(); // JTabbedPane to hold different toolbars
gameTabs.setPreferredSize(new Dimension(200, 494)); // Set the preffered size for the tabbed pane
currentBall = gameScene.getBall(0); // First ball
// Add the panels to the tabbed pane
gameTabs.addTab("Scene", makeScenePanel());
gameTabs.addTab("Balls", makeBallsPanel());
gameTabs.addTab("Background", makeBackgroundPanel());
add(gameTabs); // Add the tab to the game interface panel
setPreferredSize(new Dimension(200, 200));
}
// Other methods ------------------------------------------------------------------------------------------------------------------------------------------
/**
* makeScenePanel
*
* @return JPanel - The scene panel with buttons added
*/
private JPanel makeScenePanel()
{
// Make scene panel
JPanel scenePanel = new JPanel();
scenePanel.setLayout(new BoxLayout(scenePanel, BoxLayout.Y_AXIS)); // Change layout
pause = new JButton("Pause"); // Make buttons
start = new JButton("Start");
reset = new JButton("Reset");
// Add button actions
pause.addActionListener(new ActionListener() // Add button listener to pause button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.setPaused(true); // Pause game
}
});
start.addActionListener(new ActionListener() // Add button listener to start button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.setPaused(false); // Unpause game
}
});
reset.addActionListener(new ActionListener() // Add button listener to reset button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.reset(); // Reset game
updateSelectBallComboBox();
changeSelectedBall(0);
}
});
scenePanel.add(pause); // Add buttons to scene panel
scenePanel.add(start);
scenePanel.add(reset);
return scenePanel;
}
/**
* makeBackgroundPanel
*
* @return JPanel - The background panel with buttons added
*/
private JPanel makeBackgroundPanel()
{
///////////////////
// Make scene panel
///////////////////
JPanel backgroundPanel = new JPanel();
backgroundPanel.setLayout(new GridLayout(5, 1, 2, 2)); // Change layout
///////////////
// Make buttons
///////////////
// background colour buttons
/////////////////
JPanel backgroundcolour = new JPanel(); // Change backgroundcolour panel
backgroundcolour.setBorder(BorderFactory.createLineBorder(Color.black));
black = new JRadioButton("Black"); // Radio buttons to change colour
white = new JRadioButton("white");
black.setSelected(true); // Set black as already selected
ButtonGroup backColourGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
backColourGroup.add(black); // Add buttons to group
backColourGroup.add(white);
backgroundcolour.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
backgroundcolour.add(new JLabel("Background colour")); // Add label
backgroundcolour.add(black); // Add buttons to colours panel
backgroundcolour.add(white);
// Add radio button actions
black.addActionListener(new ActionListener() // Add button listener to black button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.setBackgroundColour(false); // Background colour = black
}
});
white.addActionListener(new ActionListener() // Add button listener to white button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.setBackgroundColour(true); // Background colour = white
}
});
// colour buttons
/////////////////
JPanel colours = new JPanel(); // Change colours panel
colours.setBorder(BorderFactory.createLineBorder(Color.black));
red = new JRadioButton("Red"); // Radio buttons to change colour
green = new JRadioButton("Green");
green.setSelected(true); // Set green as already selected
blue = new JRadioButton("Blue");
ButtonGroup colourGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
colourGroup.add(red); // Add buttons to group
colourGroup.add(green);
colourGroup.add(blue);
colours.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
colours.add(new JLabel("Change colours")); // Add label
colours.add(red); // Add buttons to colours panel
colours.add(green);
colours.add(blue);
// Add radio button actions
red.addActionListener(new ActionListener() // Add button listener to red button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (gameScene.getBackgroundEffect().getColour() != 0)
gameScene.getBackgroundEffect().setColour(0); // Background effect colour = red
}
});
green.addActionListener(new ActionListener() // Add button listener to green button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (gameScene.getBackgroundEffect().getColour() != 1)
gameScene.getBackgroundEffect().setColour(1); // Background effect colour = green
}
});
blue.addActionListener(new ActionListener() // Add button listener to blue button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (gameScene.getBackgroundEffect().getColour() != 2)
gameScene.getBackgroundEffect().setColour(2); // Background effect colour = blue
}
});
// hide buttons
///////////////
JPanel hideShow = new JPanel(); // Change hideShow panel
hideShow.setBorder(BorderFactory.createLineBorder(Color.black));
hide = new JRadioButton("Hide"); // Radio buttons to hide show
show = new JRadioButton("Show");
show.setSelected(true); // Set show as already selected
ButtonGroup hideShowGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
hideShowGroup.add(hide); // Add buttons to group
hideShowGroup.add(show);
hideShow.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
hideShow.add(new JLabel("Hide / Show")); // Add label
hideShow.add(hide); // Add buttons to hideShow panel
hideShow.add(show);
// Add radio button actions
hide.addActionListener(new ActionListener() // Add button listener to hide button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().hide(); // Hide
}
});
show.addActionListener(new ActionListener() // Add button listener to show button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().unHide(); // Unhide
}
});
// stopMove buttons
///////////////
JPanel stopMove = new JPanel(); // Change StopMove panel
stopMove.setBorder(BorderFactory.createLineBorder(Color.black));
stop = new JRadioButton("Stop"); // Radio buttons to stop move
move = new JRadioButton("Move");
stop.setSelected(true); // Set stop as already selected
ButtonGroup stopMoveGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
stopMoveGroup.add(stop); // Add buttons to group
stopMoveGroup.add(move);
stopMove.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
stopMove.add(new JLabel("Stop / Move")); // Add label
stopMove.add(stop); // Add buttons to stopMove panel
stopMove.add(move);
// Add radio button actions
stop.addActionListener(new ActionListener() // Add button listener to stop button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().setMove(false); // stop
}
});
move.addActionListener(new ActionListener() // Add button listener to move button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().setMove(true); // move
}
});
// changePath buttons
///////////////
JPanel changePath = new JPanel(); // Change changePath panel
changePath.setBorder(BorderFactory.createLineBorder(Color.black));
arch = new JRadioButton("Arch"); // Radio buttons to change path
figureOfEight = new JRadioButton("Figure of eight");
arch.setSelected(true); // Set stop as already selected
ButtonGroup changePathGroup = new ButtonGroup(); // Group buttons so only one can be pressed at a time
changePathGroup.add(arch); // Add buttons to group
changePathGroup.add(figureOfEight);
changePath.setLayout(new GridLayout(2, 2, 0, 0)); // Change layout
changePath.add(new JLabel("Change Path")); // Add label
changePath.add(arch); // Add buttons to changePath panel
changePath.add(figureOfEight);
// Add radio button actions
arch.addActionListener(new ActionListener() // Add button listener to arch button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().setPath(new Arch()); // Arch
}
});
figureOfEight.addActionListener(new ActionListener() // Add button listener to figureOfEight button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
gameScene.getBackgroundEffect().setPath(new FigureOfEight()); // Figure of eight
}
});
/////////////////////////////////////
// Add components to background panel
/////////////////////////////////////
backgroundPanel.add(backgroundcolour);
backgroundPanel.add(colours);
backgroundPanel.add(hideShow);
backgroundPanel.add(stopMove);
backgroundPanel.add(changePath);
return backgroundPanel;
}
/**
* makeBallsPanel
*
* @return JPanel - The ball panel with buttons added
*/
private JPanel makeBallsPanel()
{
// Make balls panel
JPanel ballsPanel = new JPanel();
ballsPanel.setLayout(new BoxLayout(ballsPanel, BoxLayout.Y_AXIS)); // Change layout
///////////////
// Make buttons
///////////////
// set all drop down boxes
/////////////////
JPanel setAll = new JPanel(); // Set all panel
setAll.setBorder(BorderFactory.createLineBorder(Color.black));
String[] setAllPathsChoices = {"Arch", "Figure of eight"}; // Selections for set all paths
setAllPaths = new JComboBox(setAllPathsChoices); // Add selections to combo box
String[] setAllTypesChoices = {"Primary ball", "Pastel ball", "3D ball", "Image ball"}; // Selections for set all types
setAllTypes = new JComboBox(setAllTypesChoices); // Add selections to combo box
setAll.add(new JLabel("Set all paths")); // Add combo boxes to set all panel
setAll.add(setAllPaths);
setAll.add(new JLabel("Set all types"));
setAll.add(setAllTypes);
// Add combo box actions
setAllPaths.addActionListener(new ActionListener() // Add selection listener to set all paths combo box
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (setAllPaths.getSelectedItem() == "Arch")
gameScene.setAllPathsArch();
else
gameScene.setAllPathsFigureOfEight();
}
});
setAllTypes.addActionListener(new ActionListener() // Add selection listener to set all types combo box
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (setAllTypes.getSelectedItem() == "Primary ball")
gameScene.setAllTypesPrimaryBall();
else if (setAllTypes.getSelectedItem() == "Pastel ball")
gameScene.setAllTypesPastelBall();
else if (setAllTypes.getSelectedItem() == "Image ball")
gameScene.setAllTypesImageBall();
else
gameScene.setAllTypesBall3D();
}
});
// new ball drop down box
/////////////////////////
JPanel newBallPanel = new JPanel(); // new ball panel
newBallPanel.setBorder(BorderFactory.createLineBorder(Color.black));
String[] newType = {"Primary ball", "Pastel ball", "3D ball", "Image ball"}; // Selections for new ball
newBall = new JComboBox(newType); // Add selections to combo box
newBallPanel.add(new JLabel("New ball")); // Add combo boxes to new ball panel
newBallPanel.add(newBall);
// Add combo box actions
newBall.addActionListener(new ActionListener() // Add selection listener to newBall combo box
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
String ballType = (String) newBall.getSelectedItem();
if (ballType == "Primary ball")
gameScene.addBall(new PrimaryBall(gameScene));
else if (ballType == "Pastel ball")
gameScene.addBall(new PastelBall(gameScene));
else if (ballType == "Image ball")
gameScene.addBall(new ImageBall(gameScene));
else
gameScene.addBall(new Ball3D(gameScene));
updateSelectBallComboBox();
changeSelectedBall(gameScene.getBalls().size() - 1); // Update interface to show new ball
}
});
// selected ball panel
/////////////////////////
JPanel selectedBallPanel = new JPanel(); // selected ball panel
selectedBallPanel.setBorder(BorderFactory.createLineBorder(Color.black));
// Select ball
selectBall = new JComboBox(gameScene.getBallList()); // Add ball list to the select drop down box
deleteBall = new JButton("DeleteBall"); // Delete button
// Ball colour
redBall = new JRadioButton("Red"); // Red choice
redBall.setSelected(true); // Start as selected
greenBall = new JRadioButton("Green"); // Green choice
blueBall = new JRadioButton("Blue"); // Blue choice
ButtonGroup ballColour = new ButtonGroup(); // So only one colour can be selected at a tim
ballColour.add(redBall);
ballColour.add(greenBall);
ballColour.add(blueBall);
// Path
archBall = new JRadioButton("Arch"); // Arch path choice
archBall.setSelected(true); // Start as selected
figureOfEightBall = new JRadioButton("Figure of eight"); // Figure of eight path choice
ButtonGroup ballPath = new ButtonGroup(); // So only one path can be selected at a tim
ballPath.add(archBall);
ballPath.add(figureOfEightBall);
// Can split
canSplit = new JCheckBox("Can split", false); // Initialy not selected
selectedBallPanel.add(new JLabel("Select ball")); // Add components to selected ball panel
selectedBallPanel.add(selectBall);
selectedBallPanel.add(deleteBall);
selectedBallPanel.add(new JLabel("Ball colour"));
selectedBallPanel.add(redBall);
selectedBallPanel.add(greenBall);
selectedBallPanel.add(blueBall);
selectedBallPanel.add(new JLabel("Path"));
selectedBallPanel.add(archBall);
selectedBallPanel.add(figureOfEightBall);
selectedBallPanel.add(canSplit);
// Add select ball drop down box action
selectBall.addActionListener(new ActionListener() // Add selection listener to selectBall combo box
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
changeSelectedBall(selectBall.getSelectedIndex()); // Change the selected ball using the index from the select ball drop down box
}
});
// Add delete button action
deleteBall.addActionListener(new ActionListener() // Add selection listener to deleteBall button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
int tempIndex = selectBall.getSelectedIndex(); // Temp index
if (gameScene.getBalls().size() > 1) // If there is more than 1 ball left
{
gameScene.removeBall(tempIndex); // Remove ball
updateSelectBallComboBox(); // Update with new ball list
changeSelectedBall(tempIndex - 1); // Update interface with new current selected ball
}
}
});
// Add radio button colour actions
redBall.addActionListener(new ActionListener() // Add button listener to redBall button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (currentBall.getColour() != 0)
currentBall.setColour(0); // currentBall colour = red
}
});
greenBall.addActionListener(new ActionListener() // Add button listener to greenBall button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
if (currentBall.getColour() != 1)
currentBall.setColour(1); // currentBall colour = green
}
});
blueBall.addActionListener(new ActionListener() // Add button listener to blueBall button
{
public void actionPerformed(ActionEvent ev) // Background colour = blue
{
if (currentBall.getColour() != 2)
currentBall.setColour(2); // currentBall colour = blue
}
});
// Add path radio button actions
archBall.addActionListener(new ActionListener() // Add button listener to arch button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
currentBall.setPath(new Arch()); // Arch
}
});
figureOfEightBall.addActionListener(new ActionListener() // Add button listener to figureOfEight button
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
currentBall.setPath(new FigureOfEight()); // Figure of eight
}
});
// Add can split check button actions
canSplit.addActionListener(new ActionListener() // Add button listener to can split
{
public void actionPerformed(ActionEvent ev) // Overide actionPerformed() method
{
currentBall.setCanGiveBirth(canSplit.isSelected()); // Ball can split if can split check button is selected
}
});
/////////////////////////////////////
// Add components to ball panel
/////////////////////////////////////
ballsPanel.add(setAll);
ballsPanel.add(newBallPanel);
ballsPanel.add(selectedBallPanel);
return ballsPanel;
}
/**
* changeSelectedBall
*
* @param ballIndexIn - The numbered index of the ball in the game scene list of balls. If less than 0 then equals 0. If more than ball list size then equals last ball
*
*/
public void changeSelectedBall(int ballIndexIn)
{
if (ballIndexIn < 0) // Make sure index is within bounds
ballIndexIn = 0;
else if (ballIndexIn >= gameScene.getBalls().size())
ballIndexIn = gameScene.getBalls().size() - 1; // Else put within bounds at either end
currentBall.highlight(false); // Unhighlight current ball
currentBall = gameScene.getBall(ballIndexIn); // Get new current ball
currentBall.highlight(true); // Highlight new current ball
// Update controls to new ball
selectBall.setSelectedIndex(ballIndexIn);
switch (currentBall.getColour()) // Update colour controls
{
case 0: redBall.setSelected(true); break;
case 1: greenBall.setSelected(true); break;
default: blueBall.setSelected(true); break;
}
if (currentBall.getPath() instanceof Arch) // If path is arch
archBall.setSelected(true); // Update controls as arch
else
figureOfEightBall.setSelected(true); // Else figure of eight
canSplit.setSelected(currentBall.canGiveBirth()); // Set if the ball can split or not
}
/**
* updateSelectBallComboBox
*
* This method updates the combo box selection model with a new one made from the updated balls arraylist from the game scene
*/
public void updateSelectBallComboBox()
{
DefaultComboBoxModel model = new DefaultComboBoxModel(gameScene.getBallList());
selectBall.setModel(model);
}
}
Kind regards
Liam morren