到达某一点时滚动对象

时间:2013-11-17 23:21:51

标签: java animation applet awt thread-sleep

我想让对象在达到y pos = 170时向上滚动。然后当它达到51时,它将再次向下滚动。这是我的代码..

import java.awt.*;
import java.applet.*;

public class ani1 extends Applet implements Runnable{

    Thread run01;
    int spacex = 51,spacey = 91;
    int score = 0;

    public void start() {
        if (run01 == null){
            run01 = new Thread(this);
            run01.start();
        }
    }
    public void stop() {
        if(run01 != null){
            run01 = null;
        }
    }
    public void run() {
        while(true){
            repaint();
            try {
                Thread.sleep(30);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        } 
    }
    public void paint(Graphics g){
        g.setColor(Color.gray);
        g.fillRoundRect(35, 70, 250, 300, 10, 250);
        g.fillRect(230,20,50,50);
        g.setColor(Color.white);
        g.fillRoundRect(45,85,230,100,10,250);
        g.setColor(Color.black);
        g.drawRect(50, 90, 150, 90);
        g.drawString("SPACE-X",215,100);
        g.drawString("Level: 1",210,120);
        g.drawString("Score : "+ score, 210,135);
        g.drawString("Life : - - -",210,150);
        g.fillRect(spacex, spacey, 10, 10);
        spacey++;
        if(spacey >170){
            spacey--;
        }

    }

}

1 个答案:

答案 0 :(得分:1)

基本上,您应该尝试对要移动的对象进行建模,即使这只是建模Point,然后您可以使用它来渲染任意形状。

这意味着您不会想要在绘画过程中做出逻辑决策。绘画过程应该只专注于绘制模型。

例如......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Spacy {

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

    public Spacy() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Movable> movables;

        public TestPane() {
            movables = new ArrayList<>(25);
            movables.add(new SpaceObject());

            Timer timer;
            timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (Movable m : movables) {
                        m.move(getSize());
                    }
                    repaint();
                }
            });
            timer.start();

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Paintable p : movables) {
                p.paint(g);
            }
            g2d.dispose();
        }
    }

    public interface Paintable {

        public void paint(Graphics g);

    }

    public interface Movable extends Paintable {

        public void setLocation(Point p);
        public Point getLocation();
        public void move(Dimension size);

    }

    public class SpaceObject implements Movable {

        private Point p;

        public SpaceObject() {
            setLocation(new Point(150, 50));
        }

        @Override
        public void setLocation(Point p) {
            this.p = p;
        }

        @Override
        public Point getLocation() {
            return p;
        }

        @Override
        public void paint(Graphics g) {

            g.setColor(Color.RED);
            Point p = getLocation();
            int radius = 10;
            g.fillOval(p.x - (radius / 2), p.y - (radius / 2), radius, radius);

        }

        @Override
        public void move(Dimension size) {
            Point p = getLocation();
            int delatX = 0;
            int delatY = 0;
            int gap = size.height / 4;
            if (p.y == gap) {
                delatX = 2;
            } else if (p.y == size.height - gap) {
                delatX = -2;
            }
            gap = size.width / 4;
            if (p.x == gap) {
                delatY = -2;
            } else if (p.x == size.width - gap) {
                delatY = 2;
            }
            p.x += delatX;
            p.y += delatY;

            if (p.x < (size.width / 4)) {
                p.x = size.width / 4;
            } else if (p.x > (size.width - (size.width / 4))) {
                p.x = size.width - (size.width / 4);
            }
            if (p.y < (size.height / 4)) {
                p.y = size.height / 4;
            } else if (p.y > (size.height - (size.height / 4))) {
                p.y = size.height - (size.height / 4);
            }

            setLocation(p);
        }            
    }        
}

您的代码存在两个基本问题。

  1. 您正在尝试决定如何在paint方法中绘制输出。通常这是不可取的。当你走向其他坏习惯时
  2. 在绘画之前,您没有更新Graphics上下文。基本上正在发生的事情是,你被赋予了你在上一个绘画周期中使用的相同Graphics上下文,这意味着当你到达终点并想要开始向上滚动时,你之前绘制的内容仍然存在。 / LI>

    我会建议;

    1. 您可以避免使用Applet并使用JApplet,事实上,我完全避免使用applet,只使用JFrame作为基本容器,它们的问题较少。
    2. 根据JPanel之类的内容创建自定义组件,并覆盖它的paintComponent方法并在那里执行自定义绘画。
    3. 请确保首先致电super.paintComponent,因为这会为您准备Graphics背景
    4. 就个人而言,我会避免在这种情况下使用Thread,它会引入额外的复杂情况,相反,我会使用javax.swing.Timer,但那只是我。
    5. 您可以查看Performing Custom Painting了解更多详情