Advertisement
Mark2020H

clip images with Java

Jun 10th, 2023
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. /* MD Harrington  London  UK */
  2.  
  3.  
  4.  
  5. import com.sun.imageio.plugins.common.ImageUtil;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.EventQueue;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.Image;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.geom.Ellipse2D;
  15. import java.io.IOException;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.swing.JFrame;
  19. import javax.swing.JPanel;
  20. import javax.swing.Timer;
  21. class Surface extends JPanel implements ActionListener {
  22.  
  23.     private int pos_x = 8;
  24.     private int pos_y = 8;
  25.     private final int RADIUS = 200;
  26.     private final int DELAY = 10;
  27.     private Timer timer;
  28.     private Image image;
  29.     private final double delta[] = { 3, 3 };
  30.  
  31.     public Surface() {
  32.         setDoubleBuffered(true);
  33.         this.setBackground(Color.black);
  34.         loadImage();
  35.         determineAndSetImageSize();
  36.         initTimer();
  37.     }
  38.  
  39.     private void loadImage() {
  40.         try {
  41.             image = javax.imageio.ImageIO.read(Surface.class.getClassLoader().getResourceAsStream("images/storm.jpg"));
  42.         } catch (IOException ex) {
  43.             Logger.getLogger(Surface.class.getName()).log(Level.SEVERE, null, ex);
  44.         }
  45.     }
  46.  
  47.     private void determineAndSetImageSize() {
  48.         int h = image.getHeight(this);
  49.         System.out.println("Height = " +h);
  50.         int w = image.getWidth(this);
  51.         setPreferredSize(new Dimension(w, h));        
  52.     }
  53.    
  54.     private void initTimer() {  
  55.         timer = new Timer(DELAY, this);
  56.         timer.start();
  57.     }
  58.  
  59.     private void doDrawing(Graphics g) {
  60.         Graphics2D g2d = (Graphics2D) g.create();
  61.         g2d.clip(new Ellipse2D.Double(pos_x, pos_y, RADIUS, RADIUS));
  62.         g2d.drawImage(image, 0, 0, null);
  63.         g2d.dispose();
  64.     }
  65.  
  66.     @Override
  67.     public void paintComponent(Graphics g) {
  68.         super.paintComponent(g);
  69.         doDrawing(g);
  70.     }
  71.  
  72.     @Override
  73.     public void actionPerformed(ActionEvent e) {
  74.         moveCircle();
  75.         repaint();
  76.     }
  77.  
  78.     private void moveCircle() {
  79.         int w = getWidth();
  80.         int h = getHeight();
  81.         if (pos_x < 0) {
  82.             delta[0] = Math.random() % 4 + 2;
  83.         } else if (pos_x > w - RADIUS) {
  84.             delta[0] = -(Math.random() % 4 + 2);
  85.         }
  86.         if (pos_y < 0 ) {
  87.             delta[1] = Math.random() % 4 + 2;
  88.  
  89.         } else if (pos_y > h - RADIUS) {
  90.             delta[1] = -(Math.random() % 4 + 2);
  91.         }
  92.  
  93.         pos_x += delta[0];
  94.         pos_y += delta[1];
  95.     }    
  96.  
  97. }
  98. public class ClippingEx extends JFrame
  99.  {
  100.     public ClippingEx() {
  101.         initUI();
  102.     }
  103.  
  104.     private void initUI() {
  105.         setTitle("Clipping");
  106.         setBackground(Color.black);
  107.         add(new Surface());
  108.         pack();
  109.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  110.         setLocationRelativeTo(null);        
  111.     }
  112.  
  113.     public static void main(String[] args) {
  114.         EventQueue.invokeLater(new Runnable() {
  115.             @Override
  116.             public void run() {
  117.                 ClippingEx cl = new ClippingEx();
  118.                 cl.setVisible(true);
  119.             }
  120.         });        
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement