Our homework called for us to move a gif around in a canvas in an applet. Since it is an intro course, they used constants but I'm thinking, what if the user resizes the applet? So, I wrote the following code:
[code=java]
import java.applet.App let;
import java.awt.*;
import java.awt.event. *;
public class MoveIt extends Applet implements ActionListener
{
private Image cupImage;
private int iTop = 15;
private int iLeft = 15;
private Canvas drawingCanvas = new Canvas();
public void init()
{
cupImage = getImage(getDoc umentBase(), "cup.gif");
this.setLayout( new BorderLayout()) ;
this.setSize(30 0, 300);
this.setBackgro und(Color.BLUE) ;
drawingCanvas.s etBackground(Co lor.BLUE);
this.add(drawin gCanvas, BorderLayout.NO RTH);
Panel keyPanel = new Panel();
keyPanel.setLay out(new BorderLayout()) ;
Button upButton = new Button("Up");
keyPanel.add(up Button,BorderLa yout.NORTH);
upButton.addAct ionListener(thi s);
Button centerButton = new Button("Center" );
keyPanel.add(ce nterButton,Bord erLayout.CENTER );
centerButton.ad dActionListener (this);
Button downButton = new Button("Down");
keyPanel.add(do wnButton,Border Layout.SOUTH);
downButton.addA ctionListener(t his);
Button leftButton = new Button("Left");
keyPanel.add(le ftButton,Border Layout.WEST);
leftButton.addA ctionListener(t his);
Button rightButton = new Button("Right") ;
keyPanel.add(ri ghtButton,Borde rLayout.EAST);
rightButton.add ActionListener( this);
this.add(keyPan el, BorderLayout.SO UTH);
}
public void paint(Graphics g)
{
g.drawImage(cup Image, iLeft, iTop, this);
}
public void actionPerformed (ActionEvent e)
{
String sItem = e.getActionComm and();
char cFirst = sItem.charAt(0) ;
switch (cFirst)
{
case 'C':
Rectangle rCanvas = drawingCanvas.g etBounds();
int iImageWidth = cupImage.getWid th(this);
int iImageHeight = cupImage.getHei ght(this);
iLeft = (rCanvas.width - iImageWidth)/2;
iTop = (rCanvas.height - iImageHeight)/2;
break;
case 'U':
iTop -= 15;
break;
case 'D':
iTop += 15;
break;
case 'L':
iLeft -= 15;
break;
case 'R':
iLeft += 15;
break;
}
repaint();
}
}
[/code]
My problem is in the Case 'C': rCanvas.height is zero! Why is that? How should I get the height & width of the Canvas. Also, I would like for the gif to be centered initially, but I don't know how to get its size without displaying it. Note, just use any small gif for cup.gif; my gif is 69x85. TIA. --Sam
[code=java]
import java.applet.App let;
import java.awt.*;
import java.awt.event. *;
public class MoveIt extends Applet implements ActionListener
{
private Image cupImage;
private int iTop = 15;
private int iLeft = 15;
private Canvas drawingCanvas = new Canvas();
public void init()
{
cupImage = getImage(getDoc umentBase(), "cup.gif");
this.setLayout( new BorderLayout()) ;
this.setSize(30 0, 300);
this.setBackgro und(Color.BLUE) ;
drawingCanvas.s etBackground(Co lor.BLUE);
this.add(drawin gCanvas, BorderLayout.NO RTH);
Panel keyPanel = new Panel();
keyPanel.setLay out(new BorderLayout()) ;
Button upButton = new Button("Up");
keyPanel.add(up Button,BorderLa yout.NORTH);
upButton.addAct ionListener(thi s);
Button centerButton = new Button("Center" );
keyPanel.add(ce nterButton,Bord erLayout.CENTER );
centerButton.ad dActionListener (this);
Button downButton = new Button("Down");
keyPanel.add(do wnButton,Border Layout.SOUTH);
downButton.addA ctionListener(t his);
Button leftButton = new Button("Left");
keyPanel.add(le ftButton,Border Layout.WEST);
leftButton.addA ctionListener(t his);
Button rightButton = new Button("Right") ;
keyPanel.add(ri ghtButton,Borde rLayout.EAST);
rightButton.add ActionListener( this);
this.add(keyPan el, BorderLayout.SO UTH);
}
public void paint(Graphics g)
{
g.drawImage(cup Image, iLeft, iTop, this);
}
public void actionPerformed (ActionEvent e)
{
String sItem = e.getActionComm and();
char cFirst = sItem.charAt(0) ;
switch (cFirst)
{
case 'C':
Rectangle rCanvas = drawingCanvas.g etBounds();
int iImageWidth = cupImage.getWid th(this);
int iImageHeight = cupImage.getHei ght(this);
iLeft = (rCanvas.width - iImageWidth)/2;
iTop = (rCanvas.height - iImageHeight)/2;
break;
case 'U':
iTop -= 15;
break;
case 'D':
iTop += 15;
break;
case 'L':
iLeft -= 15;
break;
case 'R':
iLeft += 15;
break;
}
repaint();
}
}
[/code]
My problem is in the Case 'C': rCanvas.height is zero! Why is that? How should I get the height & width of the Canvas. Also, I would like for the gif to be centered initially, but I don't know how to get its size without displaying it. Note, just use any small gif for cup.gif; my gif is 69x85. TIA. --Sam
Comment