Advertisement
Guest User

Untitled

a guest
May 12th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. package be.wout.draw;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Rectangle;
  7. import java.awt.TexturePaint;
  8. import java.awt.image.BufferedImage;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import javax.swing.JPanel;
  13.  
  14. import be.wout.draw.shapes.Shape;
  15. import be.wout.listeners.DrawPanelListener;
  16.  
  17. public class DrawPanel extends JPanel
  18. {
  19. private DrawPanelListener dpl;
  20. private List<Shape> shapes = new ArrayList<>();
  21. private Shape tmpShape;
  22.  
  23. public DrawPanel()
  24. {
  25. setOpaque(true);
  26. setBackground(Color.WHITE);
  27.  
  28. dpl = new DrawPanelListener(this);
  29. addMouseListener(dpl);
  30. addMouseMotionListener(dpl);
  31. }
  32.  
  33. public void paintComponent(Graphics g)
  34. {
  35. super.paintComponent(g);
  36.  
  37. for (Shape s : shapes)
  38. {
  39. if (s != null)
  40. s.render(g);
  41. }
  42. if (tmpShape != null)
  43. tmpShape.render(g);
  44. }
  45.  
  46. public void addImage(BufferedImage img)
  47. {
  48. Graphics2D g = (Graphics2D) img.getGraphics();
  49. g.setPaint(new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight())));
  50. g.fillRect(0, 0, getWidth(), getHeight());
  51.  
  52. repaint();
  53. validate();
  54. }
  55.  
  56. public List<Shape> getShapes()
  57. {
  58. return shapes;
  59. }
  60.  
  61. public void setShapes(List<Shape> shapes)
  62. {
  63. this.shapes = shapes;
  64. }
  65.  
  66. public Shape getTmpShape()
  67. {
  68. return tmpShape;
  69. }
  70.  
  71. public void setTmpShape(Shape tmpShape)
  72. {
  73. this.tmpShape = tmpShape;
  74. }
  75.  
  76. public void deleteLastElement()
  77. {
  78. int lastItem = shapes.size() - 1;
  79. if (!shapes.isEmpty())
  80. {
  81. shapes.remove(lastItem);
  82. }
  83. }
  84.  
  85. public Shape getLastElement()
  86. {
  87. int lastItem = shapes.size() - 1;
  88. if (!shapes.isEmpty())
  89. {
  90. return shapes.get(lastItem);
  91. }
  92. else
  93. {
  94. return null;
  95. }
  96. }
  97.  
  98. public void redo(Shape deletedShape)
  99. {
  100. shapes.add(deletedShape);
  101. }
  102.  
  103. public void clear()
  104. {
  105. shapes.clear();
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement