paint()方法不会在JPanel上绘制 [英] paint() method would not draw on JPanel

查看:214
本文介绍了paint()方法不会在JPanel上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很少的Java绘图源代码,但是它们工作正常,但是当我尝试制作自己的源代码时,我无法获得 paint(Grahpics g)工作方法!我再次查看了已有的代码,并查看了Oracle页面中的一些教程,但我似乎无法知道为什么它不起作用。
有人可以检查一下并告诉我这里有什么问题吗?

I tried few source codes of drawing in java and they were working fine, but when i tried to make one of my own I could not get the paint(Grahpics g) method to work! I looked again at the codes I have and checked some of the tutorials in Oracle's pages but i don't seem to be able to know why it would not work. can someone please check it and tell me what is wrong here??

主要方法:
公共类主要

main method: public class main

{
    public static void main(String[] args) 
    {
        new board();
    }
}

board:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class board implements ActionListener
{
    private JFrame f = new JFrame("Speedy");
    private JPanel gamePanel = new JPanel();


    private Image bg = new ImageIcon(this.getClass().getResource("road.png")).getImage();
    private Timer t;


    private car myCar = new car();


    public board()
    {
        t = new Timer(50,this);
        t.start();


        gamePanel.setSize(600,400);
        gamePanel.setDoubleBuffered(true);
        gamePanel.setFocusable(true);
        gamePanel.addKeyListener(new TAdapter());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(gamePanel,BorderLayout.CENTER);
        //f.addKeyListener(new TAdapter());
        f.setBounds(200,100,600,400);
        f.setVisible(true);
        f.revalidate();
        f.repaint();

    }




    public void paint(Graphics g) {
        gamePanel.paint(g);

        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(bg,0,0,null);
        g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);

        System.out.println("Painted");

        g.dispose();
    }



    public void actionPerformed(ActionEvent e) 
    {
        gamePanel.repaint();
        //System.out.println("Painting..");
    }





    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {}

        public void keyPressed(KeyEvent e)  
        {
            myCar.keyPressed(e);
            System.out.println("You pressed: "+e);
        }
    }

}

汽车。 java:

    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;

    import javax.swing.ImageIcon

;



public class car 
{

    private Image image;
    public int xPos,yPos;

    public car()
    {
        image = new ImageIcon(this.getClass().getResource("car.png")).getImage();
        xPos = 300;
        yPos = 200;
        System.out.println(image.getWidth(null));
    }



    public Image getImg() {return image;}


    public void move() {}


    public void keyPressed(KeyEvent e) 
    {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) xPos -= 1;
        if (key == KeyEvent.VK_RIGHT)xPos += 1;
        if (key == KeyEvent.VK_UP)   yPos -= 1;
        if (key == KeyEvent.VK_DOWN) yPos += 1;
    }

}

没有错误,显示我的图像宽度是正确的,计时器也会触发 ActionListener KeyListener 也可以,但是图像不会绘制! paint(Graphics g)方法只是不想被触发!
谷歌搜索没有帮助..我以为这将是一个普遍的问题,但是没有人遇到我的问题,所有解决方案都使我失败。请
帮助吗?
如果有人可以解释,将不胜感激!

There are no errors, it shows me the width of the image which is right, also the timer triggers the ActionListener, also KeyListener is working, but the images would not draw! the paint(Graphics g) method just does not want to get triggered! Googling it did not help.. I thought this would be a common problem but nobody has the problem I have, all solutions failed me. help please? If someone can explain it would be most appreciated!

推荐答案

您的班级委员会不会扩展 JPanel 类。因此,Swing永远不会调用 paint()方法。
此外,语句 gamePanel.repaint()将仅执行默认的 JPanel gamePanel 的> paint()方法。相反,您希望执行覆盖的 paint 方法,因此可能需要这样做:

Your class Board does not extend the JPanel class. So the paint() method is never called by the Swing. Also, the statement gamePanel.repaint() will only execute the default JPanel paint() method of gamePanel. Instead you want the overridden paint method to be executed, so might want to do this:

public class Board extends JPanel implements ActionListener {
   ....
    public void paint(Graphics g) {
       this.paint(g);

       Graphics2D g2d = (Graphics2D)g;
       g2d.drawImage(bg,0,0,null);
       g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);

       System.out.println("Painted");

       g2d.dispose();
    }
    ....
}

替换您的操作

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

替代解决方案:
如果您不希望您的 Board 类扩展 JPanel ,您还可以覆盖 paint()初始化 gamePanel 方法。

Alternative solution: If you do not want your Board class to extend JPanel, you can also override the paint() method of the gamePanel as you initialize it.

gamePanel = new JPanel() {
   @Override
   public void paint(Graphics g) {
       this.paint(g);

       Graphics2D g2d = (Graphics2D)g;
       g2d.drawImage(bg,0,0,null);
       g2d.drawImage(myCar.getImg(), myCar.xPos, myCar.yPos, null);

       g2d.dispose();
   }
};

但是,我建议使用第一个解决方案,而不要使用匿名类

However, I would recommend the first solution rather than this one with anonymous classes.

这篇关于paint()方法不会在JPanel上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆