shape selection again

I

Ian Stanley

Hello again.
Re-visiting my problem( of several months ) -
I need a way to detect what type of shape is selected from a vector of
shapes.
As suggested I have now posted some complete code. It is the shortest
version I can use to illustrate the problem.
The problem is that I want the shapes to be drawn in their own color but as
soon as you apply a transform the shape is no longer an instanceOf its
original tpye it is now an instanceOf a generalPath.
can anyone suggest a way to test what the shape(type) is when selected - it
will then be drawn in the appropriate color?
I have tried various combinations using the of the bounds of the objects but
have had no luck.
Thought of adding a vector with the shape type and setting the color
accordingly but would prefer a way to test what type of shape is being
selected - I have other more complex shape, manipulations and animations to
add.
Thanking you in advance.
Ian

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class rotateokorig
{
public static void main(String[] args)
{
Draw D = new Draw() ;
}
}
interface Constants {
final int RECTANGLE = 1, OVAL = 2, ROTATE = 3, SCALE = 4;
}
class Draw extends JFrame
implements Constants, ActionListener{
JPanel bp = new JPanel() ;
DrawJPanel djp ;
Container content = getContentPane() ;
JButton rectButton, ovalButton, rotateButton, quitButton, scaleButton
;
Draw() {
super() ;
content.setLayout(new BorderLayout()) ;
djp = new DrawJPanel() ;
bp.add(rectButton = new JButton("Rectangle")) ;
bp.add(ovalButton = new JButton("Oval")) ;
bp.add(rotateButton = new JButton("rotate")) ;
bp.add(scaleButton = new JButton("scale")) ;
bp.add(quitButton = new JButton("Quit")) ;
quitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent
e) {
System.exit(0) ;
}
}) ;
rectButton.addActionListener(this) ;
ovalButton.addActionListener(this) ;
rotateButton.addActionListener(this) ;
scaleButton.addActionListener(this) ;
content.add(bp, BorderLayout.NORTH) ;
content.add(djp, BorderLayout.CENTER) ;
setSize(750, 500) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand() ;
switch (cmd.charAt(0)) {
case 'R':
djp.setMode(RECTANGLE) ;
break ;
case 'O':
djp.setMode(OVAL) ;
break ;

case 'r':
djp.setMode(ROTATE) ;
break ;
case 's':
djp.setMode(SCALE) ;
break ;
}
}
}
class DrawJPanel extends JPanel
implements Constants
{
Vector v = new Vector();
Shape current_shape = null;
int drawingMode;
public DrawJPanel()
{
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
// check if we hit an existing shape
current_shape = locateRect(x,y);
// if ( current_shape==null)
// {
if (drawingMode == RECTANGLE){
Rectangle2D.Float e = new
Rectangle2D.Float(x, y, 80, 50);
v.add(e);
repaint();
}
if (drawingMode == OVAL){
Ellipse2D.Float e = new
Ellipse2D.Float(x, y, 80, 50);
v.add(e);
repaint();
}

else if (drawingMode == ROTATE &&
current_shape != null ){
rotateShape(x,y);
repaint();
}
else if (drawingMode == SCALE &&
current_shape != null ){
scaleShape(x,y);
repaint();
}
}
}
);
}
void setMode(int mode) {
drawingMode = mode ;
}

public Shape locateRect(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) )
return s;
}
return null;
}

public Shape rotateShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getRotateInstance(Math.toRadians(45), x, y);
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public Shape scaleShape(int x, int y)
{
for (int i = 0; i < v.size(); i++){
Shape s = (Shape)v.elementAt(i);
if (s.contains(x,y) ){
AffineTransform tr =
AffineTransform.getScaleInstance(0.7,0.7);
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(s);
repaint();
}
}
return null;
}
public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
for(int i=0; i<v.size(); i++){
/*******************************************************
*** if shape instanceOf Rectangle g.setColor(Color.blue);
*** if shape instanceOf Oval g.setColor(Color.red);
********************************************************/
g.draw((Shape)v.elementAt(i));
}
}
}
 
V

VisionSet

Ian Stanley said:
Hello again.
Re-visiting my problem( of several months ) -
I need a way to detect what type of shape is selected from a vector of
shapes.
As suggested I have now posted some complete code. It is the shortest
version I can use to illustrate the problem.
The problem is that I want the shapes to be drawn in their own color but as
soon as you apply a transform the shape is no longer an instanceOf its
original tpye it is now an instanceOf a generalPath.

No you can't change what it is an instance of.
A single instance will always be an instance of ALL of its superclasses and
any interfaces it or its superclasses implement.
can anyone suggest a way to test what the shape(type) is when selected - it
will then be drawn in the appropriate color?
I have tried various combinations using the of the bounds of the objects but
have had no luck.

If you extend the Shape interface and add getColour & setColour methods.
You can handle it as a Shape for drawing and get its colour for
g.setColor(). Just make all your concrete shapes implement this.

eg

interface ColourableShape extends Shape {
Color getColour();
void setColour();
}

Or you can take the colour out of the shape instance and wrap the shape in a
ColouredShape Object:

class ColouredShape {
Shape shape;
Color colour;
...
}

then use accessor methods to get what is required, when it is required
 
I

Ian

Thanks Mike,
Doesn't the same problem exist?
once you apply a transform to a Shape (eg rotate scale) it becomes a
grneralpath object.
perhaps I am not understanding(most likely), are there any examples
out there that illustrate what you mean
Thank you for tour reply.
Ian
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top