Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2004

Tip: Looking for answers? Try searching our database.

Scale Question (Graphics2D)

Thread view: 
Andreas Beresko - 28 Jul 2004 19:24 GMT
Hi,

I use a JPanel as a Canvas to draw some shapes. The Panel is embedded in an
JScrollPane.
When I scale the canvas ( i do this in the paint() - method by using scale()
method) my drawing zoomes like i want it. But the drawing is bigger than
the visible area. The problem is, that no scrolling bars appear. I thought,
a JScrollPane enables them if the client in the viewport becomes bigger than
the visible area?! Where's my error?
And second problem: I recognize the shapes when I click on them using
mousePressed method. If I don't scale the drawing everything works fine, but
when
i scale it an then click in a shape, it's not recognized. It seems as if i
should "scale" the mouse coordinates too, but how to do?!

best regards Andreas
Tor Iver Wilhelmsen - 28 Jul 2004 19:58 GMT
> When I scale the canvas ( i do this in the paint() - method by using scale()
> method) my drawing zoomes like i want it. But the drawing is bigger than
> the visible area. The problem is, that no scrolling bars appear. I thought,
> a JScrollPane enables them if the client in the viewport becomes bigger than
> the visible area?! Where's my error?

You also need to resize the JPanel. Java has no problems drawing
outside the bounds of a component, and scaling (and other transforms)
in Java2D only affects the Shape drawn.
Andreas Beresko - 28 Jul 2004 20:20 GMT
"Tor Iver Wilhelmsen" <tor.iver.wilhelmsen@broadpark.no> schrieb :

> You also need to resize the JPanel. Java has no problems drawing
> outside the bounds of a component, and scaling (and other transforms)
> in Java2D only affects the Shape drawn.

I already tried this, but no success... maybe another problem? I'll give you
code...

Canvas canvas = new Canvas();
JScrollPane canvasScroll = new JScrollPane(canvas);
mainWindow.getContentPane().add(BorderLayout.CENTER,canvasScroll);

class Canvas extends JPaenl{

   public Canvas(){
       setSize(10000,1000);  // <- I think this should be enough to show
scroll bars ?!
   }
}
Tor Iver Wilhelmsen - 29 Jul 2004 09:31 GMT
>         setSize(10000,1000);  // <- I think this should be enough to show
> scroll bars ?!

Except when using layout managers - including the very custom one in
JScrollPane - they will resize as they see fit.

As another poster pointed out, there are other methods in Swing to set
the min, max and preferred sizes.
ak - 28 Jul 2004 20:03 GMT
> Hi,
>
[quoted text clipped - 5 lines]
> a JScrollPane enables them if the client in the viewport becomes bigger than
> the visible area?! Where's my error?

the problem is, that you scale Graphics, canvas size is unchanged.

> And second problem: I recognize the shapes when I click on them using
> mousePressed method. If I don't scale the drawing everything works fine, but
> when
> i scale it an then click in a shape, it's not recognized. It seems as if i
> should "scale" the mouse coordinates too, but how to do?!

the question is what you should scale? you could
a) apply scale to all your shapes,
or
b) scale graphics, change size of canvas, put glassPane over canvas and
scale mouse events (I didn't tested it)

Signature

Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader

Roedy Green - 28 Jul 2004 20:05 GMT
> I thought,
>a JScrollPane enables them if the client in the viewport becomes bigger than
>the visible area?! Where's my error?

What does getSize say?

You probably have not implemented getMinimumSize getMaximumSize
getPreferredSize.

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green - 28 Jul 2004 20:07 GMT
>i scale it an then click in a shape, it's not recognized. It seems as if i
>should "scale" the mouse coordinates too, but how to do?!

And Dale thinks this is where we should START the poor newbies!

see http://mindprod.com/jgloss/affinetransform.html#MOUSE

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Andreas Beresko - 28 Jul 2004 22:56 GMT
"Roedy Green" <look-on@mindprod.com.invalid> schrieb:
> And Dale thinks this is where we should START the poor newbies!
> see http://mindprod.com/jgloss/affinetransform.html#MOUSE

And this works really very fine, thanks a lot :-)
Chris Smith - 28 Jul 2004 20:13 GMT
> I use a JPanel as a Canvas to draw some shapes. The Panel is embedded in an
> JScrollPane.
[quoted text clipped - 3 lines]
> a JScrollPane enables them if the client in the viewport becomes bigger than
> the visible area?! Where's my error?

The scale method only affects the results of the paint operation.  What
you want to do is change the result of getPreferredSize, and then call
revalidate() on the component.  That should be done at whatever point
you make the change that will cause the scale factor to increase.

Incidentally, you should be overriding paintComponent to draw on a
JPanel, not paint.  And then, unless you intend to overwrite every pixel
of the contents, you should start the whole process with a call to
super.paintComponent as well.

> And second problem: I recognize the shapes when I click on them using
> mousePressed method. If I don't scale the drawing everything works fine, but
> when
> i scale it an then click in a shape, it's not recognized. It seems as if i
> should "scale" the mouse coordinates too, but how to do?!

You can scale the mouse coordinates by multiplying the x coordinate by
the horizontal scale factor, and the y coordinate by the vertical scale
factor.  These scale factors are the values you pass into the
Graphics2D.scale method.

If you start doing more complex transformations than just scaling, then
you ought to save the AffineTransform object that represents your
transformations.  Then whenever you need to do a transformation, use one
of the appropriate transform methods on that AffineTransform object.  At
the beginning of paintComponent, you can call g2d.setTransform to reuse
the same AffineTransform for painting as well.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Andreas Beresko - 28 Jul 2004 20:23 GMT
"Chris Smith" <cdsmith@twu.net> schrieb:
> The scale method only affects the results of the paint operation.  What
> you want to do is change the result of getPreferredSize, and then call
> revalidate() on the component.  That should be done at whatever point
> you make the change that will cause the scale factor to increase.

Ah ok, when I use setPrefferedSize it works...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.