Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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);
- }
- // here i want to add image and i use this method in MainMenu class when i open image from file
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement