getbounds2D compiler error??

I

Ian Stanley

hi,
continuing a long standing problem of mine(cutdown version code below) - I
am getting a compiler error which tells me that:
MyShape should be declared abstract; it does not define getbounds2D() in
myShape
However, I have tried defining the specified method to return a rectangle
but can't seem to get it right as just get more errors and don't know where
to go from here. Also, declaring the class abstract does not work in this
case as the problem moves to the sub-class
I need to keep this class strucure as I have many more methods & classes
which perform various operations etc.
The idea in this example is to draw rectangles and after selecting a rotate
mode button and clicking inside adrawn rectangle this shape is rotated
any help appreciated as to what I have done wrong
regards
Ian

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class rot2
{
public static void main(String[] args)
{
Draw D = new Draw() ;
}
}
interface Constants {
final int RECTANGLE = 1, ROTATE = 2, w = 100, h = 75;
}
class Draw extends JFrame
implements Constants, ActionListener{
JPanel bp = new JPanel() ;
DrawJPanel drawPanel ;
Container content = getContentPane() ;
JButton rectButton, rotateButton, quitButton;
Draw() {
super() ;
content.setLayout(new BorderLayout()) ;
drawPanel = new DrawJPanel() ;
bp.add(rectButton = new JButton("Rectangle")) ;
bp.add(rotateButton = new JButton("rotate")) ;
bp.add(quitButton = new JButton("Quit")) ;
quitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent
e) {
System.exit(0) ;
}
}) ;
rectButton.addActionListener(this) ;
rotateButton.addActionListener(this) ;
content.add(bp, BorderLayout.NORTH) ;
content.add(drawPanel, BorderLayout.CENTER) ;
setSize(750, 500) ;
setVisible(true) ;
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand() ;
switch (cmd.charAt(0)) {
case 'R':
drawPanel.setMode(RECTANGLE) ;
break ;
case 'r':
drawPanel.setMode(ROTATE) ;
break ;
}
}
}
class DrawJPanel extends JPanel
implements Constants
{
Vector v = new Vector();
MyShape current_shape;
int mode;
int newMode;
private int x1, y1, x2, y2;
public DrawJPanel()
{
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
if (mode == RECTANGLE){
v.add(new Rectangle(x, y, w, h));
repaint();
}
else if (mode == ROTATE){
if(findMyShape(x,y)!=false){
rotateMyShape(current_shape,x,y);
repaint();
}
}
}
}
);
}
void setMode(int newMode) {
mode =newMode ;
}
boolean findMyShape(int x, int y)
{
for (int i = v.size()-1; i >= 0; i-- ) // start at end of Vector
{
MyShape s = (MyShape)v.elementAt(i);
if( s.contains(x, y) )
{
current_shape = s;
return true;
}
}
return false;
}
public MyShape rotateMyShape(MyShape current_shape, int x, int y)
{
AffineTransform tr =
AffineTransform.getRotateInstance(Math.toRadians(45), x, y);
Shape ts = tr.createTransformedShape(current_shape);
v.addElement(ts);
v.removeElement(current_shape);
repaint();
return current_shape;
}

public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
for(int i=0; i<v.size(); i++)
g.draw((MyShape)v.elementAt(i));
}
}
class MyShape extends JComponent implements Shape {
int x, y ;
int w, h ;
MyShape(int x, int y, int w, int h) {
super() ;
this.x = x ;
this.y = y ;
this.w = w ;
this.h = h ;
}
}
class Rectangle extends MyShape {
Rectangle(int x, int y, int w, int h) {
super(x, y, w, h) ;
}
public void paint(Graphics g) {
g.setColor(Color.BLUE) ;
g.drawRect(x, y, w-1, h-1) ;
}
}
 
S

Sudsy

Ian Stanley wrote:
class MyShape extends JComponent implements Shape {

There are 10 methods you'll have to provide if you want to
implement Shape. They're all listed in the javadocs. Some
of them are complex (e.g. getPathIterator) so you might
want to look at the code of someone who has travelled this
particular path before you.
 
V

VisionSet

Ian Stanley said:
hi,
continuing a long standing problem of mine(cutdown version code below) - I
am getting a compiler error which tells me that:
MyShape should be declared abstract; it does not define getbounds2D() in
myShape
However, I have tried defining the specified method to return a rectangle
but can't seem to get it right as just get more errors and don't know where
to go from here. Also, declaring the class abstract does not work in this
case as the problem moves to the sub-class
I need to keep this class strucure as I have many more methods & classes
which perform various operations etc.
The idea in this example is to draw rectangles and after selecting a rotate
mode button and clicking inside adrawn rectangle this shape is rotated
any help appreciated as to what I have done wrong
....

You must implement all the methods of the Shape interface.
If you all you want is a Rectangle, there is no need as java.awt.Rectangle
already implements Shape, there is no need to reinvent the wheel.

If you have other shapes look at the Shape interface in the docs to see
which classes implement this interface. Have a look at them all and pick
one that your shapes can inherit from. But only if they truly have a 'is a'
relationship

ie if you go for java.awt.Polygon make sure any subclasses truly are a
Polygon.
If you can't find one to fit the bill you will have to implement Shape
yourself and provide all the methods, you may be able to delegate calls via
composition to ease the task.
 
A

Andrew Thompson

Ian Stanley said:
hi,
continuing a long standing problem of mine(cutdown version code below) - I
am getting a compiler error which tells me that:
MyShape should be declared abstract; it does not define getbounds2D() in
myShape .....
I need to keep this class strucure as I have many more methods & classes
which perform various operations etc.

Wait. Let's think about that for a moment.

Your other 'methods and classes which
perform various operations' can not be
doing much if they rely on this code that
does not compile, right?
The idea in this example is to draw rectangles and after selecting a rotate
mode button and clicking inside adrawn rectangle this shape is rotated
any help appreciated as to what I have done wrong
regards
Ian

Your example is excellent, but you
need to adjust some things..
public class rot2

by convention, class names start with capital
letters, they should also be meaningful..
In this case, I would recommend getting rid of
this class entirely and putting the main inside...
class Draw extends JFrame

here.. But 'Draw' is a terrible name for _this_
class, it has a capital and follows the convention,
but it is vague and ill defined, as well as being
too similar to a number of words central to java.

Further, class names are supposed to be
nouns, not verbs, so this might better be
called RectangularBoxFrame
class DrawJPanel extends JPanel
RectangularBoxPanel..

class MyShape extends JComponent implements Shape {

Again 'MyShape' ..it reminds me of the terrible two's,
the 'I, me, mine' phase of the 24 month old.

'ImplementedShape', 'ConcreteShape' .. both bad
names, but either is better than 'MyShape'..

And, of course, you need to implement the
methods to which the compiler referred.
class Rectangle extends MyShape {

change this.. let us not confuse it with..
java.awt.Rectangle, which is also in the namespace.

Actually, I think this may be the base
cause of why you are going around in circles,
(though I might have missed something in the code)

Clealy distinguish thos from java.awt.Rectangle,
change the rest of the class names, and see how
it comes out.

HTH
 
I

Ian

Thanks for your replies.
my original program does not use inheritance for the shapes but this
is where I am having problem understanding.
As suggested I have tried fixing the problem by extending rectangle2D
but am obviusly not doing it int the right place.
Can anyone please suggest how to do this so I get all the methods from
the class as I need to add many more shapes - circles, triangles etc
and many more manipulations - scales, translates etc but I need to at
least get it working from this point.
please forgive me as I am self learning and have been posting this
problem for many months.
regards
Ian.
 
A

Andrew Thompson

Ian said:
Thanks for your replies.

Please do not top-post Ian, it makes
conversations a mess.
my original program does not use inheritance for the shapes but this
is where I am having problem understanding.

You need to understand inhheritance (at least
a little) before you can understand the API docs,
and you need to understnad the API docs
(or how to get information from them) to
get very far in java.
As suggested I have tried fixing the problem by extending rectangle2D
but am obviusly not doing it int the right place.

In secret code you are not showing us..
that is definitely the wrong place.
Perhaps you also need to check-out..
http://www.yoda.arachsys.com/java/newsgroups.html
...The second part.
Can anyone please suggest how to do this so I get all the methods from
the class

API docs and understanding how inheritance
works, try search java.sun,com and mindprod.com
for 'inheritance' and that'll give you some
starting points..
please forgive me as I am self learning and have been posting this
problem for many months.

To get quicker answers you need may to
alter the way you post.

You seem to be more at the stage
of comp.java.lang.help than
comp.java.lang.programmer, perhaps
you should consider psoting there instead

Also note that trimming any text that you are
not replying to, is very helpful as well (and
considered polite).
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top