draw and move images
Hello, everyone,
I am currently coding one applet and stuck in drawing and moving images. The assignment requirement is the following:
In this assignment, you are required to write a Java applet that emulates the 15-puzzle game.
You are free to use whatever development tools you see fit to write this Java applet, but the applet must use the JDK 1.1.x features, including the new event model, AWT, multimedia / imaging and exception handling.
In your applet, provide users the option to choose an image file as the starting image. When the game starts, randomly shuttle the image and show it in the grid. Play a short audio clip when the user attempts to make an illegal move. Upon successful completion of the game, play a pleasant audio clip to congratulate the user. You should design and implement your applet so that flickering is minimized.
My source code is the following:
Puzzle.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Puzzle extends JApplet {
private JButton scramble, exit;
private JPanel puzzlePanel,leftPanel,buttonPanel,imagePanel,showPanel;
private JPanel bPanel[];
private JPanel pPanel[];
private JComboBox imageComboBox;
private JCheckBox show;
private Image ys;
private Icon image1;
public void init()
{
super.init();
puzzlePanel = new JPanel();
leftPanel = new JPanel();
buttonPanel = new JPanel();
imagePanel = new JPanel();
showPanel = new JPanel();
bPanel = new JPanel[3];
pPanel = new JPanel[16];
scramble = new JButton("scramble");
exit = new JButton("exit");
imageComboBox = new JComboBox();
imageComboBox.setEditable(false);
imageComboBox.addItem("yellowstone1.jpg");
imageComboBox.addItem("yellowstone2.jpg");
imageComboBox.addItem("yellowstone3.jpg");
imageComboBox.addItem("yellowstone4.jpg");
show = new JCheckBox("show");
ys = getImage(getDocumentBase(),"yellowstone1.jpg");
ComboBoxHandler cbh = new ComboBoxHandler();
imageComboBox.addActionListener(cbh);
leftPanel.setLayout(
new GridLayout( 3,1 ) );
for(int i=0; i<3;i++){
bPanel = new JPanel();
}
bPanel[0].add(scramble);
bPanel[1].add(exit);
bPanel[2].add(imageComboBox);
buttonPanel.setLayout(
new GridLayout( 3,1 ) );
for(int i=0; i<3;i++){
buttonPanel.add(bPanel);
}
puzzlePanel.setLayout(new GridLayout(4,4,3,3));
for ( int i = 0; i < 16; i++ ) {
pPanel = new JPanel();
pPanel.setVisible(true);
pPanel.setBackground(Color.cyan);
puzzlePanel.add(pPanel);
}
leftPanel.add(buttonPanel);
imagePanel.setBackground(Color.lightGray);
leftPanel.add(imagePanel);
leftPanel.add(show);
Container c = getContentPane();
c.add( leftPanel, BorderLayout.EAST );
c.add( puzzlePanel, BorderLayout.CENTER );
}
public void paint(Graphics g)
{
g.drawImage(ys, 0, 0, imagePanel);
}
/*public void paint(Graphics g){
g.drawImage(ys,0,0,imagePanel);
}
*/
public static void main( String args[] )
{
// create window in which applet will execute
JFrame applicationWindow =
new JFrame( "An applet running as an application" );
applicationWindow.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
// create one applet instance
Puzzle appletObject = new Puzzle();
appletObject.setSize( 500,300 );
// call applet's init and start methods
appletObject.init();
appletObject.start();
// attach applet to center of window
applicationWindow.getContentPane().add( appletObject );
// set the window's size
applicationWindow.setSize( 500, 300);
// showing the window causes all GUI components
// attached to the window to be painted
applicationWindow.show();
}
private class ComboBoxHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
//ys = getImage(getDocumentBase(),"yellowstone1.jpg");
//imagePanel.add(image1);
//Graphics g = ys.getGraphics();
//g.drawImage(ys,0,0,imagePanel);
}
}
}
/*private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
for ( int i = 0; i < choices.length; i++ )
if ( e.getSource() == choices[ i ] ) {
drawingArea.setCurrentChoice( i );
break;
}
}
}
}*/
Puzzle.html
<html>
<applet code=SimpleImageViewer.class width=500 height=300>
</applet>
</html>
My questions are following:
1.how to draw image in the specific area: for example image panel or one grid of puzzle panel?
2.how to move image using mouse move, drag, drop event.eventlistener and eventhandler?
Thanks in advance.
John

