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

如何在Java中平滑JFrame的滚动

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

如何在Java中平滑JFrame的滚动

为什么不将

Graphics2D
图形放在(大的)图形中
BufferedImage
并在滚动窗格的标签中显示?像这样的东西(动画,5000x5000px):

import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.util.Random;import javax.swing.*;public class BigScrollImage {    BigScrollImage() {        final int x = 5000;        final int y = 5000;        final BufferedImage bi = new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB);        Graphics2D g1 = bi.createGraphics();        g1.setColor(Color.BLACK);        g1.fillRect(0, 0, x, y);        g1.dispose();        final JLabel label = new JLabel(new ImageIcon(bi));        ActionListener listener = new ActionListener() { Random rand = new Random(); @Override public void actionPerformed(ActionEvent ae) {     Graphics2D g2 = bi.createGraphics();     int x1 = rand.nextInt(x);     int x2 = rand.nextInt(x);     int y1 = rand.nextInt(y);     int y2 = rand.nextInt(y);     int r = rand.nextInt(255);     int g = rand.nextInt(255);     int b = rand.nextInt(255);     g2.setColor(new Color(r,g,b));     g2.drawLine(x1,y1,x2,y2);     g2.dispose();     label.repaint(); }        };        Timer t = new Timer(5,listener);        JScrollPane scroll = new JScrollPane(label);        Jframe f = new Jframe("Big Scroll");        f.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);        f.add(scroll);        f.pack();        f.setSize(800, 600);        f.setLocationByPlatform(true);        f.setVisible(true);        t.start();    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable(){ @Override public void run() {     new BigScrollImage(); }        });    }}

它试图每秒绘制200线,并且似乎在此处平滑滚动。



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

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

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