Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic A resizeToFitText problem (Read 5214 times)
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
A resizeToFitText problem
Dec 18th, 2007 at 3:59am
Print Post  
Hi,

I have the following code:

[code]
package research;

import java.awt.Font;
import java.awt.geom.Rectangle2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import app.Util.Utility;
import app.Util.SAXTConstants.LocalizationConstants;

import com.mindfusion.jdiagram.Align;
import com.mindfusion.jdiagram.ArrowStyle;
import com.mindfusion.jdiagram.Box;
import com.mindfusion.jdiagram.FitSize;
import com.mindfusion.jdiagram.FlowChart;
import com.mindfusion.jdiagram.GraphicsUnit;
import com.mindfusion.jdiagram.RerouteArrows;
import com.mindfusion.jdiagram.TextFormat;

public class Goop extends JApplet {
private static final long serialVersionUID = 1L;

@Override
public void init() {
super.init();

flowChart = new FlowChart();

resizeTest();
}

private void initialize() {
flowChart.setSelectionOnTop(false);

flowChart.setArrowStyle(ArrowStyle.Cascading);
flowChart.setRouteArrows(true);
flowChart.getRoutingOptions().setTriggerRerouting(
RerouteArrows.WhileCreating | RerouteArrows.WhenModified);

flowChart.setMeasureUnit(GraphicsUnit.Pixel);

TextFormat textFormat = new TextFormat(Align.Center, Align.Near);
textFormat.setWrapAtCharacter(true);
flowChart.setTextFormat(textFormat);
}

private void resizeTest() {
flowChart = new FlowChart();
initialize();

JScrollPane pane = new JScrollPane(flowChart);
this.getContentPane().add(pane);
flowChart.setDocBounds(new Rectangle2D.Float(0, 0, 64000, 64000));

Box four = new Box(flowChart);
four.setFont(new Font("Arial", 10, 10));

JTextArea area = new JTextArea(5, 10);
JScrollPane pane2 = new JScrollPane(area);

int result =
JOptionPane
.showOptionDialog(
Utility.getApplet(this),
new Object[] { "Title", pane2 },
Utility
.getLocalizedString(LocalizationConstants.INPUT),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (result == JOptionPane.OK_OPTION) {
String symbolName = area.getText();
four.setText(symbolName);
}
four.setBounds(150, 150, 100, 100);

sizeUp(four, flowChart);
flowChart.add(four);
}

private FlowChart flowChart;

public void sizeUp(Box box, FlowChart canvas) {
Box sizerUp = new Box(canvas);

sizerUp.setText(box.getText());

sizerUp.setFont(box.getFont());

Rectangle2D.Float stringSize =
canvas.measureString(sizerUp.getText(), sizerUp.getFont());

stringSize.width = stringSize.width;
stringSize.height = stringSize.height + 1;

sizerUp.setBrush(box.getBrush());
sizerUp.setPen(box.getPen());
sizerUp.setBounds(stringSize.getBounds());

sizerUp.resizeToFitText(FitSize.KeepWidth);
sizerUp.resizeToFitText(FitSize.KeepHeight);

Rectangle2D.Float newBounds = sizerUp.getBounds();

box.resize(newBounds.width, newBounds.height);
}

}

[/code]

I have a method written called sizeUp that resizes the box to fit the text it is given.
When the applet starts, if I pass this string:
[code]
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[/code]

it works alright.

If I pass a very long string (>1000 characters) as this:
[code]
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[/code]
the resizeToFitText doesn't seem to work fine.

Should my logic there change, or does resizeToFitText have problem with large text?

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: A resizeToFitText problem
Reply #1 - Dec 18th, 2007 at 6:21am
Print Post  
Hi,

Does resizeToFitText return false? And what parameter are you passing to it?

Stoyan
  
Back to top
 
IP Logged
 
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
Re: A resizeToFitText problem
Reply #2 - Dec 19th, 2007 at 4:17am
Print Post  
Hi Stoyan,

I call resizeToFitText first with KeepWidth and the with KeepHeight, as you can see from the sample code that I have pasted.

When I pass the small multiline text, both the resizeToFitText calls return true.

When I pass the two line text with > 1000 chars, both the calls are returning false.

So, the function cannot handle large text?

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: A resizeToFitText problem
Reply #3 - Dec 19th, 2007 at 6:32am
Print Post  
Hi Praveen,

From what I can see in the resizeToFit code, the function returns false when using the KeepWidth/Height parameters but the text cannot fit in the current width or height.

You are setting the result from measureString as initial size of the box, but it might not be enough because the control includes some margin space and the pen width in its fitSize calculations. In addition, the call of a single measureString might differ slightly from the sum of single-character measurements performed by NetDiagram. Perhaps this will work if you inflate stringSize a bit, e.g. add pen.getWidth() + 10points to both the width and height.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
Re: A resizeToFitText problem
Reply #4 - Dec 27th, 2007 at 3:57am
Print Post  
Hi,

I have tried what you suggested. I added the following code before calling the resize methods.

[code]
stringSize.width = stringSize.width + box.getPen().getWidth() + 10;
stringSize.height = stringSize.height + box.getPen().getWidth() + 10;
[/code]

The problem still exists though. The functions still return false. Is there something else I must be doing?

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: A resizeToFitText problem
Reply #5 - Dec 27th, 2007 at 6:03am
Print Post  
Is the sizerUp shape a rectangle?

Stoyan
  
Back to top
 
IP Logged
 
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
Re: A resizeToFitText problem
Reply #6 - Dec 27th, 2007 at 9:14am
Print Post  
Hi,

I haven't set any explicit shape. So, I assume it is rectangle.

In the above example (with the Goop class), I have given the entire code so that you can see it. I only introduced some uncompilable code in "JOptionPane
.showOptionDialog(...".
Maybe you can change that and see for yourself this thing in action.

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: A resizeToFitText problem
Reply #7 - Dec 28th, 2007 at 8:36am
Print Post  
Hi,

At this time measureString can not handle multiline text. Use this method instead:

Code
Select All
public Rectangle2D.Float measureString(String text, Font font)
{
	Graphics2D g2 = (Graphics2D)getGraphics();

	g2.setTransform(getScreenTransform());

	FontMetrics metrics = g2.getFontMetrics(font);

	Rectangle2D.Float textSize = new Rectangle2D.Float(0, 0, 0, 0);
	String[] lines = text.split("\n");
	for (String line : lines)
	{
		textSize.width = Math.max(textSize.width, metrics.stringWidth(line));
		textSize.height += font.getSize2D();
	}

	g2.dispose();

	return textSize;
}
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint