import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.BorderLayout; import java.util.Observer; import javax.swing.JFrame; import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Color; import java.awt.Rectangle; import java.awt.Font; import java.awt.Component; import java.awt.geom.Point2D; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.awt.geom.Ellipse2D; import java.awt.font.TextLayout; public class MoveGlass { private static JFrame window; // The application window private static Board view; // The view of the sketch private static Glass front; static MouseHandler mouseHandler; static double dx = 1.0, dy = 1.0; // increments for change in location of rect static boolean moveIt = false; static Point2D.Double corner = new Point2D.Double(50.0,50.0); //corner of rect static Rectangle2D.Double rect = new Rectangle2D.Double(corner.getX(),corner.getY(),140,100); public static void main(String[] args) { window = new JFrame("MoveGlass"); // Create the app window window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Default is exit the application Toolkit theKit = window.getToolkit(); // Get the window toolkit Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size window.setBounds(wndSize.width/4, wndSize.height/4, // Position wndSize.width/2, wndSize.height/2); // Size view = new Board(); // Create the view front = new Glass(); mouseHandler = new MouseHandler(); front.addMouseListener(mouseHandler); window.getContentPane().add(view, BorderLayout.CENTER); window.setGlassPane(front); window.setVisible(true); window.getGlassPane().setVisible(true); // this loop redraws the glassPane while (corner.getX() < 200.0) { try{Thread.sleep(25);}catch(InterruptedException exc){} if(moveIt){ corner.setLocation(corner.getX() + dx, corner.getY() + dy); rect = new Rectangle2D.Double(corner.getX(),corner.getY(),140,100); front.repaint(); } } } static class Board extends JComponent{ public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; // Get a 2D device context // g2D.setPaint(color); // g2D.fill(rect); // g2D.draw(rect); g2D.setPaint(Color.CYAN); g2D.setFont(new Font("Arial", Font.PLAIN, 18)); g2D.draw(new Line2D.Float(240.0f, 120.0f, 80.0f, 200.0f)); g2D.draw(new Ellipse2D.Double(220.0,100.0,40.0,40.0)); g2D.setPaint(Color.RED); g2D.drawString("Thanksgiving",70,100); } } static class Glass extends JComponent{ public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; // Get a 2D device context g2D.setPaint(Color.GREEN); g2D.fill(rect); g2D.draw(rect); } private Graphics2D g2D = null; // Temporary graphics context } static class MouseHandler extends MouseAdapter { // change increments to start rect moving public void mousePressed(MouseEvent e) { if (rect.contains(e.getX(), e.getY())) moveIt = true; } } }