SHARE
TWEET

Untitled

a guest May 12th, 2014 2 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Rectangle;
  5. import java.awt.TexturePaint;
  6. import java.awt.image.BufferedImage;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import javax.swing.JPanel;
  10.  
  11. public class DrawPanel extends JPanel
  12. {
  13.         private DrawPanelListener dpl;
  14.         private List<Shape> shapes = new ArrayList<>();
  15.         private Shape tmpShape;
  16.  
  17.         public DrawPanel()
  18.         {
  19.                 setOpaque(true);
  20.                 setBackground(Color.WHITE);
  21.  
  22.                 dpl = new DrawPanelListener(this);
  23.                 addMouseListener(dpl);
  24.                 addMouseMotionListener(dpl);
  25.         }
  26.  
  27.         public void paintComponent(Graphics g)
  28.         {
  29.                 super.paintComponent(g);
  30.  
  31.                 for (Shape s : shapes)
  32.                 {
  33.                         if (s != null)
  34.                                 s.render(g);
  35.                 }
  36.                 if (tmpShape != null)
  37.                         tmpShape.render(g);
  38.         }
  39.  
  40.         // here i want to add image and i use this method in MainMenu class when i open image from file
  41.         public void addImage(BufferedImage img)
  42.         {
  43.                 Graphics2D g = (Graphics2D) img.getGraphics();
  44.                 g.setPaint(new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight())));
  45.                 g.fillRect(0, 0, getWidth(), getHeight());
  46.                 repaint();
  47.                 validate();
  48.         }
  49.  
  50.         public List<Shape> getShapes()
  51.         {
  52.                 return shapes;
  53.         }
  54.  
  55.         public void setShapes(List<Shape> shapes)
  56.         {
  57.                 this.shapes = shapes;
  58.         }
  59.  
  60.         public Shape getTmpShape()
  61.         {
  62.                 return tmpShape;
  63.         }
  64.  
  65.         public void setTmpShape(Shape tmpShape)
  66.         {
  67.                 this.tmpShape = tmpShape;
  68.         }
  69.  
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top