Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.Toolkit;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollBar;
- import javax.swing.JScrollPane;
- import javax.swing.border.EmptyBorder;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- public class ImagePanel extends JPanel {
- private static final long serialVersionUID = 1L;
- private BufferedImage image;
- private JPanel canvas;
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- protected int x1, x2, y1, y2;
- private int state = 0;
- // state =1 -> draw image.
- // state =2 -> draw rectangle.
- private Graphics2D graphics = (Graphics2D) getGraphics();
- public ImagePanel() {
- }
- public void drawImage(BufferedImage img) {
- image = img;
- state = 1;
- System.out.println("11111111111");
- this.canvas = new JPanel() {
- private static final long serialVersionUID = 1L;
- // @Override
- public void paint(Graphics g) {
- System.out.println("22222222222");
- super.paintComponent(g);
- graphics = (Graphics2D) g;
- if (state > 0) {
- // state>0 -> image is not equal null.
- int x = screenSize.width - 5 - image.getWidth();
- int y = screenSize.height - 180 - image.getHeight();
- x /= 2;
- y /= 2;
- x = Math.max(x, 0);
- y = Math.max(y, 0);
- g.drawImage(image, x, y, null);
- }
- if (state == 2) {
- // cropping.
- g.drawRect(Math.min(x1, x2), Math.min(y1, y2),
- Math.abs(x2 - x1), Math.abs(y2 - y1));
- }
- }
- };
- canvas.setPreferredSize(new Dimension(image.getWidth(), image
- .getHeight()));
- JScrollPane sp = new JScrollPane(canvas);
- setLayout(new BorderLayout());
- add(sp, BorderLayout.CENTER);
- repaint();
- }
- public void drawRect(int x1, int y1, int x2, int y2) {
- state = 2;
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- repaint();
- }
- // @Override
- public void paint(Graphics g) {
- System.out.println("painttt");
- super.paintComponent(g);
- graphics = (Graphics2D) g;
- if (state > 0) {
- // state>0 -> image is not equal null.
- int x = screenSize.width - 5 - image.getWidth();
- int y = screenSize.height - 180 - image.getHeight();
- x /= 2;
- y /= 2;
- x = Math.max(x, 0);
- y = Math.max(y, 0);
- g.drawImage(image, x, y, null);
- }
- if (state == 2) {
- // cropping.
- g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
- Math.abs(y2 - y1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement