draw LegendTitle in JPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
JFreeChartUser
Posts: 9
Joined: Mon Sep 11, 2006 12:41 pm

draw LegendTitle in JPanel

Post by JFreeChartUser » Thu Jan 25, 2007 2:56 pm

I try to draw a LegendTitle in a JPanel.
The problem is: the legend text ist drifting away!!

Code: Select all

 public class LegendPanel extends JPanel 
  {   
    private LegendTitle mLegendTitle = null;
    
    
    public LegendPanel( LegendTitle inLegendTitle )
    {
       mLegendTitle = inLegendTitle;
    }
  
    public void paintComponent( Graphics gr ) 
    {
      super.paintComponent( gr );
      Graphics2D g2 = (Graphics2D)gr;  

      AffineTransform pos = new AffineTransform();
      pos.translate(5,10);
      g2.transform(pos); 
      
      Rectangle2D rect2d = new Rectangle();
      rect2d.setRect(0, 0, 100, 100);
      ((Drawable)mLegendTitle).draw( (Graphics2D)g2,  rect2d);
      gr.dispose();
    }
}
[/code]

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Jan 25, 2007 5:29 pm

The LegendTitle uses some fairly complex layout code. You need to call the arrange() method before you call the draw() method, and you need to draw into a rectangle that has the same dimensions as returned by the arrange() method. Take a look in the drawTitle() method of the JFreeChart class for an example of how it is done in practice.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

robdocmagic
Posts: 7
Joined: Wed May 17, 2006 12:46 pm

Post by robdocmagic » Thu Mar 29, 2007 3:24 pm

Did you ever get a chance to solve your issue with the drift? Is so, would you be so kind to post the JPanel code?

robdocmagic
Posts: 7
Joined: Wed May 17, 2006 12:46 pm

Post by robdocmagic » Thu Mar 29, 2007 3:48 pm

I think I got it.. it works, not sure if its right..

Code: Select all

class LegendPanel extends JPanel {
        private LegendTitle t = null;

        public LegendPanel(LegendTitle inLegendTitle) {
            t = inLegendTitle;
        }
        public void paintComponent(Graphics gr) {
            super.paintComponent(gr);
            Graphics2D g2 = (Graphics2D) gr;

            Rectangle2D titleArea = new Rectangle2D.Double();
            Rectangle2D area = new Rectangle2D.Double();
            RectangleEdge position = t.getPosition();
            double ww = area.getWidth();
            double hh = area.getHeight();
            RectangleConstraint constraint = new RectangleConstraint(ww, new Range(0.0, ww), LengthConstraintType.RANGE, hh, new Range(0.0, hh), LengthConstraintType.RANGE);
            Object retValue = null;
            BlockParams p = new BlockParams();
            Size2D size = t.arrange(g2, constraint);
            retValue = t.draw(g2, titleArea, p);
            gr.dispose();
        }
    }

Locked