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