2012-06-12 9 views
6

Realizzo un'applicazione per screenshot basata su Java e voglio farlo in modo che quando si preme una combinazione di tasti sulla tastiera qualcosa come this video accade dove si seleziona e area sul tuo schermo, e richiede una schermata dell'area selezionata.Selezionare un'area da catturare usando il mouse

Come selezionare un'area da catturare usando il mouse?

+0

Ho appena finito di aggiungere il cursore personalizzato. Non ho idea di come iniziare a renderlo in grado di selezionare lo schermo. –

+0

Che cosa hai fatto nei tuoi sforzi per risolvere questo problema, oltre a pubblicare questa domanda? – Jeffrey

+1

Ho cercato per almeno 3 giorni su qualche tipo di suggerimento su come fare questo. Non ne ho trovato uno, quindi ho deciso di postare questa domanda. –

risposta

16

Inizia con qualcosa di simile.

Screen Capture Rectangle

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import javax.swing.*; 

/** Getting a Rectangle of interest on the screen. 
Requires the MotivatedEndUser API - sold separately. */ 
public class ScreenCaptureRectangle { 

    Rectangle captureRect; 

    ScreenCaptureRectangle(final BufferedImage screen) { 
     final BufferedImage screenCopy = new BufferedImage(
       screen.getWidth(), 
       screen.getHeight(), 
       screen.getType()); 
     final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy)); 
     JScrollPane screenScroll = new JScrollPane(screenLabel); 

     screenScroll.setPreferredSize(new Dimension(
       (int)(screen.getWidth()/3), 
       (int)(screen.getHeight()/3))); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(screenScroll, BorderLayout.CENTER); 

     final JLabel selectionLabel = new JLabel(
       "Drag a rectangle in the screen shot!"); 
     panel.add(selectionLabel, BorderLayout.SOUTH); 

     repaint(screen, screenCopy); 
     screenLabel.repaint(); 

     screenLabel.addMouseMotionListener(new MouseMotionAdapter() { 

      Point start = new Point(); 

      @Override 
      public void mouseMoved(MouseEvent me) { 
       start = me.getPoint(); 
       repaint(screen, screenCopy); 
       selectionLabel.setText("Start Point: " + start); 
       screenLabel.repaint(); 
      } 

      @Override 
      public void mouseDragged(MouseEvent me) { 
       Point end = me.getPoint(); 
       captureRect = new Rectangle(start, 
         new Dimension(end.x-start.x, end.y-start.y)); 
       repaint(screen, screenCopy); 
       screenLabel.repaint(); 
       selectionLabel.setText("Rectangle: " + captureRect); 
      } 
     }); 

     JOptionPane.showMessageDialog(null, panel); 

     System.out.println("Rectangle of interest: " + captureRect); 
    } 

    public void repaint(BufferedImage orig, BufferedImage copy) { 
     Graphics2D g = copy.createGraphics(); 
     g.drawImage(orig,0,0, null); 
     if (captureRect!=null) { 
      g.setColor(Color.RED); 
      g.draw(captureRect); 
      g.setColor(new Color(255,255,255,150)); 
      g.fill(captureRect); 
     } 
     g.dispose(); 
    } 

    public static void main(String[] args) throws Exception { 
     Robot robot = new Robot(); 
     final Dimension screenSize = Toolkit.getDefaultToolkit(). 
       getScreenSize(); 
     final BufferedImage screen = robot.createScreenCapture(
       new Rectangle(screenSize)); 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new ScreenCaptureRectangle(screen); 
      } 
     }); 
    } 
} 
+0

Wow, grazie mille amico! Così utile Mi stavo solo chiedendo l'hai fatto? –

+0

Va bene, l'ho accettato. Grazie ancora amico. –

+0

Funzionerà solo se si acquisisce uno screenshot dell'applicazione Java in esecuzione. Non sarai in grado di eseguire lo screening delle altre applicazioni in esecuzione sul desktop dell'utente. Pensavo che volessi un'app per la cattura dello schermo per uso generico, il che significa che non puoi usare Swing. – chubbsondubs

Problemi correlati