0

So I have several problems: 1) Is this code OK ? Or can it be written better ? (I will have an array with pictures in the final version) 2) When I click Next button the rectangle that I drew on first picture stays on the second one, how to clear it ? So after pressing Next button there is no rectangle on the new picture ? 3) I want to be able to auto-scroll while I drag the mouse button (while drawing the rectangle), but it's not really working... Please help

 public class SelectionExample {

     static public TestPane panelek;
     static public BufferedImage tempimage;



    public static void main(String[] args) {
        new SelectionExample();
    }

    public SelectionExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setLayout(new BorderLayout());
                panelek = new TestPane();
               panelek.setPreferredSize(new Dimension(100, 100));
                panelek.setAutoscrolls(true);
                JScrollPane scrollPane = new JScrollPane(panelek);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);


                frame.add(scrollPane, BorderLayout.CENTER);
                JButton next = new JButton("NEXT");
                frame.add(next, BorderLayout.PAGE_START);


                try {
                    tempimage = ImageIO.read(new File("D:/test/temp1.jpg"));
                } catch (IOException ex) {
                    Logger.getLogger(SelectionExample.class.getName()).log(Level.SEVERE, null, ex);
                }


                next.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                 try {
                    tempimage = ImageIO.read(new File("D:/test/temp2.jpg"));
                } catch (IOException ex) {
                    Logger.getLogger(SelectionExample.class.getName()).log(Level.SEVERE, null, ex);
                }
           panelek.repaint();


            }
        });      



                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);



            }
        });
    }










}
Pirate
  • 77
  • 1
  • 9
  • The rectangle is drawn by your `paintComponent` based on the values of `selection`, so maybe you need away to clear/reset those values when you move to the next image – MadProgrammer Mar 07 '16 at 00:00
  • Dragging `JScrollPane` [example](http://stackoverflow.com/questions/31171502/scroll-jscrollpane-by-dragging-mouse-java-swing/31173371#31173371) – MadProgrammer Mar 07 '16 at 00:01
  • Hi, so in action listener of next button I should set all values of selection to 0 ? I have just checked this code with JscrollPane and well it didnt help.... I am able to move around the picture while I darg the mouse the problem is I can't draw rectangle and move around picture at the same time.... – Pirate Mar 07 '16 at 00:12
  • Okay, so how did you think you might make it work? How would you tell the `JScrollPane` from not "dragging" when you want to draw a selection rectangle? Or did you want to do both? As to the the rectangle clearing, I'd have a method in your panel, probably called `clearSelection` which did this – MadProgrammer Mar 07 '16 at 00:15
  • what I want is: if the picture is too big to be displayed on panel I want to have scrolls - I have it, but when I start to draw the rectangle and Ill go to lets say right side (end of current view) but I am still dragging I want to have autoscroll to the right side. So the current view (part of the pictures) moves I hope thta You understand – Pirate Mar 07 '16 at 00:24
  • You want to scroll based when you hit the boundaries of the viewable area? – MadProgrammer Mar 07 '16 at 00:28
  • Or lets say I have a picture with 4 starts in a row, blue, red, green and black, First I can see only 2 starts - blue and red, but when i click just after the red star and start dragging right the current view changes and i cant see blue and red stars but I can see green and black(at the same time when I drag I want to draw this rectangle) – Pirate Mar 07 '16 at 00:29
  • Yeach, I want to scroll ONLY when I draw a rectangle and I hit the boundaries of the viewable area. – Pirate Mar 07 '16 at 01:10

1 Answers1

1

Is this code OK ? Or can it be written better ?

Don't use static variables. If you want to change the image on your panel then you need to create a setImage(...) method in your class. The method will then save the image and invoke repaint(). That is classes should be responsible for managing their properties and provide getter/setter methods.

The formatting of your code is terrible and therefore difficult to read. Use either tabs or spaces for code indentation and be consistent.

I want to be able to auto-scroll while I drag the mouse button

Read the API for the setAutoScrolls(...) method of the JComponent class. It provides code for the mouseDragged(...) method of your MouseMotionLister.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hi, thnx for help I will try to implement it all in TestPane class so there wont be any static variables. About this JscrollPane - it workes, I am able to drag the mouse and move around pictue but I can't draw rectangle and move around picture at the same time (if the picture is too big to be displayed on panel). – Pirate Mar 07 '16 at 00:14