栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java上绘制2个朝不同方向移动的球,但一个消失了

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java上绘制2个朝不同方向移动的球,但一个消失了

问题…

  1. while-loop
    在事件调度线程中,该线程可调整图形对象的位置
  2. Thread.sleep
    paint
    方法上。
  3. 不打电话
    super.paintComponent
  4. paintComponent
    方法中更新对象的状态。

Swing使用一个单线程模型,该模型负责将重绘请求分派给所有组件。

在EDT中执行任何停止处理这些事件的操作,将防止Swing重新绘制UI。这将使您的动画看起来像突然从一个步骤到整个步骤都消失了。

有关更多详细信息,请参阅Swing中的并发。特别要看一看初始线程和如何使用Swing计时器

我要强调第4点

您无法控制重绘周期。重涂请求可能是出于多种原因而提出的,这些原因并非您所要求的,这将导致您的对象无法控制或在您不希望被更新时被更新。您绝对不能从任何

paint
方法中更改UI的任何部分的状态。

简单的例子

这是一个非常简单的示例,但它演示了在Swing中制作任何动画时需要了解的基本概念

public class SimpleBouncyBall {    public static void main(String[] args) {        new SimpleBouncyBall();    }    public SimpleBouncyBall() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (Exception ex) {     }     Jframe frame = new Jframe("Test");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.add(new CourtPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    public class CourtPane extends JPanel {        private Ball ball;        private int speed = 5;        public CourtPane() { Timer timer = new Timer(40, new ActionListener() {     @Override     public void actionPerformed(ActionEvent e) {         Rectangle bounds = new Rectangle(new Point(0, 0), getSize());         if (ball == null) {  ball = new Ball(bounds);         }         speed = ball.move(speed, bounds);         repaint();     } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start();        }        @Override        public Dimension getPreferredSize() { return new Dimension(100, 100);        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g);  if (ball != null) {     Graphics2D g2d = (Graphics2D) g.create();     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);     Point p = ball.getPoint();     g2d.translate(p.x, p.y);     ball.paint(g2d);     g2d.dispose(); }        }    }    public class Ball {        private Point p;        private int radius = 12;        public Ball(Rectangle bounds) { p = new Point(); p.x = 0; p.y = bounds.y + (bounds.height - radius) / 2;        }        public Point getPoint() { return p;        }        public int move(int speed, Rectangle bounds) { p.x += speed; if (p.x + radius >= (bounds.x + bounds.width)) {     speed *= -1;     p.x = ((bounds.x + bounds.width) - radius) + speed; } else if (p.x <= bounds.x) {     speed *= -1;     p.x = bounds.x + speed; } p.y = bounds.y + (bounds.height - radius) / 2; return speed;        }        public void paint(Graphics2D g) { g.setColor(Color.RED); g.fillOval(0, 0, radius, radius);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号