标题:为啥我的Paint方法没有被调用呢?
只看楼主
zhao9302
Rank: 1
等 级:新手上路
帖 子:170
专家分:0
注 册:2007-5-10
 问题点数:0 回复次数:3 
为啥我的Paint方法没有被调用呢?
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowEvent;
import java.util.ArrayList;


public class TankClient extends Frame
{
    public static int GameHeight = 600;
    public static int GameWidth = 800;
    
    static TankClient tc = new TankClient();
    
    ArrayList<Tank> tanks = new ArrayList<Tank>();
    ArrayList<Missile> missiles = new ArrayList<Missile>();
    Image offScreenImage = null;
    
    Tank myTank = new Tank(this.tc,400,200,Direction.U);
    
    public static void main(String args[])
    {   
        tc.loginFrame();
    }
    
    private void loginFrame()
    {
        Frame f = new Frame();
        f.setTitle("TankWar");
        f.setLocation(200, 100);
        f.setSize(GameWidth, GameHeight);
        f.setBackground(Color.GREEN);
        f.setResizable(false);
        f.setVisible(true);
        f.addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
        new Thread(new PaintThread()).start();
    }
    
    public void paint(Graphics g)
    {
        g.drawString("EnemyTank Count:" + tanks.size(), 10, 70);
        myTank.paint(g);
    }
    
    public void update(Graphics g)
    {
        if(offScreenImage == null)
            offScreenImage = this.createImage(GameWidth,GameHeight);
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = g.getColor();
        gOffScreen.setColor(Color.GREEN);
        gOffScreen.fillRect(0, 0, GameWidth,GameHeight);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    
    private class PaintThread implements Runnable
    {

        public void run()
        {
            repaint();
            while(true)
            {
                try
                {
                    repaint();
                    Thread.sleep(30);
                } catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
    }

}
搜索更多相关主题的帖子: Paint awt import Tank java 
2008-08-12 00:57
jdk2006
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2007-5-12
得分:0 
我也遇到过这样的问题。
 Frame f = new Frame();
创建新的Frame实例,而不能在调用paint方法。
如果直接采用this(当前的TankClient类)就应该行。
我想paint方法是对当前的component有效吧。
附个代码你看看:
Frame窗体加载背景的实现
来源:http://ohgrateboy.blog.
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import *;
import javax.imageio.ImageIO;
public class GameFrame extends JFrame{
    private static final int Width=Toolkit.getDefaultToolkit().getScreenSize().width;
     private static final int Heigth=Toolkit.getDefaultToolkit().getScreenSize().height;

private BufferedImage bfimage;
 

public GameFrame(){
  

         this.setTitle("JAVA五子棋设计");
                this.setResizable(false);//不可以最大化
  

        this.setSize(590,580);
   

       this.setLocation(Width-800, Heigth-700);
  

       this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  

      this.setVisible(true);
      }
       public void paint(Graphics g){
               //加入背景图片
               try{
  

                File file=new File("src\\image\\wuziqi.jpg");
  

               bfimage=ImageIO.read(file);
  

      }catch(Exception e){}
  

      g.drawImage(bfimage, 0, 30, this)
       }
       public static void main(String[] args) {
                new GameFrame();
 }

2008-08-12 05:22
zhao9302
Rank: 1
等 级:新手上路
帖 子:170
专家分:0
注 册:2007-5-10
得分:0 
谢谢啦

我要一步一步往上爬……
2008-08-12 14:20
lzz4438253
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2007-1-4
得分:0 
你们paint都不先调用super.paint(g)吗。。。

2008-08-12 22:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-227781-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.688723 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved