S
Shane
Im having trouble getting started with swing. Â I have created the following,
but I cannot figure out how to paintComponent(Graphics g){ g.drawOval(x, y,
2, 2);}. I posted this originally to c.l.java.help but that may be a low
traffic NG so am reposting it
um
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
//import tutorial.MyDrawPanel;
/**
 * @author Shane
 */
public class Assignment2 extends JPanel implements MouseListener{
    int x1, y1, x2, y2;
    static JFrame jf;
    BlankArea blankArea;
    // eclipse complaint fix0red
    static final long serialVersionUID =0;
   Â
    public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
     Â
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    } catch (IllegalAccessException ex) {
      ex.printStackTrace();
    } catch (InstantiationException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        drawGUI();
      }
    });
  }
    class MyDrawPanel extends JPanel{
        // eclipse complaint fix0red again
        static final long serialVersionUID =0;
    }
    static MyDrawPanel dp;
    //Draw our window
   Â
   Â
   Â
    static JComponent newContentPane;
   Â
    private static void drawGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Regression Line");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Â
    //Create and set up the content pane.
    newContentPane = new Assignment2();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.setPreferredSize(new Dimension(500,500));
   Â
    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }
 Â
    //mouse events
    public Assignment2() {
    super(new GridLayout(0,1));
    blankArea = new BlankArea(Color.WHITE);
    add(blankArea);
    //Register for mouse events on blankArea and the panel.
    blankArea.addMouseListener(this);
    addMouseListener(this);
    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
  }
   Â
    void eventOutput(String eventDescription, MouseEvent e) {
  }
   Â
    public void mousePressed(MouseEvent e) {
  }
 Â
  public void mouseReleased(MouseEvent e) {
  }
 Â
  public void mouseEntered(MouseEvent e) {
  }
 Â
  public void mouseExited(MouseEvent e) {
  }
 Â
    int x, y;
    //Grab the x and y of where the mouse is clicked
   Â
  public void mouseClicked(MouseEvent e) {
    // eventOutput("Mouse clicked (# of clicks: "
        // + e.getClickCount() + ")", e);
    x = e.getX();
    y = e.getY();
   Â
    dp.repaint();
   Â
  };
 Â
}
but I cannot figure out how to paintComponent(Graphics g){ g.drawOval(x, y,
2, 2);}. I posted this originally to c.l.java.help but that may be a low
traffic NG so am reposting it
um
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
//import tutorial.MyDrawPanel;
/**
 * @author Shane
 */
public class Assignment2 extends JPanel implements MouseListener{
    int x1, y1, x2, y2;
    static JFrame jf;
    BlankArea blankArea;
    // eclipse complaint fix0red
    static final long serialVersionUID =0;
   Â
    public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
     Â
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    } catch (IllegalAccessException ex) {
      ex.printStackTrace();
    } catch (InstantiationException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        drawGUI();
      }
    });
  }
    class MyDrawPanel extends JPanel{
        // eclipse complaint fix0red again
        static final long serialVersionUID =0;
    }
    static MyDrawPanel dp;
    //Draw our window
   Â
   Â
   Â
    static JComponent newContentPane;
   Â
    private static void drawGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Regression Line");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Â
    //Create and set up the content pane.
    newContentPane = new Assignment2();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.setPreferredSize(new Dimension(500,500));
   Â
    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }
 Â
    //mouse events
    public Assignment2() {
    super(new GridLayout(0,1));
    blankArea = new BlankArea(Color.WHITE);
    add(blankArea);
    //Register for mouse events on blankArea and the panel.
    blankArea.addMouseListener(this);
    addMouseListener(this);
    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
  }
   Â
    void eventOutput(String eventDescription, MouseEvent e) {
  }
   Â
    public void mousePressed(MouseEvent e) {
  }
 Â
  public void mouseReleased(MouseEvent e) {
  }
 Â
  public void mouseEntered(MouseEvent e) {
  }
 Â
  public void mouseExited(MouseEvent e) {
  }
 Â
    int x, y;
    //Grab the x and y of where the mouse is clicked
   Â
  public void mouseClicked(MouseEvent e) {
    // eventOutput("Mouse clicked (# of clicks: "
        // + e.getClickCount() + ")", e);
    x = e.getX();
    y = e.getY();
   Â
    dp.repaint();
   Â
  };
 Â
}