当前位置: 首页 > 面试题库 >

如何在Java中平滑JFrame的滚动

尚景焕
2023-03-14
问题内容

我的Java应用程序中有一个JFrame,其中包含一个JPanel在运行时创建的图形对象。问题是,在滚动JFrame大号图形时,滚动速度会变慢,并且滚动条无法平滑移动。请注意,我正在使用Graphics 2D对象,并且正在执行repaint滚动操作。

有什么办法可以使的滚动动作变得平滑JFrame

这是代码的一部分

public class DiagramPanel implements MouseListener{

    int click=0;
    Point p1;
    Point p2;
    private Dimension panelDimension;
    .... // variables

    public void go() {
        p1 = new Point();
        p2 = new Point();

        JFrame f = new JFrame();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setSize(1200,500);
        panelx = new DiaPanel();
        panelx.setOpaque(true);
        panelx.setBackground(Color.white);
        panelx.setAutoscrolls(true);
        panelx.addMouseListener(this);


        JScrollPane scrollPane = new JScrollPane();

        // scrollPane.add(panelx);

        ClassRectangle tempRect = null;
        for (ClassRectangle rect : this.classRectangles) {
            tempRect = rect;
        }


        Rectangle rect = new Rectangle();
        rect.setBounds(tempRect.getW() - 100, 0, 1000,
                tempLife.getEndpointY() * 500);

        panelDimension = new Dimension(0,0);
        for (ClassRectangle rectx : classRectangles){
            panelDimension.width=rectx.getW()+300;
        }
        for (LifeLine life : lifeLines) {
            panelDimension.height=life.getEndpointY()+300;
        }

        scrollPane.setViewportView(panelx);
        panelx.computeVisibleRect(rect);
        JScrollPane scrollPane1 = new JScrollPane(panelx);

        panelx.setPreferredSize(panelDimension);
        panelx.repaint();
        panelx.revalidate();
        p1.x=0;
        p1.y=0;
        p2.y=panelDimension.height;
        p2.x=panelDimension.width;
        f.add( scrollPane1);
        scrollPane.revalidate();
        f.setBackground(Color.white);
    }

    public DiagramPanel(ArrayList<Rectangle> classRectangles,
            ArrayList<Pair> pairs, ArrayList<Line> lines,
            ArrayList<Life> meth) {

            // constructing obj of DrawingPanel Here
    }

    public class SeqDiaPanel extends JPanel {
        /**
         * 
         */

        private static final long serialVersionUID = 1L;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d2 = (Graphics2D) g;
            g2d2.setColor(Color.orange);

            //grid
            for (int i = 0; i < panelDimension.height; i++) {
                g2d2.drawLine(0, 0 + i * 5, panelDimension.width+1000, 0 + i * 5);

            }

            for (int i = 0; i < panelDimension.width; i++) {
                g2d2.drawLine(0 + i * 5, 0, 0 + i *5,panelDimension.height+300);
            }

            g2d2.setColor(Color.black);

            // objects 
            .......... some objects here


            }
            }
            // draw Lines

            Stroke drawingStroke = new BasicStroke(2, BasicStroke.CAP_BUTT,
                    BasicStroke.JOIN_BEVEL, 0, new float[] { 5 }, 0);
            // Stroke drawingStroke = new BasicStroke();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(drawingStroke);
            for (Line life : lines) {
                g2d.drawLine(life.getStartpointX(), life.getStartpointY(),
                        life.getEndpointX(), life.getEndpointY());
                panelDimension.height=life.getEndpointY()+300;
            }

            // draw methodLfe
            for (Object2 ml1 : Obj2) {
                g2d2.fill3DRect(ml1.StartX(), ml1.getMethodStartY(),
                        ml1.getBreadth(), ml1.getEndX(),true);
            }

        }


    }

    // tobeused

    public int calculateWidth(String name){
        Font font = new Font("Serif", Font.BOLD, 12);
         FontMetrics metrics = new FontMetrics(font){

            /**
             * 
             */
            private static final long serialVersionUID = 1L;};
        int tempInt2=SwingUtilities.computeStringWidth( metrics, name);
        tempInt2=tempInt2+10;
        return tempInt2;
    }




    /*public class MouseClick implements MouseListener{
        Point p =  new Point(0,0);
        @Override
        public void mouseClicked(MouseEvent evnt) {

            p.x=evnt.getX();
            p.y=evnt.getY();
            System.out.println("MouseClicked @"+p.x+":"+p.y);

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

    }*/

    @Override
    public void mouseClicked(MouseEvent evnt) {
        click++;
        if(click==1){
        //Point p= new Point();
        p1.x=evnt.getX();
        p1.y=evnt.getY();
    //  System.out.println("MouseClicked1 @"+p1.x+":"+p1.y);
        }

        if(click==2){
            p2.x=evnt.getX();
            p2.y=evnt.getY();
            //System.out.println("MouseClicked2 @"+p2.x+":"+p2.y);
            click=0;
            if(p1.x<p2.x&&p1.y<p2.y){
            panelx.repaint();
            }
            else{

            }

        }/*else{
            p1.x=0;
            p1.y=0;
            p2.x=panelDimension.width+500;
            p2.y=panelDimension.height+700;
        }*/
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}

问题答案:

为什么不将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线,并且似乎在此处平滑滚动。



 类似资料:
  • 问题内容: 我给了我的JFrame窗口一个带有圆角的自定义形状,但是如何使它平滑(抗锯齿) 问题答案: 呈现内容的方式将有很多,但是基本概念是为要绘制的Graphics上下文提供呈现提示。 例如,如果我要绘画组件,则可能会使用类似…的方法。 查看控制渲染质量以获取更多详细信息 更新了示例 更新了新示例 我使用的技巧之一是,不使用“ setShape”,而是简单地创建一个透明窗口,并使用自定义面板为

  • 我的应用程序中的CoordinatorLayout中的平滑滚动有问题。 我试图做到这一点:http://wstaw.org/m/2015/10/02/google-scroll.gif 但我最好的结果是:http://wstaw.org/m/2015/10/02/my-scroll.gif 我做错了什么?提前感谢。

  • 我想实现像Google Play App一样的平滑滚动行为。我尝试过这里提到的解决方案: 解决方案1 解决方案2 解决方案3 但所有上述解决方案都没有像在Google Play应用程序中那样给出预期的结果。以下是xml代码: 如何实现平滑滚动? 编辑:以下是recyclerview适配器 }

  • 问题内容: 我已经尝试将平滑的滚动功能添加到我的网站已有一段时间了,但似乎无法使其正常工作。 这是与导航有关的HTML代码: 这是我添加的JS代码: 对于它的价值,这里是我什么我迄今所做收到信息,并在这里是我在它目前的形式现场。如果可以的话,我给您烤馅饼,饼干或其他东西。谢谢! 问题答案: 您真的需要那个插件吗?您可以为scrollTop属性设置动画:

  • 问题内容: 有没有一种方法可以使用jQuery向下滚动到锚链接? 喜欢: ? 问题答案: 这是我的方法: 然后,您只需要像这样创建锚: