Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 4th, 2012  |  syntax: None  |  size: 3.34 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class InputPanel extends JPanel{
  2.     public static int shapeType; //1: Rectangle; 2: Oval; 3: Line
  3.     public static boolean isFilled; //whether or not the shape is filled
  4.     public static Color color; //color of the shape
  5.  
  6.     public InputPanel(){
  7.  
  8.         JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  9.         panel.setBackground(Color.GRAY);
  10.         setPreferredSize(new Dimension(200,500));
  11.  
  12.         JButton rect = new JButton("Rectangle");
  13.         JButton oval = new JButton("Oval");
  14.         JButton line = new JButton("Line");
  15.         JRadioButton fill = new JRadioButton("Filled:");
  16.         JButton color1 = new JButton("Color..");
  17.  
  18.         rect.addActionListener(new rectListener());
  19.         oval.addActionListener(new ovalListener());
  20.         line.addActionListener(new lineListener());
  21.         isFilled = fill.isSelected();
  22.         color1.addActionListener(new colorListener());
  23.  
  24.         panel.add(rect);
  25.         panel.add(oval);
  26.         panel.add(line);
  27.         panel.add(fill);
  28.         panel.add(color1);
  29.  
  30.         this.setVisible(true);
  31.  
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. public class PaintPanel extends JPanel{
  38.  
  39.     public static int x1, y1, x2, y2;
  40.  
  41.     ArrayList<Shape> shapeList = new ArrayList<Shape>();
  42.  
  43.     public PaintPanel(){
  44.  
  45.         JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  46.         panel.setBackground(Color.WHITE);
  47.         setPreferredSize(new Dimension(500, 500));
  48.  
  49.         this.addMouseListener(new MouseAdapter() {
  50.         @Override public void mousePressed(MouseEvent e) {
  51.             PaintPanel.x1 = e.getX();
  52.             PaintPanel.y1 = e.getY();
  53.         }
  54.  
  55.         @Override public void mouseReleased(MouseEvent e) {
  56.             PaintPanel.x2 = e.getX();
  57.             PaintPanel.y2 = e.getY();
  58.             if(InputPanel.shapeType == 1){
  59.                 shapeList.add(new Rectangle(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
  60.             }
  61.             if(InputPanel.shapeType == 2){
  62.                 shapeList.add(new Oval(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2, InputPanel.isFilled));
  63.             }  
  64.             if(InputPanel.shapeType == 3){
  65.                 shapeList.add(new Line(PaintPanel.x1, PaintPanel.y1, PaintPanel.x2, PaintPanel.y2));
  66.             }
  67.             repaint();
  68.         }
  69.       });
  70.  
  71.      this.setVisible(true);  
  72.     }
  73.  
  74.     @Override
  75.     public void paintComponent(Graphics g){
  76.         super.paintComponent(g);
  77.  
  78.         for(Shape s : shapeList){
  79.             s.draw(g);
  80.         }
  81.     }
  82.  
  83. }
  84.  
  85. public class PaintGUI {
  86.  
  87.     public static void main(String[] args){
  88.  
  89.         JFrame frame = new JFrame("Shape Drawer!");
  90.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91.  
  92.         frame.add(new InputPanel());
  93.         frame.add(new PaintPanel());
  94.  
  95.         frame.pack();
  96.  
  97.         frame.setVisible(true);
  98.  
  99.     }
  100.  
  101. }
  102.        
  103. public static void main(String[] args){
  104.  
  105.     SwingUtilities.invokeLater(new Runnable() {
  106.         @Override
  107.         public void run() {
  108.             JFrame frame = new JFrame("Shape Drawer!");
  109.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  110.  
  111.             //set the layout, add your panels
  112.  
  113.             frame.pack();
  114.             frame.setVisible(true);            
  115.         }
  116.     });
  117. }
  118.        
  119. frame.add(new InputPanel());
  120. frame.add(new PaintPanel());
  121.        
  122. frame.add(new InputPanel(), BorderLayout.CENTER);
  123. frame.add(new PaintPanel(), BorderLayout.CENTER);
clone this paste RAW Paste Data