Board index » jbuilder » how to invoke the paint method without any key event?

how to invoke the paint method without any key event?


2005-11-30 07:35:16 PM
jbuilder12
Dear everyone
when my application starts, the tank image doesn't show up after a
keystroke, how to show it up(invoke the paint method?) without any key
event?
package movetank;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class MoveTank extends JFrame implements KeyListener {
private Image image = null;
private Image imageUp = null;
private Image imageDown = null;
private Image imageLeft = null;
private Image imageRight = null;
private Image imageBullet = null;
private Graphics graphic= null;
private int xSite = 5;
private int ySite = 30;
private int xOldSite = 5;
private int yOldSite = 30;
private int xSlign = 500;
private int ySlign = 500;
private int xBSite, yBSite;
// private int xBOldSite,yBOldSite;
private int xySite[][] = new int[100][95];
private int i, j;
private int direction; //0:Up,1:Down,2:Right,3:Left
public MoveTank()
{
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Down.jpg")
imageDown =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Down.jpg");
imageUp =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Up.jpg");
imageLeft =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Left.jpg");
imageRight =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Right..jpg");
imageBullet =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\bullet.jpg");
this.setBackground(Color.white);
this.setResizable(false);
addKeyListener(this);
setTitle("MoveTank");
setSize(xSlign,ySlign);
setVisible(true);
Dimension d = getSize();
image = createImage(d.width, d.height);
graphic = image.getGraphics();
// g.setColor(Color.white);
// g.drawImgage(imageDown,25,25,this);
graphic.setColor(Color.white);
graphic.drawImage(imageDown, 25, 25, this);
// graphic.setColor(Color.white);
direction = 1;
for(i = 0; i < 100; i++){
for(j = 0; j < 95; j++) xySite[i][j] = 0;
}
markTank(xSite,ySite);
putXYSite();
System.out.print("dddd");
repaint();
System.out.print("dddd");
}
public static void main(String[] args) {
MoveTank tank = new MoveTank();
tank.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_UP:
if( ySite>30)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
ySite = yOldSite - 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageUp, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Up.jpg");
direction = 0;
//改变坦克的方?
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_DOWN: //移动的条?
if(ySite < 470)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
ySite = yOldSite + 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageDown, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Down.jpg");
direction = 1;
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_LEFT: //移动的条?
if(xSite>5 )
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
xSite = xOldSite - 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageLeft, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Left.jpg");
direction = 3;
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_RIGHT: //移动的条?
if( xSite < 470)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
xSite = xOldSite + 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageRight, xSite, ySite, this);
// image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Right..jpg");
direction = 2;
// System.out.println("direction:" + direction);
}
break;
default: break;
}
repaint();
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A)
{
switch(direction)
{
case 0:
xBSite = xSite+10;
yBSite = ySite-5;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(0, xBSite, yBSite);
break;
case 1:
xBSite = xSite+10;
yBSite = ySite+25;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(1, xBSite, yBSite);
break;
case 2:
xBSite = xSite+25;
yBSite = ySite+10;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(2, xBSite, yBSite);
break;
case 3:
xBSite = xSite-5;
yBSite = ySite+10;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(3, xBSite, yBSite);
default: break;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g)
{
if(image!=null){
g = getGraphics();
g.setColor(Color.white);
// g.fillRect(xOldSite,yOldSite,25,25);
//用一个长方形,跟背景颜色一样的把原来的坦克覆盖
g.drawImage(image, 0, 0, this); //把新的图片贴?
}//if(imageBullet!=null)
//g.drawImage(image2, 100, 100, this);
}
public void markTank(int a, int b)
//将坦克所在的地方相应数组置一
{
int k= 5, d= 5;
for(k = 0; k < 5; k++)
{
for(d = 0; d < 5; d++)
{
xySite[(a-5)/5+k][(b-30)/5+d] = 1;
}
}
}
public void clearMarkTank(int a, int b)
//将坦克所在的地方相应数组?
{
int k= 5, d= 5;
for(k = 0; k < 5; k++)
{
for(d = 0; d < 5; d++)
{
xySite[(a-5)/5+k][(b-30)/5+d] = 0;
}
}
}
public void putXYSite() //测试用的
{
for(i = 0; i < 100; i++)
{
for(j = 0; j < 95; j++)
{
if(xySite[i][j]==1)
System.out.println("xySite["+i+"]"+"["+j+"]=" + xySite[i][j]);
}
}
}
public void moveBullet(int dir, int x1, int y1)
{
/* int x2,y2;
int flag=1;
switch(dir)
{
case 0:
while(flag==1)
{
if(y1==30)
{
graphic.fillRect(x1, y1, 5, 5);
flag = 0;
break;
}
x2 =x1;
y2= y1;
y1=y2+5;
graphic.fillRect(x2, y2, 5, 5);
graphic.drawImage(imageBullet, x1, x2, this);
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {}
}
break;
default: break;
}*/
}
}
 
 

Re:how to invoke the paint method without any key event?

Dear everyone
when my application starts, the tank image doesn't show up after a
keystroke, how to show it up(invoke the paint method?) without any key
event?
package movetank;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class MoveTank extends JFrame implements KeyListener {
private Image image = null;
private Image imageUp = null;
private Image imageDown = null;
private Image imageLeft = null;
private Image imageRight = null;
private Image imageBullet = null;
private Graphics graphic= null;
private int xSite = 5;
private int ySite = 30;
private int xOldSite = 5;
private int yOldSite = 30;
private int xSlign = 500;
private int ySlign = 500;
private int xBSite, yBSite;
// private int xBOldSite,yBOldSite;
private int xySite[][] = new int[100][95];
private int i, j;
private int direction; //0:Up,1:Down,2:Right,3:Left
public MoveTank()
{
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Down.jpg")
imageDown =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Down.jpg");
imageUp =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Up.jpg");
imageLeft =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Left.jpg");
imageRight =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\Right..jpg");
imageBullet =
getContentPane().getToolkit().createImage("d:\\tank\\MoveTank\\image\\bullet.jpg");
this.setBackground(Color.white);
this.setResizable(false);
addKeyListener(this);
setTitle("MoveTank");
setSize(xSlign,ySlign);
setVisible(true);
Dimension d = getSize();
image = createImage(d.width, d.height);
graphic = image.getGraphics();
// g.setColor(Color.white);
// g.drawImgage(imageDown,25,25,this);
graphic.setColor(Color.white);
graphic.drawImage(imageDown, 25, 25, this);
// graphic.setColor(Color.white);
direction = 1;
for(i = 0; i < 100; i++){
for(j = 0; j < 95; j++) xySite[i][j] = 0;
}
markTank(xSite,ySite);
putXYSite();
System.out.print("dddd");
repaint();
System.out.print("dddd");
}
public static void main(String[] args) {
MoveTank tank = new MoveTank();
tank.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_UP:
if( ySite>30)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
ySite = yOldSite - 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageUp, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Up.jpg");
direction = 0;
//改变坦克的方?
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_DOWN: //移动的条?
if(ySite < 470)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
ySite = yOldSite + 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageDown, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Down.jpg");
direction = 1;
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_LEFT: //移动的条?
if(xSite>5 )
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
xSite = xOldSite - 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageLeft, xSite, ySite, this);
//image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Left.jpg");
direction = 3;
// System.out.println("direction:" + direction);
}
break;
case KeyEvent.VK_RIGHT: //移动的条?
if( xSite < 470)
{
clearMarkTank(xSite, ySite);
xOldSite = xSite;
yOldSite = ySite;
xSite = xOldSite + 5;
markTank(xSite,ySite);
putXYSite();
graphic.fillRect(xOldSite, yOldSite, 25, 25);
graphic.drawImage(imageRight, xSite, ySite, this);
// image =
getContentPane().getToolkit().createImage("G:\\java\\MoveTank\\image\\Right..jpg");
direction = 2;
// System.out.println("direction:" + direction);
}
break;
default: break;
}
repaint();
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A)
{
switch(direction)
{
case 0:
xBSite = xSite+10;
yBSite = ySite-5;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(0, xBSite, yBSite);
break;
case 1:
xBSite = xSite+10;
yBSite = ySite+25;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(1, xBSite, yBSite);
break;
case 2:
xBSite = xSite+25;
yBSite = ySite+10;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(2, xBSite, yBSite);
break;
case 3:
xBSite = xSite-5;
yBSite = ySite+10;
graphic.drawImage(imageBullet, xBSite, yBSite, this);
moveBullet(3, xBSite, yBSite);
default: break;
}
}
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g)
{
if(image!=null){
g = getGraphics();
g.setColor(Color.white);
// g.fillRect(xOldSite,yOldSite,25,25);
//用一个长方形,跟背景颜色一样的把原来的坦克覆盖
g.drawImage(image, 0, 0, this); //把新的图片贴?
}//if(imageBullet!=null)
//g.drawImage(image2, 100, 100, this);
}
public void markTank(int a, int b)
//将坦克所在的地方相应数组置一
{
int k= 5, d= 5;
for(k = 0; k < 5; k++)
{
for(d = 0; d < 5; d++)
{
xySite[(a-5)/5+k][(b-30)/5+d] = 1;
}
}
}
public void clearMarkTank(int a, int b)
//将坦克所在的地方相应数组?
{
int k= 5, d= 5;
for(k = 0; k < 5; k++)
{
for(d = 0; d < 5; d++)
{
xySite[(a-5)/5+k][(b-30)/5+d] = 0;
}
}
}
public void putXYSite() //测试用的
{
for(i = 0; i < 100; i++)
{
for(j = 0; j < 95; j++)
{
if(xySite[i][j]==1)
System.out.println("xySite["+i+"]"+"["+j+"]=" + xySite[i][j]);
}
}
}
public void moveBullet(int dir, int x1, int y1)
{
/* int x2,y2;
int flag=1;
switch(dir)
{
case 0:
while(flag==1)
{
if(y1==30)
{
graphic.fillRect(x1, y1, 5, 5);
flag = 0;
break;
}
x2 =x1;
y2= y1;
y1=y2+5;
graphic.fillRect(x2, y2, 5, 5);
graphic.drawImage(imageBullet, x1, x2, this);
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {}
}
break;
default: break;
}*/
}
}