paint gui problem2

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
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
intx)
y = me.getY(); // scorpio - got rid of re-declaration was (was
inty)
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; }
}
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top