title: Cant draw Shapes in Java
body: <p>another java swing question. I really really searched for lots of hours but i cant get my code working.. so i hope you guys can help me out. I think i have some fundamental understanding issues.</p>
  
  <p>I have to expand existing code.
  My first task was to draw a line from mouseclick to release, i implemented that successfully.</p>
  
  <p>Now I should implement missing code in the RandomDrawer class to draw circles with the click of the JMenuItem. They should be random and all, but that is not important right now, i can do this WHEN i actually draw oder SEE my shapes :D.</p>
  
  <p>My problem is i can't get it to draw anything at all.
  How can i tell him to paint on the canvas? 
  It worked in the Painter Class, when i implement it in the same way to the RandomDrawer Class it does not.
  Can someone help me?</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>    package plotterpackage;
  
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Point;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import java.awt.geom.Line2D;
  
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JPanel;
      import javax.swing.JTextField;
      import javax.swing.WindowConstants;
      import javax.swing.border.BevelBorder;
  
      /
  
  public class Main {
      private JFrame frame;
      private JPanel canvas;
      private JPanel statusBar;
      private JTextField status;
  
      public Main() {
          initialize();
      }
  
      public static void main(String[] args) {
          Main app = new Main();
          app.start();
      }
  
      protected void initialize() {
          frame = new JFrame("Main");
          frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          frame.setBounds(50,50, 50+640, 50+480);
          frame.setBackground(Color.GREEN);
          frame.setJMenuBar(createMenuBar());
  
          frame.getContentPane().add(createContent(), BorderLayout.CENTER);
          frame.getContentPane().add(createStatusBar(),BorderLayout.SOUTH);
      }
  
      public void start() {
          // show the GUI
          frame.setVisible(true);
          status.setText("Application started");
      }
  
      protected JComponent createContent() {
          canvas = new JPanel();
          canvas.addMouseListener(new Painter());
          canvas.setBackground(new Color(128,218,255));
          canvas.setForeground(Color.RED);
          canvas.setBorder(new BevelBorder(BevelBorder.LOWERED));
          return canvas;
      }
  
      protected JComponent createStatusBar() {
          FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
          layout.setHgap(5);
  
          statusBar = new JPanel(layout);
          statusBar.add(new JLabel("Status: "));
  
          status = new JTextField();
          status.setPreferredSize(new Dimension(400,25));
          status.setEditable(false);
          status.setBorder(new BevelBorder(BevelBorder.RAISED,Color.MAGENTA,Color.LIGHT_GRAY));
          status.getInsets().set(2, 10, 2, 10);
          statusBar.add(status);
  
          return statusBar;
      }
  
      protected JMenuBar createMenuBar() {
          JMenuBar mb = new JMenuBar();
          JMenuItem item;
          JMenu menu;
          // Action menu
          menu = new JMenu("Actions");
          mb.add(menu);
          item = new JMenuItem("Draw RandomCircle");
          item.addActionListener(new RandomDrawer());
          menu.add(item);
  
          item = new JMenuItem("Draw RandomTriangle");
          item.addActionListener(new RandomDrawer());
          menu.add(item);
  
          menu.addSeparator();
          item = new JMenuItem("Exit");
          item.addActionListener(new AppCloser());
          menu.add(item);
  
          // Color menu not used so far
          menu = new JMenu("Colors");        
          mb.add(menu);
          // Help menu not used so far
          menu = new JMenu("Help");        
          mb.add(menu);
  
          return mb;
      }
  
      class AppCloser implements ActionListener {
  
          @Override
          public void actionPerformed(ActionEvent e) {
              System.out.printf("application finished, bye-bye... \n");
              frame.setVisible(false);
              frame.dispose();
              System.exit(0);
          }        
      }
  
      class RandomDrawer extends JPanel implements ActionListener {
  
          @Override 
          public void paintComponent(Graphics g) {
              super.paintComponent(g);
              g.drawOval(50, 50, 50, 50);
              //repaint();
          }
          @Override
          public void actionPerformed(ActionEvent e) {
              double x = Math.random();
              double y = Math.random();
              status.setText(String.format("rnd draw x:%.3f y:%.3f",x,y));
              //Graphics2D gg = (Graphics2D) canvas.getGraphics();
              //paintComponent(gg);
              //gg.draw(new Line2D.Double(0, 0, x, y));
              //gg.drawLine(5, 5, 20, 20);
              //gg.paintComponent();
          }
  
  
      }
  
  
      class Painter extends MouseAdapter {
          Point start, end;
          int startX, startY;
  
          @Override
          public void mousePressed(MouseEvent e) {
              start = e.getPoint();
              startX = e.getX();
              startY = e.getY();
              status.setText(String.format("Mouse start: " + start));
  
          }
  
  
          @Override
          public void mouseReleased(MouseEvent e) {
              end = e.getPoint();
              status.setText(String.format("Mouse end: " + end));
              Graphics2D gc = (Graphics2D) canvas.getGraphics();
              //gc.drawLine(0, 0, 80, 80);
              gc.drawLine(startX, startY, e.getX(), e.getY());
              //gc.drawOval(startX, startY, e.getX(), e.getY());
              //gc.fillOval(startX, startY, e.getX(), e.getY());
          }        
      }
  }
  </code></pre>
  
  <p>Thanks a lot.</p>