Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.wout.draw;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.TexturePaint;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JPanel;
- import be.wout.draw.shapes.Shape;
- import be.wout.listeners.DrawPanelListener;
- public class DrawPanel extends JPanel
- {
- private DrawPanelListener dpl;
- private List<Shape> shapes = new ArrayList<>();
- private Shape tmpShape;
- public DrawPanel()
- {
- setOpaque(true);
- setBackground(Color.WHITE);
- dpl = new DrawPanelListener(this);
- addMouseListener(dpl);
- addMouseMotionListener(dpl);
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- for (Shape s : shapes)
- {
- if (s != null)
- s.render(g);
- }
- if (tmpShape != null)
- tmpShape.render(g);
- }
- public void addImage(BufferedImage img)
- {
- Graphics2D g = (Graphics2D) img.getGraphics();
- g.setPaint(new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight())));
- g.fillRect(0, 0, getWidth(), getHeight());
- repaint();
- validate();
- }
- public List<Shape> getShapes()
- {
- return shapes;
- }
- public void setShapes(List<Shape> shapes)
- {
- this.shapes = shapes;
- }
- public Shape getTmpShape()
- {
- return tmpShape;
- }
- public void setTmpShape(Shape tmpShape)
- {
- this.tmpShape = tmpShape;
- }
- public void deleteLastElement()
- {
- int lastItem = shapes.size() - 1;
- if (!shapes.isEmpty())
- {
- shapes.remove(lastItem);
- }
- }
- public Shape getLastElement()
- {
- int lastItem = shapes.size() - 1;
- if (!shapes.isEmpty())
- {
- return shapes.get(lastItem);
- }
- else
- {
- return null;
- }
- }
- public void redo(Shape deletedShape)
- {
- shapes.add(deletedShape);
- }
- public void clear()
- {
- shapes.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement