java jpanel paint,paint()方法不会在JPanel上绘制

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);

}

}

}

car.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;

}

}

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!

解决方案

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();

}

....

}

Replace your action functionality with this:

public void actionPerformed(ActionEvent e) {

this.repaint();

}

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值