需要帮助调整 Graphics2D 中的缩放系统 - Java

Need help to adjust a Zoom system in Graphics2D - Java

这是我的第一个问题。我正在尝试构建一个可通过缩放调整的白页。它位于 JScrollPane 内,因此 JScrollPane 的 ScrollBar 的大小可在该 JPanel 的维度中调整。

我想将这些滚动条的大小调整为页面大小(代码中的变量宽度和高度)+ 2 borderSize,因此完整大小等于页面 + 其周围 borderSize 的边距。如果 zoom = 1.0.

有效

如果zoom < 1.0,则滚动条比Page小,切一块。如果 zoom > 1,则 Dimension 尺寸比页面大得多,在其右下角留下更大的边框,大于 borderSize。

我该怎么做?

PS:自学java,去年隔离期间,没有老师,只有网络,有什么批评或建议,请告诉我。

这是 JPanel 的代码:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JSlider;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

public class Main2 {
private MyPanel mp = new MyPanel();
private JFrame frame;
private JSlider zoomSlider = new JSlider();
private JLabel zoomLabel = new JLabel("Zoom: XXX");

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main2 window = new Main2();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public Main2() {
    initialize();
}


private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 619, 403);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout springLayout = new SpringLayout();
    frame.getContentPane().setLayout(springLayout);
    
    
    springLayout.putConstraint(SpringLayout.SOUTH, zoomSlider, 40, SpringLayout.NORTH, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, zoomSlider, -115, SpringLayout.EAST, frame.getContentPane());
    zoomSlider.setValue(100);
    zoomSlider.setSnapToTicks(true);
    zoomSlider.setPaintTicks(true);
    zoomSlider.setMaximum(200);
    zoomSlider.setMinorTickSpacing(5);
    zoomSlider.setMinimum(5);
    springLayout.putConstraint(SpringLayout.NORTH, zoomSlider, 0, SpringLayout.NORTH, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, zoomSlider, 0, SpringLayout.WEST, frame.getContentPane());
    frame.getContentPane().add(zoomSlider);
    
    JScrollPane scrollPane = new JScrollPane(mp);
    springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, zoomSlider);
    springLayout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, frame.getContentPane());
    frame.getContentPane().add(scrollPane);
    
    
    springLayout.putConstraint(SpringLayout.NORTH, zoomLabel, 10, SpringLayout.NORTH, frame.getContentPane());
    springLayout.putConstraint(SpringLayout.WEST, zoomLabel, 6, SpringLayout.EAST, zoomSlider);
    frame.getContentPane().add(zoomLabel);
    
    
    frame.addWindowStateListener(new WindowStateListener() {

        @Override
        public void windowStateChanged(WindowEvent arg0) {
            // TODO Auto-generated method stub
            mp.draw();
        }
        
    });
    
    
    zoomSlider.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            int temp = (zoomSlider.getValue())-zoomSlider.getValue()%5;
            setZoom(temp);
            
            mp.draw();
            
        }

        @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) {
            int temp = (zoomSlider.getValue())-zoomSlider.getValue()%5;
            setZoom(temp);
            
            
            mp.draw();
        
            
        }
        
        
        
    });
    
    mp.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getPreciseWheelRotation() < 0) {
                
                setZoom(zoomSlider.getValue()- 5);
           
            } else {
                setZoom(zoomSlider.getValue()+ 5);
            }
        // zoom += e.getPreciseWheelRotation();
            if (mp.getZoom()*100 < 10) {
                setZoom(10);
            }
            mp.draw();
        }
        });
    
    AdjustmentListener adj = new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            setZoom(zoomSlider.getValue());
            mp.draw();
            
        }
        
    };
    scrollPane.getVerticalScrollBar().addAdjustmentListener(adj);
    scrollPane.getHorizontalScrollBar().addAdjustmentListener(adj);
    
    
}
public void setZoom(int n) {
    mp.setZoom(n);
    zoomSlider.setValue(n);
    zoomLabel.setText("Zoom: "+mp.getZoom()+"x");
    
}
}
class MyPanel extends JPanel{

private static final long serialVersionUID = -716735372803790424L;

int borderSize=28;
int zoom=100;
int height = 3565;
int width = 2537;
int widthz, heightz; 

public MyPanel() {
setBackground(Color.DARK_GRAY);
}

@Override
public Dimension getPreferredSize() {
int a, b;
String temp;
Float x, y;
x=(getZoom()*width); //Size of the page adjusted by zoom
y=(getZoom()*height);

temp = x.toString(); 
String temp1[] = temp.split("\."); // converted to string to convert it to int
a = Integer.valueOf(temp1[0])+2*borderSize; //that value + 2 BorderSize
temp = y.toString();
String temp2[] = temp.split("\.");
b = Integer.valueOf(temp2[0])+2*borderSize;

return new Dimension (a,b);
}

@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d = putZoom(g2d);
g2d.setColor(Color.WHITE);
g2d.fillRect(this.getX(),this.getY(), width, height);
g2d.setColor(Color.BLACK);
g2d.drawRect(this.getX()+borderSize,this.getY()+borderSize,width-2*borderSize,height-2*borderSize);
g2d.dispose();

}

public Graphics2D putZoom(Graphics2D g) {

AffineTransform at = new AffineTransform();
at.translate(borderSize,borderSize); // put the page a borderSize from the upper-left corner
at.scale(getZoom(),getZoom()); //adjust the page as zoom 

Graphics2D g2d = g;
g2d.setTransform(at);
return g2d;
}

public void draw() { //this method is to update the draw from the main
repaint();

}

public Float getZoom() {
return Float.valueOf(zoom)/100;
}
public void setZoom(int zom) { //this method is to update Zoom from the main

zoom=zom;
String zoomheight []= (String.valueOf(getZoom()*height)).split("\.");
heightz = Integer.valueOf(zoomheight[0]);
String zoomwidth []= (String.valueOf(getZoom()*width)).split("\.");
widthz = Integer.valueOf(zoomwidth[0]);

}

public int getZoomInt() {
return this.zoom;
}

}

缩放(值从 0.1 到 2.0)。

我该如何改进?此外,我不知道如何更新 JScrollPane 的滚动条以及 zoom.Thanks 以寻求帮助。

更新:我创建了一个最小的可重现示例。

简介

在你更新你的问题之前我就开始研究这个了。我使用了缩放百分比而不是缩放系数。

我创建了以下 GUI 并将初始状态设置为 30%。

我将内部 JPanel 制作成棋盘格,这样您可以更轻松地查看缩放。我修改了您的初始值,因此内部 JPanel 将代表一张 8 1/2 x 11 的纸,每英寸 50 像素。

这是 100% 的相同 GUI。

这是 10% 的相同 GUI。

说明

我创建了一个 JFrame 和一个控件 JPanel 来保存 JSlider。我使用 GridLayout 创建控件 JPanel.

我创建了一个内部 JPanel 来保存绘图,并创建了一个显示 JPanel 来保存 JScrollPane。我使显示 JPanel 与内部 JPanel 的大小成比例,这样我就不会有任何拉伸问题。

让 GUI 重新生效/重新绘制被证明是最大的挑战。我最终不得不 invalidate JScrollPaneJScrollBars 和显示 JPanel。每次更改缩放百分比时,我还必须将 JScrollBars 重置为零。

代码

这是完整的可运行代码。我制作了所有 类 内部 类 所以我可以 post 这作为一个代码块。

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ZoomJPanelGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ZoomJPanelGUI());
    }
    
    private int zoomPercentage = 30;
    
    private DisplayPanel displayPanel;
    
    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("Zoom JPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createControlPanel(), BorderLayout.BEFORE_FIRST_LINE);
        this.displayPanel = new DisplayPanel(zoomPercentage);
        frame.add(displayPanel.getPanel(), BorderLayout.AFTER_LAST_LINE);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createControlPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
        
        JLabel label = new JLabel("Zoom Percentage");
        label.setFont(panel.getFont().deriveFont(Font.BOLD, 24f));
        panel.add(label);
        
        JSlider slider =  new JSlider(
                JSlider.HORIZONTAL, 10, 100, zoomPercentage);
        slider.setFont(panel.getFont().deriveFont(16f));
        slider.setMajorTickSpacing(30);
        slider.setMinorTickSpacing(5);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent event) {
                JSlider slider = (JSlider) event.getSource();
                if (!slider.getValueIsAdjusting()) {
                    zoomPercentage = (int) slider.getValue();
                    displayPanel.setZoomPercentage(zoomPercentage);
                    displayPanel.repaint();
                    frame.pack();
                }
            }   
        });
        
        panel.add(slider);
        
        return panel;
    }
    
    public class DisplayPanel {
        
        private InnerPanel innerPanel;
        
        private final JPanel panel;
        
        private JScrollPane scrollPane;
        
        private int zoomPercentage;
        
        public DisplayPanel(int zoomPercentage) {
            this.zoomPercentage = zoomPercentage;
            this.panel = createDisplayPanel();
        }

        private JPanel createDisplayPanel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            
            this.innerPanel = new InnerPanel(zoomPercentage);
            scrollPane = new JScrollPane(innerPanel);
            scrollPane.setPreferredSize(new Dimension(475, 600));
            panel.add(scrollPane, BorderLayout.CENTER);
            
            return panel;
        }
        
        public void setZoomPercentage(int zoomPercentage) {
            this.zoomPercentage = zoomPercentage;
            innerPanel.setZoomPercentage(zoomPercentage);
        }

        public JPanel getPanel() {
            return panel;
        }

        public void repaint() {
            innerPanel.repaint();
            scrollPane.invalidate();
            JScrollBar hScrollBar = scrollPane.getHorizontalScrollBar();
            JScrollBar vScrollBar = scrollPane.getVerticalScrollBar();
            hScrollBar.setValue(0);
            vScrollBar.setValue(0);
            hScrollBar.invalidate();
            vScrollBar.invalidate();
            panel.invalidate();
        }
        
    }
    
    public class InnerPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private int maximumBorderSize = 25;
        private int maximumCellSize = 50;
        private int maximumHeight = 5500;
        private int maximumWidth = 4250;
        private int zoomPercentage;
        
        public InnerPanel(int zoomPercentage) {
            this.zoomPercentage = zoomPercentage;
        }
        
        public void setZoomPercentage(int zoomPercentage) {
            this.zoomPercentage = zoomPercentage;
        }

        @Override
        public Dimension getPreferredSize() {
            int width = maximumWidth * zoomPercentage / 100;
            int height = maximumHeight * zoomPercentage / 100;
            return new Dimension(width, height);
        }
        
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            
            int borderSize = maximumBorderSize * zoomPercentage / 100;
            paintBackground(g2d);
            paintBorder(g2d, borderSize);
            paintCheckerboard(g2d, borderSize);
        }

        private void paintBackground(Graphics2D g2d) {
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }

        private void paintBorder(Graphics2D g2d, int borderSize) {
            g2d.setColor(Color.BLACK);
            g2d.setStroke(new BasicStroke(3f));
            g2d.drawRect(borderSize, borderSize, getWidth() - 2 * borderSize,
                    getHeight() - 2 * borderSize);
        }

        private void paintCheckerboard(Graphics2D g2d, int borderSize) {
            int cellSize = maximumCellSize * zoomPercentage / 100;
            int width = maximumWidth - maximumBorderSize * 2 - 2;
            int height = maximumHeight - maximumBorderSize * 2 - 2;
            int cellWidth = width / maximumCellSize;
            int cellHeight = height / maximumCellSize;
            
            boolean isBlue = true;
            int x = borderSize;
            int y = borderSize;
            int heightRemainder = height - cellHeight * cellSize;
            for (int i = 0; i < cellHeight; i++) {
                int widthRemainder = width - cellWidth * cellSize;
                for (int j = 0; j < cellWidth; j++) {
                    if (isBlue) {
                        g2d.setColor(Color.BLUE);
                    } else {
                        g2d.setColor(Color.YELLOW);
                    }
                    isBlue = !isBlue;
                    g2d.fillRect(x, y, cellSize, cellSize);
                    x += cellSize;
                    if (widthRemainder > 0) {
                        x++;
                        widthRemainder--;
                    }
                }
//              isBlue = !isBlue;
                x = borderSize;
                y += cellSize;
                if (heightRemainder > 0) {
                    y++;
                    heightRemainder--;
                }
            }
        }
        
    }

}

我终于做到了。首先不使用转换来缩放它,而是使用缩放的大小进行新绘制,在方法 setSizes() 中调整所有大小,并根据这些大小调整维度。 (刚改了这个class)

class MyPanel extends JPanel{

private static final long serialVersionUID = -716735372803790424L;

int borderSize=28;
int zoom=100;
int height = 3565;
int width = 2537;
int widthz, heightz;
int maxHeight, maxWidth; //max size of draw
int maxAreaHeight, maxAreaWidth; //max size of area

public MyPanel() {
    setBackground(Color.DARK_GRAY);
}

@Override
public Dimension getPreferredSize() {
    setSizes();
    return new Dimension (maxAreaWidth,maxAreaHeight);
}

@Override
public void paintComponent (Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d= createBase(g2d);

}

public void draw() { //this method is to update the draw from the main
    repaint();

}

public Float getZoom() {return Float.valueOf(zoom)/100;}
public void setZoom(int zom) {zoom=zom;}
public int getZoomInt() {return this.zoom;}

public void setSizes () {
    widthz= width*getZoomInt()/100;
    heightz=height*getZoomInt()/100;
    maxHeight = heightz+2*borderSize;
    maxWidth = widthz +2*borderSize;
    maxAreaHeight = this.getY()+maxHeight;
    maxAreaWidth = this.getX()+maxWidth;

    if (this.getSize() != new Dimension(maxAreaWidth, maxAreaHeight)) {
        this.setSize(maxAreaWidth, maxAreaHeight);
    }
}
public Graphics2D createBase(Graphics2D g2d) {
    Graphics2D g = g2d;
    setSizes();

    g.setColor(Color.WHITE);
    g.fillRect(this.getX()+borderSize,this.getY()+borderSize, widthz, heightz);
    g.setColor(Color.BLACK);
    g.drawRect(this.getX()+borderSize+borderSize*zoom/100,this.getY()+borderSize+borderSize*zoom/100,widthz-2*borderSize*zoom/100,heightz-2*borderSize*zoom/100);


    return g;
}

}

感谢大家的帮助。