ho questo JPanel che prende come sfondo una immagine e sul quale posso fare una selezione col mouse:
funziona però manca la visualizzazione dell'area selezionata.codice:public class DrawPanel extends JFrame { private JButton button; private JScrollPane scroll; private int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); private int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); public DrawPanel(String path) { super("Draw panel"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(new BorderLayout()); button = new JButton("Capture"); ImagePanel panel = new ImagePanel(path); scroll = new JScrollPane(panel); add(scroll, BorderLayout.CENTER); add(button, BorderLayout.SOUTH); } private class ImagePanel extends JPanel implements MouseListener, MouseMotionListener { private BufferedImage buff; private Rectangle currentRect; private String fileDelete; private Graphics g; private int xx = 0; private int yy = 0; private int w = 0; private int h = 0; public ImagePanel(String path) { setLayout(new BorderLayout()); setPreferredSize(new Dimension(width, height)); fileDelete = path; try { buff = ImageIO.read(new File(path)); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { capture(e); } }); addMouseListener(this); addMouseMotionListener(this); } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(buff, 0, 0, this); } public void mousePressed(MouseEvent e) { xx = e.getX(); yy = e.getY(); currentRect = new Rectangle(xx, yy, 0, 0); } public void mouseDragged(MouseEvent e) { currentRect.setSize(e.getX() - currentRect.x, e.getY() - currentRect.y); repaint(); } public void mouseReleased(MouseEvent e) { w = e.getX() - currentRect.x; h = e.getY() - currentRect.y; currentRect.setSize(w, h); repaint(); } public void capture(ActionEvent e) { try { FileChooserPanel.salva(xx, yy, w, h); DeleteTemp.delete(fileDelete); } catch (AWTException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } //metodi che nn servono public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } } }
come posso fare per far vedere l'area selezionata??

Rispondi quotando
