用Java移动图像

时间:2012-02-28 16:33:10

标签: java image swing jframe

我想让我在绘画类中绘制的图像向左移动JFrame作为计时器滴答。但我不知道该怎么做。另外,我试图让我的程序在点击图像时使图像消失。

移动球类

import java.awt.Graphics2D;    
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;

public class movingball extends JPanel{

    private int move=50;
    private Timer timer = new Timer(move, new TimerListener());
    private int radius = 10;
    private int x = 300;    
    private int y = 0;

    public  movingball() {
          timer.start();
    }

    private class TimerListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        x=x-20;
        repaint();  }//trying to get the oval to move left 20 
    }

    protected void paintComponent(Graphics2D g){
    super.paintComponent(g);
    g.fillOval(x, 100 , radius * 2, radius * 2);    }
    }

movingControl类

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;    
import javax.swing.JPanel;

public class movingControl extends JPanel {

private movingball ball= new movingball();

public movingControl(){
    JPanel panel = new JPanel();
    ball.setBorder(new javax.swing.border.LineBorder(Color.red));
    panel.addMouseListener(new movingballListener());
    setLayout(new BorderLayout());
    add(ball, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);
}
}

SnniperGameApp类

我知道我拼错了狙击手

import javax.swing.JApplet;

public class SnniperGameApp extends JApplet {

static final long serialVersionUID = 2777718668465204446L;
//i dont know what this serial thing is. But my program wont start without it

public SnniperGameApp(){
    add(new movingControl());
}
}

ClickingEvent类

import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

class movingballListener extends MouseAdapter{

    public void mouseReleased(MouseEvent e) {
        if ((e.getModifiers() & InputEvent.BUTTON1_DOWN_MASK) != 0) {
          System.out.println(  (e.getPoint()));}}
    }

1 个答案:

答案 0 :(得分:3)

您没有正确覆盖paintComponent()

the javadoc开始,paintComponent()的签名为:

protected void paintComponent(Graphics g)

但你有:

protected void paintComponent(Graphics2D g)

方法签名必须匹配 - 如果需要,您可以安全地将Graphics转换为方法内的Graphics2D

@Override注释添加到方法是一种让编译器检查确实覆盖方法的好方法,而不仅仅是编写一个看起来相同的方法!


这是一个有效的SSCCE(为了节省空间,我已经内联了一些常量,不要把它当作实际代码的好习惯!):

public class MovingBall extends JPanel
{
    private Timer timer = new Timer(50, new TimerListener());
    private int x = 300;

    public MovingBall()
    {
        timer.start();
    }

    private class TimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            x -= 20;
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.fillOval(x, 100, 20, 20);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add(new MovingBall());
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}