Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.RenderingHints;
  8. import java.awt.Toolkit;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JFileChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.JScrollBar;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.border.EmptyBorder;
  18. import javax.swing.filechooser.FileNameExtensionFilter;
  19. import javax.swing.JButton;
  20.  
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.MouseEvent;
  24. import java.awt.image.BufferedImage;
  25. import java.io.File;
  26. import java.io.IOException;
  27.  
  28. public class ImagePanel extends JPanel {
  29. private static final long serialVersionUID = 1L;
  30. private BufferedImage image;
  31. private JPanel canvas;
  32. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  33. protected int x1, x2, y1, y2;
  34. private int state = 0;
  35. // state =1 -> draw image.
  36. // state =2 -> draw rectangle.
  37.  
  38. private Graphics2D graphics = (Graphics2D) getGraphics();
  39.  
  40. public ImagePanel() {
  41.  
  42. }
  43.  
  44. public void drawImage(BufferedImage img) {
  45. image = img;
  46. state = 1;
  47. System.out.println("11111111111");
  48. this.canvas = new JPanel() {
  49. private static final long serialVersionUID = 1L;
  50.  
  51. // @Override
  52. public void paint(Graphics g) {
  53. System.out.println("22222222222");
  54.  
  55. super.paintComponent(g);
  56. graphics = (Graphics2D) g;
  57. if (state > 0) {
  58. // state>0 -> image is not equal null.
  59. int x = screenSize.width - 5 - image.getWidth();
  60. int y = screenSize.height - 180 - image.getHeight();
  61.  
  62. x /= 2;
  63. y /= 2;
  64.  
  65. x = Math.max(x, 0);
  66. y = Math.max(y, 0);
  67. g.drawImage(image, x, y, null);
  68. }
  69. if (state == 2) {
  70. // cropping.
  71. g.drawRect(Math.min(x1, x2), Math.min(y1, y2),
  72. Math.abs(x2 - x1), Math.abs(y2 - y1));
  73. }
  74. }
  75. };
  76. canvas.setPreferredSize(new Dimension(image.getWidth(), image
  77. .getHeight()));
  78. JScrollPane sp = new JScrollPane(canvas);
  79. setLayout(new BorderLayout());
  80. add(sp, BorderLayout.CENTER);
  81. repaint();
  82. }
  83.  
  84. public void drawRect(int x1, int y1, int x2, int y2) {
  85. state = 2;
  86. this.x1 = x1;
  87. this.y1 = y1;
  88. this.x2 = x2;
  89. this.y2 = y2;
  90. repaint();
  91. }
  92.  
  93. // @Override
  94. public void paint(Graphics g) {
  95. System.out.println("painttt");
  96.  
  97. super.paintComponent(g);
  98. graphics = (Graphics2D) g;
  99. if (state > 0) {
  100. // state>0 -> image is not equal null.
  101. int x = screenSize.width - 5 - image.getWidth();
  102. int y = screenSize.height - 180 - image.getHeight();
  103.  
  104. x /= 2;
  105. y /= 2;
  106.  
  107. x = Math.max(x, 0);
  108. y = Math.max(y, 0);
  109. g.drawImage(image, x, y, null);
  110. }
  111. if (state == 2) {
  112. // cropping.
  113. g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
  114. Math.abs(y2 - y1));
  115. }
  116.  
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement