<commit_before>package edu.mit.csail.uid;

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.imageio.*;

class CapturePrompt extends JWindow implements Subject{
   static Color _overlayColor = new Color(0F,0F,0F,0.6F);
   final static float MIN_DARKER_FACTOR = 0.6f;
   final static long MSG_DISPLAY_TIME = 2000;
   static GraphicsDevice _gdev = null;

   Observer _obs;

   Screen _scr;
   BufferedImage _scr_img = null;
   BufferedImage _darker_screen = null;
   float _darker_factor;
   Rectangle rectSelection;
   BasicStroke bs;
   int srcScreenId=0;
   int srcx, srcy, destx, desty;
   boolean _canceled = false;
   long _msg_start;
   String _msg;

   BasicStroke _StrokeCross = new BasicStroke (1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1, new float [] { 2f }, 0);

   public void addObserver(Observer o){
      _obs = o;
   }
   public void notifyObserver(){
      _obs.update(this);
   }

   private void captureScreen(Screen scr) {
      ScreenImage simg = scr.capture();
      _scr_img = simg.getImage();

      _darker_factor = 0.6f;
      RescaleOp op = new RescaleOp(_darker_factor, 0, null);
      _darker_screen = op.filter(_scr_img, null);

   }

   private Color selFrameColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
   private Color selCrossColor = new Color(1.0f, 0.0f, 0.0f, 0.6f);
   private Color screenFrameColor = new Color(1.0f, 0.0f, 0.0f, 0.6f);
   BasicStroke strokeScreenFrame = new BasicStroke(5);

   private void drawScreenFrame(Graphics2D g2d, int scrId){
      Rectangle rect = Screen.getBounds(scrId);
      Rectangle ubound = (new UnionScreen()).getBounds();
      g2d.setColor(screenFrameColor);
      g2d.setStroke(strokeScreenFrame);
      rect.x -= ubound.x;
      rect.y -= ubound.y;
      int sw = (int)(strokeScreenFrame.getLineWidth()/2);
      rect.x += sw;
      rect.y += sw;
      rect.width -= sw*2; 
      rect.height -= sw*2;
      g2d.draw(rect);
   }

   private void drawSelection(Graphics2D g2d){
      if (srcx != destx || srcy != desty)
      {
         int x1 = (srcx < destx) ? srcx : destx;
         int y1 = (srcy < desty) ? srcy : desty;
         int x2 = (srcx > destx) ? srcx : destx;
         int y2 = (srcy > desty) ? srcy : desty;

         if(Screen.getNumberScreens()>1){
            Rectangle selRect = new Rectangle(x1,y1,x2-x1,y2-y1);
            Rectangle ubound = (new UnionScreen()).getBounds();
            selRect.x += ubound.x;
            selRect.y += ubound.y;
            Rectangle inBound = selRect.intersection(Screen.getBounds(srcScreenId));
            x1 = inBound.x - ubound.x;
            y1 = inBound.y - ubound.y;
            x2 = x1 + inBound.width-1;
            y2 = y1 + inBound.height-1;
         }

         rectSelection.x = x1;
         rectSelection.y = y1;
         rectSelection.width = (x2-x1)+1;
         rectSelection.height = (y2-y1)+1;

         if(rectSelection.width>0 && rectSelection.height>0)
            g2d.drawImage(_scr_img.getSubimage(x1, y1,x2-x1+1, y2-y1+1),
                          null, x1, y1);

         g2d.setColor(selFrameColor);
         g2d.setStroke(bs);
         g2d.draw(rectSelection);

         int cx = (x1+x2)/2;
         int cy = (y1+y2)/2;
         g2d.setColor(selCrossColor);
         g2d.setStroke(_StrokeCross);
         g2d.drawLine(cx, y1, cx, y2);
         g2d.drawLine(x1, cy, x2, cy);

         if(Screen.getNumberScreens()>1)
            drawScreenFrame(g2d, srcScreenId);
      }
   }

   static Font fontMsg = new Font("Arial", Font.PLAIN, 40);
   void drawMessage(Graphics2D g2d){
      if(_msg == null)
         return;
      if(_msg_start == -1) _msg_start = (new Date()).getTime();
      long now = (new Date()).getTime();
      if(now - _msg_start <= MSG_DISPLAY_TIME){
         float alpha = 1f - (float)(now-_msg_start)/MSG_DISPLAY_TIME;
         g2d.setFont(fontMsg);
         g2d.setColor(new Color(1f,1f,1f,alpha));
         g2d.drawString(_msg, 0, 45);
         repaint();
      }


   }

   BufferedImage bi = null;
   public void paint(Graphics g)
   {
      if( _scr_img != null ){
         Graphics2D g2dWin = (Graphics2D)g;

         if ( bi==null || bi.getWidth(this) != getWidth() ||
              bi.getHeight(this) != getHeight() ) {
            bi = new BufferedImage( 
                  getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB );
         }
         Graphics2D bfG2 = bi.createGraphics();
         bfG2.drawImage(_darker_screen,0,0,this);
         drawMessage(bfG2);
         drawSelection(bfG2);
         g2dWin.drawImage(bi, 0, 0, this);
         setVisible(true);
      }
      else
         setVisible(false);
   }
<commit_msg>center the message in capture prompt <commit_after>package edu.mit.csail.uid;

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.imageio.*;

class CapturePrompt extends JWindow implements Subject{
   static Color _overlayColor = new Color(0F,0F,0F,0.6F);
   final static float MIN_DARKER_FACTOR = 0.6f;
   final static long MSG_DISPLAY_TIME = 2000;
   static GraphicsDevice _gdev = null;

   Observer _obs;

   Screen _scr;
   BufferedImage _scr_img = null;
   BufferedImage _darker_screen = null;
   float _darker_factor;
   Rectangle rectSelection;
   BasicStroke bs;
   int srcScreenId=0;
   int srcx, srcy, destx, desty;
   boolean _canceled = false;
   long _msg_start;
   String _msg;

   BasicStroke _StrokeCross = new BasicStroke (1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1, new float [] { 2f }, 0);

   public void addObserver(Observer o){
      _obs = o;
   }
   public void notifyObserver(){
      _obs.update(this);
   }

   private void captureScreen(Screen scr) {
      ScreenImage simg = scr.capture();
      _scr_img = simg.getImage();

      _darker_factor = 0.6f;
      RescaleOp op = new RescaleOp(_darker_factor, 0, null);
      _darker_screen = op.filter(_scr_img, null);

   }

   private Color selFrameColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
   private Color selCrossColor = new Color(1.0f, 0.0f, 0.0f, 0.6f);
   private Color screenFrameColor = new Color(1.0f, 0.0f, 0.0f, 0.6f);
   BasicStroke strokeScreenFrame = new BasicStroke(5);

   private void drawScreenFrame(Graphics2D g2d, int scrId){
      Rectangle rect = Screen.getBounds(scrId);
      Rectangle ubound = (new UnionScreen()).getBounds();
      g2d.setColor(screenFrameColor);
      g2d.setStroke(strokeScreenFrame);
      rect.x -= ubound.x;
      rect.y -= ubound.y;
      int sw = (int)(strokeScreenFrame.getLineWidth()/2);
      rect.x += sw;
      rect.y += sw;
      rect.width -= sw*2; 
      rect.height -= sw*2;
      g2d.draw(rect);
   }

   private void drawSelection(Graphics2D g2d){
      if (srcx != destx || srcy != desty)
      {
         int x1 = (srcx < destx) ? srcx : destx;
         int y1 = (srcy < desty) ? srcy : desty;
         int x2 = (srcx > destx) ? srcx : destx;
         int y2 = (srcy > desty) ? srcy : desty;

         if(Screen.getNumberScreens()>1){
            Rectangle selRect = new Rectangle(x1,y1,x2-x1,y2-y1);
            Rectangle ubound = (new UnionScreen()).getBounds();
            selRect.x += ubound.x;
            selRect.y += ubound.y;
            Rectangle inBound = selRect.intersection(Screen.getBounds(srcScreenId));
            x1 = inBound.x - ubound.x;
            y1 = inBound.y - ubound.y;
            x2 = x1 + inBound.width-1;
            y2 = y1 + inBound.height-1;
         }

         rectSelection.x = x1;
         rectSelection.y = y1;
         rectSelection.width = (x2-x1)+1;
         rectSelection.height = (y2-y1)+1;

         if(rectSelection.width>0 && rectSelection.height>0)
            g2d.drawImage(_scr_img.getSubimage(x1, y1,x2-x1+1, y2-y1+1),
                          null, x1, y1);

         g2d.setColor(selFrameColor);
         g2d.setStroke(bs);
         g2d.draw(rectSelection);

         int cx = (x1+x2)/2;
         int cy = (y1+y2)/2;
         g2d.setColor(selCrossColor);
         g2d.setStroke(_StrokeCross);
         g2d.drawLine(cx, y1, cx, y2);
         g2d.drawLine(x1, cy, x2, cy);

         if(Screen.getNumberScreens()>1)
            drawScreenFrame(g2d, srcScreenId);
      }
   }

   static Font fontMsg = new Font("Arial", Font.PLAIN, 60);
   void drawMessage(Graphics2D g2d){
      if(_msg == null)
         return;
      if(_msg_start == -1) _msg_start = (new Date()).getTime();
      long now = (new Date()).getTime();
      if(now - _msg_start <= MSG_DISPLAY_TIME){
         float alpha = 1f - (float)(now-_msg_start)/MSG_DISPLAY_TIME;
         g2d.setFont(fontMsg);
         g2d.setColor(new Color(1f,1f,1f,alpha));
         int sw = g2d.getFontMetrics().stringWidth(_msg);
         int sh = g2d.getFontMetrics().getMaxAscent();
         Rectangle ubound = (new UnionScreen()).getBounds();
         for(int i=0;i<Screen.getNumberScreens();i++){
            Rectangle bound = Screen.getBounds(i);
            int cx = bound.x+ (bound.width-sw)/2 - ubound.x;
            int cy = bound.y+ (bound.height-sh)/2 - ubound.y;
            g2d.drawString(_msg, cx, cy);
         }
         repaint();
      }


   }

   BufferedImage bi = null;
   public void paint(Graphics g)
   {
      if( _scr_img != null ){
         Graphics2D g2dWin = (Graphics2D)g;

         if ( bi==null || bi.getWidth(this) != getWidth() ||
              bi.getHeight(this) != getHeight() ) {
            bi = new BufferedImage( 
                  getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB );
         }
         Graphics2D bfG2 = bi.createGraphics();
         bfG2.drawImage(_darker_screen,0,0,this);
         drawMessage(bfG2);
         drawSelection(bfG2);
         g2dWin.drawImage(bi, 0, 0, this);
         setVisible(true);
      }
      else
         setVisible(false);
   }

{'fraction_non_alphanumeric': 0.12869582827055487, 'fraction_numerical': 0.02875658161198866, 'mean_word_length': 2.3011363636363638, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3750104', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3472, 'n_words': 721}