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);
}
});
}
}