Results 1 to 6 of 6
Thread: CreateCustomPanel
- 10-21-2009, 01:52 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 19
- Rep Power
- 0
- 10-21-2009, 05:50 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Answer 1. No can do. JPanels are rectangles.
Answer 2. Here's how to get what you want:
Write each JPanel so it uses only the trapezoidal area you want it to draw.
To display them, they will be in a Container of some sort (say a JPanel or a JFrame).
Now set the LayoutManager for the Container to an adhoc manager that overlaps
the trapezoids as desired.
Example: suppose trapezoid a has width 100 and heights 200 and 300
and suppose trapezoid b has wdth 100 and heights 300 and 200.
A suitable LayoutManager could be:
The only notable feature of this code is that the y-coordinate for b (200)Java Code:class tlm implements LayoutManager { Dimension size = new Dimension(100,500); public void addLayoutComponent(String name, Component comp) {} public void removeLayoutComponent(Component comp) {} public Dimension preferredLayoutSize(Container parent) {return size;} public Dimension minimumLayoutSize(Container parent) {return size;} public void layoutContainer(Container parent) { a.setBounds(0,0,100,300); b.setBounds(0,200,100,300); } }
is above a's bottom (0+300). The assumption here is that the top edge of a is horizontal
and the top edge of b has the same slope as the bottom edge of a.
- 10-22-2009, 01:22 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 19
- Rep Power
- 0
CreateCustomPanel
Hello zweibieren,
Interesting idea.
If i am correct you recommend to make two JPanel objects (a and b)
and not specify size to them in advance.
Then to make new LayoutManager and add them to some swing
container. After that when i add a and b panels to that container i
should get trapezoid (with one pair of parallel sides)?
- 10-22-2009, 03:06 AM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
On reconsidering your original post, it may be that you want to produce a distorted image.
Something like letter sizes getting smaller as the text marches across the page.
Or a building drawn in perspective with a vanishing point off to the side.
If this is what you want, you can use Java Advanced Imaging (JAI)
https://jai.dev.java.net/ I am not familiar with this package,
but you may want the PerspectiveTransform or WarpPerspective classes.
(The first maps coordinates. The second preforms a warp on an image.)
An entirely different approach would be to have one large JPanelIf i am correct you recommend to make two JPanel objects (a and b)
and not specify size to them in advance.
Then to make new LayoutManager and add them to some swing
container. After that when i add a and b panels to that container i
should get trapezoid (with one pair of parallel sides)?
and just draw trapezoids wherever you want them.
I'm not sure that I got my point across,
so I'll try to answer each of the items in your last post:
"make two JPanel objects (a and b)"
Make as many JPanels as you need. My note assumes you want
trapezoidal JPanels because you want to nest them together.
If you just want to draw trapezoids there is no need for multiple JPanels.
I have assumed I understand why you need trapezoids;
but I may be wrong. I should have asked why you want trapezoids.
"and not specify size to them in advance."
Component sizes are always rather indirect.
They can be suggested by overriding getPreferredSize, etc.
Or they can be provided in an adhoc layout manager.
The final allocated size is always that assigned by the LayoutManager.
"Then to make new LayoutManager and add them to some swing container."
Yes. Layout the JPanels so they overlap. The trick is this:
The JPanels are rectangles; but each paints only within a trapezoid.
"when i add a and b panels to that container i should get trapezoid"
Again, each JPanel draws within whatever shape it likes.
This depends on the paintComponent() method that you write.
My proposed scheme does not automatically draw in trapzoidal shape.
- 10-22-2009, 10:07 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 19
- Rep Power
- 0
CustomPanel
Hello again,
What i really want to do is to make JPanel in trapezoid shape, so with parallel horizontal edges of different size. I don't need to nest trapezoidal panels together i only want to change shape to one panel (similar like getting panel with rounding edges). I assume
this can be done by extending original JPanel and implementing paintComponent method but i am not figure out how to do that yet.
Thanks.
- 10-22-2009, 01:58 PM #6
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Try Painting in AWT and Swing
Here is a paintComponent that draws a trapezoid:
The JPanel will be rectangular; the image it draws will be a trapezoid.Java Code:public void paint(Graphics g) { g.setColor(Color.RED); int [] xPoints = {0, 100, 100, 0}; int [] yPoints = {0, 20, 100, 40}; g.drawPolygon(xPoints, yPoints, xPoints.length); }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks