Results 1 to 6 of 6
  1. #1
    nenadm is offline Member
    Join Date
    Nov 2008
    Posts
    19
    Rep Power
    0

    Default CreateCustomPanel

    Hello,

    I want to create custom JPanel component that will have horizontal sides of different length in contrast to default JPanel which have Rectangle shape.
    Can anyone help me on how to do this (which methods to re-implement and how).

    Thanks.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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:
    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);
    	}	
    }
    The only notable feature of this code is that the y-coordinate for b (200)
    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.

  3. #3
    nenadm is offline Member
    Join Date
    Nov 2008
    Posts
    19
    Rep Power
    0

    Default 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)?

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    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.)


    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)?
    An entirely different approach would be to have one large JPanel
    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.

  5. #5
    nenadm is offline Member
    Join Date
    Nov 2008
    Posts
    19
    Rep Power
    0

    Default 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.

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Try Painting in AWT and Swing

    Here is a paintComponent that draws 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);
    }
    The JPanel will be rectangular; the image it draws will be a trapezoid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •