I
Ian
Hi,
Can anyone please help as I can't find where the problem is with the program
below.
I have cut out most of the code but the idea is a simple one : make a
selection via a JButton for the required action . I have two of the actions
working fine (clear and exit). When selecting a shape the idea is to paint
the apppropriate Entity(at the location of a mouse click) but for some
reason this won't work. I don't see why the shapes arent being drawn at the
location of the mouse clicks.
Any help with where I have gone wrong will be appreciated.
regards
Ian.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class drawme
{
public static void main(String[] args)
{
// use look and feel for my system (Win32)
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
Panel f = new Panel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.show();
}
}
class Panel extends JFrame
implements ActionListener, MouseListener
{
private PaintPanel pp;
private JButton square;
private JButton circle;
private JButton clear;
private JButton exit;
int x;
int y;
public Panel()
{
// ----------------------------------
// construct and configure components
// ----------------------------------
pp = new PaintPanel();
pp.setBorder(BorderFactory.createLineBorder(Color.gray));
square = new JButton("Square");
circle = new JButton("Circle");
clear = new JButton("Clear");
exit = new JButton("Exit");
// make buttons the same size
exit.setMaximumSize(clear.getPreferredSize());
// -------------
// add listeners
// -------------
pp.addMouseListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
square.addActionListener(this);
circle.addActionListener(this);
// ------------------
// arrange components
// ------------------
JPanel cp = new JPanel();
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
cp.add(square);
cp.add(circle);
cp.add(clear);
cp.add(exit);
pp.setAlignmentY(Component.TOP_ALIGNMENT);
cp.setAlignmentY(Component.TOP_ALIGNMENT);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(pp);
p.add(cp);
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.setContentPane(p);
}
// -------------------------------
// implement ActionListener method
// -------------------------------
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if (source == clear)
pp.clear();
else if (source == exit)
System.exit(0);
else if (source == square)
pp.drawEntity( Entity.SQUARE, x, y);
else if (source == circle)
pp.drawEntity( Entity.CIRCLE, x, y);
}
// -----------------------------------
// implement MouseListener methods (5)
// -----------------------------------
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseClicked(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
}
/** An inner class for painting shapes and other graphics.
*/
class PaintPanel extends JPanel
{
private Vector v;
PaintPanel()
{
v = new Vector();
this.setBackground(Color.pink);
this.setPreferredSize(new Dimension(250, 250));
}
public void paint(Graphics g)
{
super.paint(g); // paint background
paintEntities(g);
}
/** Paint all the entities stored in the vector */
private void paintEntities(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < v.size(); ++i)
{
Entity e = (Entity)v.elementAt(i);
int x = e.getX();
int y = e.getY();
switch (e.getType())
{
case Entity.SQUARE:
g2.setColor(new Color(0, 0, 128));
g2.draw(new Rectangle2D.Double(x, y, 25, 25));
break;
case Entity.CIRCLE:
g2.setColor(new Color(128, 0, 0));
g2.draw(new Ellipse2D.Double(x, y, 25, 25));
break;
}
}
}
/** Clear the panel (by clearing the vector of entities) */
public void clear()
{
v.clear();
this.repaint();
}
/** Draw an entity */
public void drawEntity( int type, int x, int y)
{
v.addElement(new Entity( type, x, y));
this.repaint();
}
}
/** A simple class to store information about graphics entities */
private class Entity
{
final static int SQUARE = 1;
final static int CIRCLE = 2;
private int type; // type of entity
private int x; // x coordinate for painting
private int y; // y coordinate for painting
Entity( int typeArg, int xArg, int yArg)
{
type = typeArg;
x = xArg;
y = yArg;
}
public int getType() {
return type; }
public int getX() {
return x; }
public int getY() {
return y; }
}
}
Can anyone please help as I can't find where the problem is with the program
below.
I have cut out most of the code but the idea is a simple one : make a
selection via a JButton for the required action . I have two of the actions
working fine (clear and exit). When selecting a shape the idea is to paint
the apppropriate Entity(at the location of a mouse click) but for some
reason this won't work. I don't see why the shapes arent being drawn at the
location of the mouse clicks.
Any help with where I have gone wrong will be appreciated.
regards
Ian.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class drawme
{
public static void main(String[] args)
{
// use look and feel for my system (Win32)
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
Panel f = new Panel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.show();
}
}
class Panel extends JFrame
implements ActionListener, MouseListener
{
private PaintPanel pp;
private JButton square;
private JButton circle;
private JButton clear;
private JButton exit;
int x;
int y;
public Panel()
{
// ----------------------------------
// construct and configure components
// ----------------------------------
pp = new PaintPanel();
pp.setBorder(BorderFactory.createLineBorder(Color.gray));
square = new JButton("Square");
circle = new JButton("Circle");
clear = new JButton("Clear");
exit = new JButton("Exit");
// make buttons the same size
exit.setMaximumSize(clear.getPreferredSize());
// -------------
// add listeners
// -------------
pp.addMouseListener(this);
clear.addActionListener(this);
exit.addActionListener(this);
square.addActionListener(this);
circle.addActionListener(this);
// ------------------
// arrange components
// ------------------
JPanel cp = new JPanel();
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
cp.add(square);
cp.add(circle);
cp.add(clear);
cp.add(exit);
pp.setAlignmentY(Component.TOP_ALIGNMENT);
cp.setAlignmentY(Component.TOP_ALIGNMENT);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(pp);
p.add(cp);
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.setContentPane(p);
}
// -------------------------------
// implement ActionListener method
// -------------------------------
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if (source == clear)
pp.clear();
else if (source == exit)
System.exit(0);
else if (source == square)
pp.drawEntity( Entity.SQUARE, x, y);
else if (source == circle)
pp.drawEntity( Entity.CIRCLE, x, y);
}
// -----------------------------------
// implement MouseListener methods (5)
// -----------------------------------
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseClicked(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
}
/** An inner class for painting shapes and other graphics.
*/
class PaintPanel extends JPanel
{
private Vector v;
PaintPanel()
{
v = new Vector();
this.setBackground(Color.pink);
this.setPreferredSize(new Dimension(250, 250));
}
public void paint(Graphics g)
{
super.paint(g); // paint background
paintEntities(g);
}
/** Paint all the entities stored in the vector */
private void paintEntities(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < v.size(); ++i)
{
Entity e = (Entity)v.elementAt(i);
int x = e.getX();
int y = e.getY();
switch (e.getType())
{
case Entity.SQUARE:
g2.setColor(new Color(0, 0, 128));
g2.draw(new Rectangle2D.Double(x, y, 25, 25));
break;
case Entity.CIRCLE:
g2.setColor(new Color(128, 0, 0));
g2.draw(new Ellipse2D.Double(x, y, 25, 25));
break;
}
}
}
/** Clear the panel (by clearing the vector of entities) */
public void clear()
{
v.clear();
this.repaint();
}
/** Draw an entity */
public void drawEntity( int type, int x, int y)
{
v.addElement(new Entity( type, x, y));
this.repaint();
}
}
/** A simple class to store information about graphics entities */
private class Entity
{
final static int SQUARE = 1;
final static int CIRCLE = 2;
private int type; // type of entity
private int x; // x coordinate for painting
private int y; // y coordinate for painting
Entity( int typeArg, int xArg, int yArg)
{
type = typeArg;
x = xArg;
y = yArg;
}
public int getType() {
return type; }
public int getX() {
return x; }
public int getY() {
return y; }
}
}