Hi, I'm fairly new to Java in the sense that I haven't programmed Java in 2 years. I am trying to get back into the swing of things, but I can't find any reference to what I need. I'm trying to write a simple drawing program that only utilizes the brush for now, I'll implement what else I need later.
My code started out as:
As such it worked fairly well, but I want to implement in a JFrame. So now my code looks like:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawWithMe extends JFrame { int xvalue = -10, yvalue = -10; public DrawWithMe() { getContentPane().add(new Label("Click and drag"), BorderLayout.SOUTH); addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { xvalue = event.getX(); yvalue = event.getY(); repaint(); } } ); addKeyListener ( new KeyAdapter() { public void keyPressed(KeyEvent event) { if(event.getKeyCode() == 27) { System.exit(0); } } } ); setSize(500,500); setVisible(true); } public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(xvalue, yvalue, 10, 10); } public static void main(String args[]) { DrawWithMe application = new DrawWithMe(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
But now, it won't even draw. Could you point out what I may need to do? I just need a push in the right direction.import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawWithMe extends JFrame { JFrame frame = new JFrame("DrawWithMe"); int xvalue = -10, yvalue = -10; public DrawWithMe() { frame.getContentPane().add(new Label("Click and drag"), BorderLayout.SOUTH); frame.addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { xvalue = event.getX(); yvalue = event.getY(); repaint(); } } ); frame.addKeyListener ( new KeyAdapter() { public void keyPressed(KeyEvent event) { if(event.getKeyCode() == 27) { System.exit(0); } } } ); frame.setSize(500,500); frame.setVisible(true); } public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(xvalue, yvalue, 10, 10); } public static void main(String args[]) { DrawWithMe application = new DrawWithMe(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Thank you in advanced for any and all help given,
-K