paint gui problem

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; }
}
}
 
S

Scorpio

I have enclosed a working version of your code and put a comment (with my
name Scorpio) besides the lines I have changed.

Regards,

Scorpio


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;
int shape = Entity.CIRCLE; // scorpio - added here - defaults to
CIRCLE
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)
{
shape = Entity.SQUARE;
pp.drawEntity( Entity.SQUARE, x, y);
}
else if (source == circle)
{
shape = Entity.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)
{
x = me.getX(); // scorpio - got rid of re-declaration was (was int
x)
y = me.getY(); // scorpio - got rid of re-declaration was (was int
y)
pp.drawEntity( shape, x, y);

}

/** 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; }
}
}
 
I

Ian

Hi,
Thankyou Scorpio for your reply, appology for staring new post but have
access problems.
I don't understand what is causing the odd behaviour - when selecting a
different shape button the shape is immediately drawn on top of the previous
shapes' location - using its coorinates(instead of waiting for a new mouse
click and drawing the new shape at the new coordinates) .
Ps this is why I had the original x and y declarations - which I now know
were wrong anyway
Any further help appreciated
Ian

Scorpio said:
I have enclosed a working version of your code and put a comment (with my
name Scorpio) besides the lines I have changed.

Regards,

Scorpio


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;
int shape = Entity.CIRCLE; // scorpio - added here - defaults to
CIRCLE
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)
{
shape = Entity.SQUARE;
pp.drawEntity( Entity.SQUARE, x, y);
}
else if (source == circle)
{
shape = Entity.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)
{
x = me.getX(); // scorpio - got rid of re-declaration was (was int
x)
y = me.getY(); // scorpio - got rid of re-declaration was (was int
y)
pp.drawEntity( shape, x, y);

}

/** 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; }
}
}
 
F

Fred L. Kleinschmidt

Ian said:
Hi,
Thankyou Scorpio for your reply, appology for staring new post but have
access problems.
I don't understand what is causing the odd behaviour - when selecting a
different shape button the shape is immediately drawn on top of the previous
shapes' location - using its coorinates(instead of waiting for a new mouse
click and drawing the new shape at the new coordinates) .
Ps this is why I had the original x and y declarations - which I now know
were wrong anyway
Any further help appreciated
Ian

Scorpio said:
I have enclosed a working version of your code and put a comment (with my
name Scorpio) besides the lines I have changed.

Regards,

Scorpio


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;
int shape = Entity.CIRCLE; // scorpio - added here - defaults to
CIRCLE
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)
{
shape = Entity.SQUARE;
pp.drawEntity( Entity.SQUARE, x, y);
}
else if (source == circle)
{
shape = Entity.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)
{
x = me.getX(); // scorpio - got rid of re-declaration was (was int
x)
y = me.getY(); // scorpio - got rid of re-declaration was (was int
y)
pp.drawEntity( shape, x, y);

}

/** 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; }
}
}

In your actionPerformed() method, you should not call
pp.drawEntity(...).
You only want to set what the next drawing shape will be; the
pp.drawEntity() call atually draws the new shape, at the existing point
(x,y) which was set the last time the mouse was clicked in your drawing
area.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top