This is my 1st question here. I’m trying to build a White Page adjustable by zoom. It’s inside a JScrollPane, so the size of the JScrollPane’s ScrollBars are adjustable in the Dimension of that JPanel.
I want to adjust the size of those ScrollBars as the Size of the page (variables width and height in the code) + 2 borderSize, so the full size is equal the Page + margin of a borderSize around it. It works if zoom = 1.0.
If zoom < 1.0, the scroll bar is smaller than the Page and cut a piece of it. If zoom > 1 the Dimension size is way bigger than the page, leaving a huger border on its right and down corners, bigger than the borderSize.
How do I do this?
PS: I’m started learning java by myself, in the Quarantine last year, never had a teacher, just the internet, so any critics or suggestions, please, tell me.
Here’s the JPanel’s code:
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; } }
Zoom(values from 0.1 to 2.0).
How can i improve this? Also, i have no idea how to update the JScrollPane’s scrollbars together with the zoom.Thanks for the help.
UPDATE: i’ve created a minimal reproducible exemple.
Advertisement
Answer
I’ve finally did it. Started by not using transformation for scaling it, but making a new draw with the size zoomed, adapting all sizes in the method setSizes(), and adjusting the Dimension by those sizes. (Just changed this 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; } }
Thanks for all the help.