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

使用Graphics2D绘制图像

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

使用Graphics2D绘制图像

您的代码充满了问题:

  1. 不要改写
    Jframe.paint()
    ,尤其是不打电话时
    super
    。设置一个ContentPane并覆盖其
    paintComponent()
    。虽然看起来很方便,但通常这是一个糟糕的设计,并且不必要。
  2. 不要覆盖
    JComponent.paint()
    ,而是覆盖
    JComponent.paintComponent()
    (并调用
    super
  3. 使用
    JLabel
    显示图像。这要简单得多。
  4. 不要混合使用AWT(Canvas)和Swing(Jframe)。坚持摇摆。

这是一个简单的示例,显示Bowser在框架中移动。(这很有趣,当您减小帧大小并使用帧边框打图像时;-))

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.MalformedURLException;import java.net.URL;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.UnsupportedLookAndFeelException;public class TestAnimation2 {    private static final int NB_OF_IMAGES_PER_SECOND = 50;    private static final int WIDTH = 800;    private static final int HEIGHT = 600;    private Random random = new Random();    private double dx;    private double dy;    private double x = WIDTH / 2;    private double y = HEIGHT / 2;    protected void initUI() throws MalformedURLException {        final Jframe frame = new Jframe(TestAnimation2.class.getSimpleName());        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        frame.setLayout(null);        final JLabel label = new JLabel(new ImageIcon(new URL("http://www.lemondedemario.fr/images/dossier/bowser/bowser.png")));        label.setSize(label.getPreferredSize());        frame.setMinimumSize(label.getPreferredSize());        frame.add(label);        frame.setSize(WIDTH, HEIGHT);        dx = getNextSpeed();        dy = getNextSpeed();        Timer t = new Timer(1000 / NB_OF_IMAGES_PER_SECOND, new ActionListener() { @Override public void actionPerformed(ActionEvent e) {     x += dx;     y += dy;     if (x + label.getWidth() > frame.getContentPane().getWidth()) {         x = frame.getContentPane().getWidth() - label.getWidth();         dx = -getNextSpeed();     } else if (x < 0) {         x = 0;         dx = getNextSpeed();     }     if (y + label.getHeight() > frame.getContentPane().getHeight()) {         y = frame.getContentPane().getHeight() - label.getHeight();         dy = -getNextSpeed();     } else if (y < 0) {         y = 0;         dy = getNextSpeed();     }     label.setLocation((int) x, (int) y); }        });        frame.setVisible(true);        t.start();    }    private double getNextSpeed() {        return 2 * Math.PI * (0.5 + random.nextDouble());    }    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {        SwingUtilities.invokeLater(new Runnable() { @Override public void run() {     try {         new TestAnimation2().initUI();     } catch (MalformedURLException e) {         // TODO Auto-generated catch block         e.printStackTrace();     } }        });    }}


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

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

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