2

I'm looking for a class in javax.swing or java.awt that gives flexible size to the object. Say that we name this class MyClass. When I add MyClass to MyPanel, it will be shaped with the objects that lie within it. To be more specific, if I draw a Rectangle, MyClass's size will be that of rectangle's, if I draw a circle, MyClass's size will be that of circle's, etc.

So far I've seen some classes but all are rectangular in shape: Container, Canvas, JLayeredPane, etc.

Any suggestions?

RMT
  • 7,040
  • 4
  • 25
  • 37
Emir
  • 762
  • 2
  • 8
  • 22

3 Answers3

3

Given a suitable layout, any class that implements the Shape interface is a good candidate. You can use the createTransformedShape() method of AffineTransform to transform it, as seen in this example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This sounds great. I am planning to put a line+string in this thing. It should be simpler, eh? Do you think I need AffineTransform and matrix algebra? – Emir Jul 15 '11 at 21:23
  • in generally for nicest and seftsold GUI and 2D/3D painting you have to buil-in top-level artist feel, sence make think better, nicest and s*e*x*y as other :-), excelent algebra is good base for that, both +1 – mKorbel Jul 15 '11 at 21:36
  • `AffineTransform` hides most of the algebra; but remember, "rotation and translation are not commutative!"—[Geometric Transformations](http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/geometry/geo-tran.html) – trashgod Jul 16 '11 at 01:01
2

You can simply use another JPanel, but the key is in what layout managers that the containers (perhaps another JPanel) use and how the components are placed into the containers -- with what constraints, if any. For more on this, please read the tutorials on how to use the various more user-friendly layout managers: Laying Out Components Within a Container.

Now as to your second issue, that of drawing shapes, you'll have to give more information to solve it. Are the curves allowed limitless? Or are just rectangles and ovals allowed?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Simply, my object will compose of a line (its constraints can dynamically change with MouseEvents) and a string. – Emir Jul 15 '11 at 21:24
1

by follows this tutorial I can image that by creating un-decorated fully translucent JFrame / JDialog, my favorite would be just JWindow which is un-decorated by default(heavens this Container lost from Swing tutorial!, or am I blind???)

to this fully translucent you have to add parent JPanel or better would be JLabel. There you should adds any of possible sharpes

I think that you can pretty to forgot for usage of AWT Components, all possible start with "J" == Swing JComponents

mKorbel
  • 109,525
  • 20
  • 134
  • 319