Advertisement
Guest User

Paint

a guest
Feb 8th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Main;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         new MainFrame();
  7.  
  8.     }
  9.  
  10. }
  11. /////////////////////////////////////////
  12. package Main;
  13.  
  14. import java.awt.BorderLayout;
  15. import java.awt.Dimension;
  16.  
  17. import javax.swing.JFrame;
  18.  
  19. import draw.DrawPanel;
  20. import Menu.MainMenu;
  21. import Menu.MainToolbar;
  22.  
  23. public class MainFrame extends JFrame {
  24.  
  25.     private static final long serialVersionUID = 1L;
  26.     private static final Dimension SIZE = new Dimension(800, 600);
  27.     private DrawPanel panel = new DrawPanel();
  28.     public MainFrame(){
  29.         super("Java Paint");
  30.         setLayout(new BorderLayout());
  31.         add(panel, BorderLayout.CENTER);
  32.         //
  33.         MainMenu oknog = new MainMenu();
  34.         setJMenuBar(oknog);
  35.         MainToolbar oknot = new MainToolbar();
  36.         add(oknot, BorderLayout.NORTH);
  37.        
  38.         setSize(SIZE);
  39.         setResizable(true);
  40.         setLocationRelativeTo(null);
  41.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  42.         setVisible(true);
  43.        
  44.     }
  45.  
  46. }
  47. //////////////////////////////////////////
  48. package draw;
  49.  
  50. import java.awt.Color;
  51. import java.awt.Graphics;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54.  
  55. import javax.swing.JPanel;
  56.  
  57. import draw.shapes.Shape;
  58. import listeners.DrawPanelListener;
  59.  
  60. public class DrawPanel extends JPanel {
  61.  
  62.     private static final long serialVersionUID = 1L;
  63.     private DrawPanelListener dpl;
  64.     private List<Shape>shapes = new ArrayList<>();
  65.     private Shape tmpShape;
  66.  
  67.     public DrawPanel()
  68.     {
  69.         setOpaque(true);
  70.         setBackground(Color.WHITE);
  71.         dpl = new DrawPanelListener(this);
  72.         addMouseListener(dpl);
  73.        
  74.         addMouseMotionListener(dpl);
  75.    
  76.        
  77.        
  78.     }
  79.     public  void paintComponents(Graphics g){
  80.         super.paintComponent(g);
  81.         if(tmpShape != null)
  82.             tmpShape.render(g);
  83.         for(Shape s : shapes )
  84.         {
  85.             if(s != null)
  86.                 s.render(g);
  87.        
  88.         }
  89.     }
  90.     public List<Shape> getShapes() {
  91.         return shapes;
  92.     }
  93.     public void setShapes(List<Shape> shapes) {
  94.         this.shapes = shapes;
  95.     }
  96.     public Shape getTmpShape() {
  97.         return tmpShape;
  98.     }
  99.     public void setTmpShape(Shape tmpShape) {
  100.         this.tmpShape = tmpShape;
  101.     }
  102.  
  103. }
  104. /////////////////////////////////////////////////
  105. package Menu;
  106.  
  107. import javax.swing.JMenu;
  108. import javax.swing.JMenuBar;
  109. import javax.swing.JMenuItem;
  110. import java.awt.event.ActionEvent;
  111. import java.awt.event.ActionListener;
  112.  
  113.  
  114. public class MainMenu extends JMenuBar{
  115.    
  116. private JMenuItem newAction = new JMenuItem("Nowy");
  117. private JMenuItem openAction = new JMenuItem("Otwórz");
  118. private JMenuItem saveAction = new JMenuItem("Zapisz");
  119. private JMenuItem saveAsAction = new JMenuItem("Zapisz jako");
  120. private JMenuItem printAction = new JMenuItem("Drukuj");
  121. private JMenuItem exit = new JMenuItem("Zamknij");
  122.    
  123.    
  124. private JMenuItem undoAction = new JMenuItem("Cofnij");
  125. private JMenuItem redoAction = new JMenuItem("Redo");
  126. private JMenuItem copyAction = new JMenuItem("Kopiuj");
  127. private JMenuItem cutAction = new JMenuItem("Wytnij");
  128. private JMenuItem pasteAction = new JMenuItem("Wklej");
  129.  
  130.     private static final long serialVersionUID = 1L;
  131.     public MainMenu(){
  132.     JMenu plikmenu = new JMenu("Plik");
  133.     JMenu edycjamenu = new JMenu("Edycja");
  134.    
  135.     this.add(plikmenu);
  136.     this.add(edycjamenu);
  137.    
  138.  
  139.    
  140.    
  141.     plikmenu.add(newAction);
  142.     plikmenu.add(openAction);
  143.     plikmenu.add(saveAction);
  144.     plikmenu.add(saveAsAction);
  145.     plikmenu.addSeparator();
  146.     plikmenu.add(printAction);
  147.     plikmenu.addSeparator();
  148.     plikmenu.add(exit);
  149.    
  150.    
  151.     edycjamenu.add(undoAction);
  152.     edycjamenu.add(redoAction);
  153.     edycjamenu.addSeparator();
  154.     edycjamenu.add(copyAction);
  155.     edycjamenu.add(cutAction);
  156.     edycjamenu.add(pasteAction);
  157.    
  158.    
  159.     initFileActions();
  160.     initEditionAction();
  161.    
  162. }
  163. private void initEditionAction() {
  164. undoAction.addActionListener(new ActionListener() {
  165.    
  166.     @Override
  167.     public void actionPerformed(ActionEvent arg0) {
  168.        
  169.         // TODO Auto-generated method stub
  170.        
  171.     }
  172. });
  173. redoAction.addActionListener(new ActionListener() {
  174.    
  175.     @Override
  176.     public void actionPerformed(ActionEvent arg0) {
  177.        
  178.         // TODO Auto-generated method stub
  179.        
  180.     }
  181. });
  182. copyAction.addActionListener(new ActionListener() {
  183.    
  184.     @Override
  185.     public void actionPerformed(ActionEvent arg0) {
  186.        
  187.         // TODO Auto-generated method stub
  188.        
  189.     }
  190. });
  191. cutAction.addActionListener(new ActionListener() {
  192.    
  193.     @Override
  194.     public void actionPerformed(ActionEvent arg0) {
  195.        
  196.         // TODO Auto-generated method stub
  197.        
  198.     }
  199. });
  200. pasteAction.addActionListener(new ActionListener() {
  201.    
  202.     @Override
  203.     public void actionPerformed(ActionEvent arg0) {
  204.        
  205.         // TODO Auto-generated method stub
  206.        
  207.     }
  208. });
  209.    
  210.    
  211. }
  212. private void initFileActions(){
  213. newAction.addActionListener(new ActionListener() {
  214.    
  215.     @Override
  216.     public void actionPerformed(ActionEvent arg0) {
  217.        
  218.         // TODO Auto-generated method stub
  219.        
  220.     }
  221. });
  222. openAction.addActionListener(new ActionListener() {
  223.    
  224.     @Override
  225.     public void actionPerformed(ActionEvent arg0) {
  226.        
  227.         // TODO Auto-generated method stub
  228.        
  229.     }
  230. });
  231. saveAction.addActionListener(new ActionListener() {
  232.    
  233.     @Override
  234.     public void actionPerformed(ActionEvent arg0) {
  235.        
  236.         // TODO Auto-generated method stub
  237.        
  238.     }
  239. });
  240. saveAsAction.addActionListener(new ActionListener() {
  241.  
  242. @Override
  243. public void actionPerformed(ActionEvent arg0) {
  244.    
  245.     // TODO Auto-generated method stub
  246.    
  247. }
  248. });
  249. printAction.addActionListener(new ActionListener() {
  250.    
  251.     @Override
  252.     public void actionPerformed(ActionEvent arg0) {
  253.        
  254.         // TODO Auto-generated method stub
  255.        
  256.     }
  257. });
  258. exit.addActionListener(new ActionListener() {
  259.    
  260.     @Override
  261.     public void actionPerformed(ActionEvent arg0) {
  262.        
  263.         System.exit(0);
  264.     }
  265. });
  266.  
  267. }
  268. }
  269. ///////////////////////////////
  270. package listeners;
  271.  
  272. import java.awt.Color;
  273. import java.awt.event.MouseEvent;
  274. import java.awt.event.MouseListener;
  275. import java.awt.event.MouseMotionListener;
  276. import java.util.List;
  277.  
  278. import draw.DrawPanel;
  279. import draw.shapes.Shape;
  280. import draw.shapes.Square;
  281.  
  282. public class DrawPanelListener implements MouseListener , MouseMotionListener {
  283.     private DrawPanel panel;
  284.     private boolean draw = false;
  285.     private Shape drawShape;
  286.     public DrawPanelListener(DrawPanel dp){
  287.         panel = dp;
  288.    
  289.     }
  290.    
  291.  
  292.     @Override
  293.     public void mouseClicked(MouseEvent e) {
  294.         // TODO Auto-generated method stub
  295.  
  296.     }
  297.  
  298.     @Override
  299.     public void mouseEntered(MouseEvent e) {
  300.         // TODO Auto-generated method stub
  301.  
  302.     }
  303.  
  304.     @Override
  305.     public void mouseExited(MouseEvent e) {
  306.         // TODO Auto-generated method stub
  307.  
  308.     }
  309.  
  310.     @Override
  311.     public void mousePressed(MouseEvent e) {
  312.         // TODO Auto-generated method stub
  313.  
  314.         if(!draw)
  315.             draw= true;
  316.        
  317.         drawShape = new Square(e.getX(),e.getY(), e.getX() + 1, e.getY() + 1, Color.black);
  318.         panel.setTmpShape(drawShape);
  319.         panel.repaint();
  320.     }
  321.  
  322.     @Override
  323.     public void mouseReleased(MouseEvent e) {
  324.         drawShape.setX2(e.getX());
  325.         drawShape.setY2(e.getY());
  326.        
  327.         List<Shape>shapes = panel.getShapes();
  328.         shapes.add(drawShape);
  329.         panel.setTmpShape(null);
  330.         panel.setShapes(shapes);
  331.         drawShape  = null;
  332.         panel.repaint();
  333.  
  334.     }
  335.  
  336.  
  337.     @Override
  338.     public void mouseDragged(MouseEvent e) {
  339.         // TODO Auto-generated method stub
  340.         drawShape.setX2(e.getX());
  341.         drawShape.setY2(e.getY());
  342.         panel.setTmpShape(drawShape);
  343.         panel.repaint();
  344.     }
  345.  
  346.  
  347.     @Override
  348.     public void mouseMoved(MouseEvent e) {
  349.         // TODO Auto-generated method stub
  350.        
  351.     }
  352.  
  353. }
  354. ////////////////////////////////
  355. package draw.shapes;
  356.  
  357. import java.awt.Color;
  358. import java.awt.Graphics;
  359.  
  360. public abstract class Shape {
  361.     private int x,y;
  362.     private int y2,x2;
  363.     private Color color;
  364.     public Shape (int x, int y, int x2, int y2, Color c)
  365.     {
  366.         this.x =x;
  367.         this.y=y;
  368.         this.y2 = y2;
  369.         this.x2 = x2;
  370.         color = c;
  371.     }
  372.     public abstract void render(Graphics g);
  373.     public int Wight() {
  374.         if(x2>x)
  375.             return x2-x;
  376.         else
  377.             return x-x2;
  378.     }
  379.     public int Height()
  380.     {
  381.         if(y2>y)
  382.             return y2-y;
  383.         else
  384.             return y-y2;
  385.        
  386.     }
  387.     public int getX() {
  388.         return x;
  389.     }
  390.     public void setX(int x) {
  391.         this.x = x;
  392.     }
  393.     public int getY() {
  394.         return y;
  395.     }
  396.     public void setY(int y) {
  397.         this.y = y;
  398.     }
  399.     public int getY2() {
  400.         return y2;
  401.     }
  402.     public void setY2(int y2) {
  403.         this.y2 = y2;
  404.     }
  405.     public int getX2() {
  406.         return x2;
  407.     }
  408.     public void setX2(int x2) {
  409.         this.x2 = x2;
  410.     }
  411.     public Color getColor() {
  412.         return color;
  413.     }
  414.     public void setColor(Color color) {
  415.         this.color = color;
  416.     }
  417.    
  418. }
  419. package draw.shapes;
  420.  
  421. import java.awt.Color;
  422. import java.awt.Graphics;
  423.  
  424. public class Square extends Shape {
  425.  
  426.     public Square(int x, int y, int x2, int y2, Color c) {
  427.         super(x, y, x2, y2, c);
  428.         // TODO Auto-generated constructor stub
  429.     }
  430.  
  431.     @Override
  432.     public void render(Graphics g) {
  433.         g.setColor(getColor());
  434.         g.fillRect(getX(), getY(), Wight(), Height());
  435.         // TODO Auto-generated method stub
  436.  
  437.     }
  438.  
  439. }
  440. //////////////////////////////////////
  441. package Menu;
  442.  
  443. import javax.swing.JToolBar;
  444.  
  445. public class MainToolbar extends JToolBar{
  446.  
  447.     /**
  448.      *
  449.      */
  450.     private static final long serialVersionUID = 1L;
  451.     public MainToolbar() {
  452.         // TODO Auto-generated constructor stub
  453.     }
  454.  
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement