简体   繁体   English

paintComponent没有画任何东西吗?

[英]paintComponent isn't painting anything?

I haven't been able to paint anything using an actionListener and paintComponent. 我无法使用actionListener和paintComponent绘制任何内容。 I'm pretty sure I have the image name/path right. 我很确定我具有正确的图像名称/路径。 What am I doing wrong? 我究竟做错了什么?

JApplet "Runner.java" JApplet“ Runner.java”

public class Runner extends JApplet{

    public void init(){
        World world = new World();
        Container screen = this.getContentPane();
        screen.add(world);
        setSize(1200, 800);
        repaint();
    }
}

part of "World.java" “ World.java”的一部分

protected ArrayList<WorldObject> allStillObjects = new ArrayList<WorldObject>();
protected ArrayList<MovableObject> allMovableObjects = new ArrayList<MovableObject>();
protected ArrayList<WorldObject> screenStillObjects = new ArrayList<WorldObject>();
protected ArrayList<MovableObject> screenMovableObjects = new ArrayList<MovableObject>();

public World(){
    this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
    this.setBackground(new Color(255,255,255));
    this.setFocusable(true);
    this.setFocusTraversalKeysEnabled(false);

    this.addObject(new Card(this, "Two of Clubs", 0, 0, "card_two_c.png"));

    timer = new Timer(60, new ClickListener());
    timer.start();
}

public void addObject(WorldObject obj){
    if(obj instanceof MovableObject){
        this.allMovableObjects.add((MovableObject)obj);
        if(isOnScreen(obj))
            this.screenMovableObjects.add((MovableObject)obj);  
    }else{
        this.allStillObjects.add(obj);
        if(isOnScreen(obj))
            this.screenStillObjects.add(obj);
    }
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    for(WorldObject obj : this.screenStillObjects)
        obj.paintComponent(g);
    for(MovableObject obj : this.screenMovableObjects)
        obj.paintComponent(g);
}

private class ClickListener implements ActionListener{

    public void actionPerformed(ActionEvent e){    
         repaint();
    }
}

"ImageObject.java" “ ImageObject.java”

protected ImageIcon pic;

public ImageObject(World world, String name, int worldX, int worldY, String imageName){
    super(world, name, worldX, worldY);
    URL imgURL = getClass().getResource("images/" + imageName);
    pic = new ImageIcon(imgURL);

    this.width = pic.getIconWidth();
    this.height = pic.getIconHeight();
}

public void paintComponent(Graphics g){
    super.paintComponent(); //doesn't work with or without this line
    pic.paintIcon(world, g, this.getWorldX(), this.getWorldY());
}

If there isn't enough of the code here I can add more 如果这里没有足够的代码,我可以添加更多

Edit: What's the best alternative to using a JApplet? 编辑:什么是使用JApplet的最佳替代方案? World extends JPanel, WorldObject extends JPanel, ImageObject extends WorldObject, MovableObject extends ImageObject, Card extends MovableObject World扩展JPanel,WorldObject扩展JPanel,ImageObject扩展WorldObject,MovableObject扩展ImageObject,Card扩展MovableObject

I added the method for addObject above. 我在上面添加了addObject的方法。

Well thank you for your answers, er, comments! 好,谢谢您的回答,呃,评论! They were very helpful. 他们非常有帮助。 I went and changed my program so that it used a JFrame instead of a JApplet, and after a little fiddling, got an image to print. 我去修改了程序,以使其使用JFrame而不是JApplet,经过一番摆弄后,得到了要打印的图像。 From there I was able to revert back to a JApplet and it works! 从那里,我能够恢复到JApplet,并且可以使用! Still not sure what my initial problem was though :| 仍然不确定我最初的问题是什么:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM