getting the graphics context from inside event methods

From: learningjava (compsci_at_rogers.com)
Date: 01/28/04


Date: 28 Jan 2004 14:15:45 -0800

hi,
This is a beginner's attempt to write a simple graphics/swing
application.
I'm trying to create a simple application in which
a circle is painted on the canvas everytime a
mouse is clicked on it (with the coordinates of the mouse).
Also, the circle moves with the mouse.

I have a class Circle that draws the circle,
and a method createAndShowGUI that displays the
GUI panel.Plus, additional classes ML and MML (listener classes).

I cannot get the current graphics context inside of
the mousePressed and mouseMoved methods to work.
Actually, they return null. Because of which, the graphics don't occur.

What can be the solution to get the graphics context?

TIA,

Gk

This is my code:
***************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Test extends JPanel{
    JTextField t = new JTextField(30);
    Canvas canvas = new Canvas();
    protected int xm, ym;
    protected int cSize = 100; // Circle size
    public Color co = Color.blue;
    protected ActionListener actionListener;
   
public Test () {
//addMouseListener(new ML());
//addMouseMotionListener(new MML());
}

public class Circle extends JComponent
    {
        public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.setColor(Color.blue);
        g.drawOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
        g.fillOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
        }
        }

public void addActionListener (
      ActionListener l)
        throws TooManyListenersException {
    if(actionListener != null)
      throw new TooManyListenersException();
    actionListener = l;
  }
  
public void removeActionListener(
      ActionListener l) {
    actionListener = null;
}

class ML extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
      xm = e.getX();
      ym = e.getY();
      //for some reason,
      //i cannot get the graphics context,
      //hence cannot draw a circle on mouse pressed
      //same with mouseMoved.
      //how can I get the current graphics context?
      //because of this, the graphics methods don't work
      
      //Graphics g = getGraphics();
      //g.drawOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
      //g.fillOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
      
      repaint();
      t.setText("mouse clicked, xm = " + xm + "ym = " + ym);
      
      // Call the listener's method:
      if(actionListener != null)
        actionListener.actionPerformed(
          new ActionEvent(Test.this,
            ActionEvent.ACTION_PERFORMED, null));
    }
}
  
  
class MML extends MouseMotionAdapter {
    public void mouseMoved(MouseEvent e) {
      xm = e.getX();
      ym = e.getY();
      //for some reason,
      //i cannot get the graphics context,
      //hence cannot draw a circle on mouse pressed
      //same with mouseMoved.
      //how can I get the current graphics context?
      //because of this, the graphics methods don't work
      
      //Graphics g = getGraphics();
      //co = Color.red;
      //g.setColor(co);
      //g.drawOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
      //g.fillOval(xm - cSize/2, ym - cSize/2 , cSize, cSize);
      t.setText("mouse moved to , xm = " + xm + "ym = " + ym);
      repaint();
   }
}

public void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        //Create and set up the window.
        JFrame frame = new JFrame("Test");
        Circle p1 = new Circle();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(t, BorderLayout.SOUTH);
        frame.getContentPane().add(p1, BorderLayout.CENTER);
        frame.addMouseListener(new ML());
        frame.addMouseMotionListener(new MML());
        frame.pack();
        frame.setVisible(true);
}
  
public static void main(String[] args) {
        final Test trial = new Test();
        trial.createAndShowGUI();
   }
}

**************



Relevant Pages

  • Re: Using Template Pattern, problem with extends to Applet
    ... private class Board ... public void initializeGame() { ... how would i able to extends to applet also ... also tried graphics g = null; ...
    (comp.lang.java.gui)
  • Re: AWT event model problem
    ... I followed your suggestions and initialized the Graphics g inside each event ... However the FIRST TIME i left click and hold the mouse down, ... public void paint ... public void mouseEntered(MouseEvent e) { ...
    (comp.lang.java.gui)
  • circle
    ... I have been trying to write this circle method that should move the ... public void moveRight() ... private int diameter; ... Returns the diameter of the receiver. ...
    (comp.lang.java.help)
  • Re: paintComponent question
    ... E.g. if you want the whole component to be at some arbitrary coordinates ... want to let the user to set the location of the circle by clicking. ... private static class CircleView extends JComponent { ... public void mouseClicked(MouseEvent e) { ...
    (comp.lang.java.gui)
  • Re: Code example for transformation by mouse dragging
    ... > it's new rotational position (in the case of a circle this would be ... class JPanelToDrawOn extends JPanel { ... public void panelMouseReleased{ ... Then each drawableObject needs to implement an interface something like ...
    (comp.lang.java.programmer)