2021年1月6日星期三

Adding JScrollPane to a drawing [duplicate]

I've been developing this cropping method over the last few days and things have been going well! However, I have encountered a minor issue. I have been drawing an image graphic to conduct the crop with but I have not been able to add a scrollpane to it. Though I checked out similar questions and answers, I've still struggled with finding an answer. Is there any chance someone could assist me?

This is the code for the window/crop buttons

    public class cropButton implements ActionListener { // listener for the crop method          public void actionPerformed(ActionEvent e) {              cropClassMain cropMethod = new cropClassMain(image);              smallFrame = new JFrame("Crop"); // seperate frame is created for cropping of the image              smallFrame.pack();              smallFrame.setSize(image.getWidth(), image.getHeight());                      smallFrame.setVisible(true);              smallFrame.setLayout(new BorderLayout());                            JScrollPane scrolly = new JScrollPane(cropMethod, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);              smallFrame.add(scrolly);//HERE IS WHERE THE JScrollPane SHOULD BE ADDED                            JPanel buttonPanel = new JPanel();              crop2 = new JButton("Crop");              escape = new JButton("Escape");              buttonPanel.add(crop2);              buttonPanel.add(escape);              smallFrame.add(buttonPanel, BorderLayout.SOUTH);              smallFrame.setVisible(true);              contentPane.setSize(990,990);                            crop2.addActionListener(new ActionListener() {                  public void actionPerformed(ActionEvent e) {                      contentPane.removeAll();                      image = cropMethod.croppedImage();                      imgLabel = new JLabel(new ImageIcon(image));                      contentPane.add(imgLabel);                      frame.setSize(1000, 999); // frame size adjusts to image size                      frame.setSize(1000, 1000);                      pane = new JScrollPane(imgLabel);                      pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);                      pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);                      SouthRegion();                      next.setEnabled(false);                      select.setEnabled(false);                      previous.setEnabled(false);                      contentPane.add(pane);                      if(grayscaleIsPressed = true)                          {                          grayscale.setEnabled(false);                          }                      smallFrame.dispose();                  }                         });                            escape.addActionListener(new ActionListener() {                  public void actionPerformed(ActionEvent e) {                      smallFrame.dispose();                  }                         });                }      }  

This is the code that creates the cropping rectangle and displays the graphic

public class cropClassMain extends JPanel {      JFrame frame, smallFrame;      JButton crop = new JButton();      JPanel contentPane = new JPanel();      public int x, y, w, h;      JLabel imgLabel;      JButton crop2, escape, grayscale, process, next, previous, select;      boolean isPDF;      ArrayList<BufferedImage> storageList = new ArrayList<BufferedImage>();      BufferedImage image, image2;      Image backgroundImage;      Icon icon2;      JLabel background;      Graphics graph;      private ButtonGroup processButtonGroup;      private static int FRAME_WIDTH = 615;      private static int FRAME_HEIGHT = 150;      JFileChooser fc = new JFileChooser();      Path path;      File draftDraw, outputLocation;      JPanel panel;      int imageAdder = 0;      String filePath, filePathOutput, fileName;      String[][] tagInfo = new String[500][500];      int place = 0;      String location;      String introAndPage;      int nextOrPrev, pgNumber = 0;      boolean wasClicked;      Rectangle cropRec;      Rectangle finalRect;      Rectangle imgRect;        public cropClassMain(BufferedImage image) {          this.image = image;          imgLabel = new JLabel(new ImageIcon(image));          MouseHandler mouse = new MouseHandler();          addMouseListener(mouse);          addMouseMotionListener(mouse);      }            public BufferedImage croppedImage() {          image2 = image.getSubimage(Math.abs(x), Math.abs(y), Math.abs(w), Math.abs(h));          return image2;      }            protected Rectangle getCropBounds() {          finalRect = null;          if (cropRec != null) {              x = cropRec.x;              y = cropRec.y;              w = cropRec.width;              h = cropRec.height;          }          finalRect = new Rectangle(x, y, w, h);          System.out.println(finalRect);          return finalRect;      }        protected void paintComponent(Graphics g) {          Rectangle drawCrop = getCropBounds();          super.paintComponent(g);          if (drawCrop != null) {              g.drawImage(image, 0, 0, null);              g.setColor(Color.red);              g.drawRect(x, y, w, h);          }      }        public class MouseHandler extends MouseAdapter {          public void mouseReleased(MouseEvent e) {              cropRec = null;              repaint();          }            public void mousePressed(MouseEvent e) {              cropRec = new Rectangle();              cropRec.setLocation(e.getPoint());              repaint();          }            public void mouseDragged(MouseEvent e) {              Point p = e.getPoint();              int width = p.x - cropRec.x;              int height = p.y - cropRec.y;              cropRec.setSize(width, height);              repaint();            }      }    }  

I just need to know how to add the JScrollPane to the SmallFrame frame. Thank you

https://stackoverflow.com/questions/65605730/adding-jscrollpane-to-a-drawing January 07, 2021 at 10:05AM

没有评论:

发表评论