0

I allowed myself to copy an example from the answer because it concerns my problem. JScrollPane & Graphics2D I've problem with connection JScrollPane with Graphics2D scale. Is there any possibility to relate it?

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JPanel {

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                Test test = new Test();
                JFrame frame = new JFrame();
                JScrollPane scrollPane = new JScrollPane(test);
                frame.add(scrollPane);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(3000, 3000);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.scale(1, 0.3);//I need to scale my picture
        g2.drawLine(30, 30, 30, 3000);
        g2.drawLine(30, 400, 500, 3000);
    }
}

My question is: How to connect g2.scale(1, 0.3) with scrollPane? When I use g2.scale(1,0.3) then scrollPane shouldn't show up. But it's still there and scale doesn't work on it.
Thank you for any help.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Your question is not clear to me. Since class `Test` extends class `JPanel` and overrides its `paintComponent` method, I understand that you want `Test` to display a background image. If I understand correctly, then how is `JScrollPane` related? On the other hand, maybe you want `JScrollPane` to display a scaled image, in which case there is no need to extend `JPanel` and override method `paintComponent`. The following comment, in your code, gives me the impression that you want to display an image: _I need to scale my picture_. So do you want to display an image? – Abra Feb 25 '22 at 07:14
  • I want scale JScrollPane to new size of painted picture. G2.scale(1,0.3) works on g2.drawLine(30, 30, 30, 3000); g2.drawLine(30, 400, 500, 3000); How to resize the scroll bar?? – Kris_Holder Feb 25 '22 at 07:33

0 Answers0