初学者求助:currentImage.paintIcon(this,page,x,y)中的this是什么意思?是引用还是什么?
这是整个程序.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DirectionPanel extends JPanel {
private final int width=300,height=200,jump=10,image_size=31;
private ImageIcon up,down,right,left,currentImage;
private int x,y;
public DirectionPanel()
{
addKeyListener(new DirectionListener());
x=width/2;
y=height/2;
up=new ImageIcon("up.jpg");
down=new ImageIcon("down.jpg");
left=new ImageIcon("left.jpg");
right=new ImageIcon("right.jpg");
currentImage=right;
setBackground(Color.black);
setPreferredSize(new Dimension(width,height));
setFocusable(true);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
currentImage.paintIcon(this,page,x,y);
}
private class DirectionListener implements KeyListener
{
public void keyPressed(KeyEvent event)
{
switch(event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage=up;
y-=jump;
break;
case KeyEvent.VK_DOWN:
currentImage=down;
y+=jump;
break;
case KeyEvent.VK_LEFT:
currentImage=left;
x-=jump;
break;
case KeyEvent.VK_RIGHT:
currentImage=right;
x+=jump;
break;
}
repaint();
}
public void keyTyped(KeyEvent event){}
public void keyReleased(KeyEvent event){}
}
}
作业帮用户2017-09-20举报