getting the graphics context from inside event methods

L

learningjava

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();
}
}


**************
 
R

Ryan Stewart

learningjava said:
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

First, where on earth did you learn to do that? Why are you overriding
addActionListener and removeActionListener? And why limit it to one
ActionListener? Especially when you never use any. Why is your circle a
JComponent? Why why why why..... If you got it from a book or example
somewhere, the author should be hunted down and shot. If this is code you
came up with on your own, I can understand. There's a severe lack of how-tos
out there concerning graphical programming. Java 1.4 Game Programming is the
one I picked up. It's very good if you really want to learn. A more complete
reply to follow....
 
R

Ryan Stewart

Ryan Stewart said:
First, where on earth did you learn to do that? Why are you overriding
addActionListener and removeActionListener? And why limit it to one
ActionListener? Especially when you never use any. Why is your circle a
JComponent? Why why why why..... If you got it from a book or example
somewhere, the author should be hunted down and shot. If this is code you
came up with on your own, I can understand. There's a severe lack of how-tos
out there concerning graphical programming. Java 1.4 Game Programming is the
one I picked up. It's very good if you really want to learn. A more complete
reply to follow....

Try this on for size. If you don't understand some part of it, just ask.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MousePanel extends JPanel implements MouseListener,
MouseMotionListener {
private Circle circle;

public MousePanel() {
addMouseListener(this);
addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e) {
circle = new Circle(e.getX(), e.getY(), 50, Color.BLUE);
repaint();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (circle != null) {
circle.render(g);
}
}

public void mouseReleased(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
if (circle != null) {
circle.setPosition(e.getX(), e.getY());
}
repaint();
}

public void mouseMoved(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.getContentPane().add(new MousePanel(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
}

private class Circle {
private int x;
private int y;
private int radius;
private Color color;

public Circle(int x, int y, int radius, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}

public void render(Graphics g) {
g.setColor(color);
g.fillOval(x - radius, y - radius, radius*2, radius*2);
}

public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}

}
}
 
T

Thomas Weidenfeller

learningjava said:
hi,
This is a beginner's attempt to write a simple graphics/swing
application.

Others, and I have provided the information at least a hundred times. So
you are well advised to first read archives of the relevant newsgroups.

* Beginners questions should go to comp.lang.java.help

* GUI questions should go to comp.lang.java.gui

* Don't use getGraphics() (last time I checked I alone have answered
this more than 40 times in comp.lang.java.gui). It is defined as being
able to return null (you didn't read the API documentation, did you?).
See e.g.

http://groups.google.com/[email protected]
http://groups.google.com/[email protected]

The later also contains pointers to the TSC Swing architecture papers
from Sun. You are also well advised to read them first, esp. the on
about the painting model.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top