JFreeChart Vertical Label for IntervalMarker - java

I would like to rotate the label for the IntervalMarker:
IntervalMarker im = new IntervalMarker(...);
im.setLabel("LABEL");
// im.setLabelOffsetType(LengthAdjustmentType.EXPAND);
// im.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
// im.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
im.setLabelAnchor(RectangleAnchor.TOP_LEFT);
im.setLabelTextAnchor(TextAnchor.TOP_LEFT);
im.setPaint(new Color(208, 194, 214));
It didn't allow me to upload a picture, here is the link:
http://i54.tinypic.com/5z40fs.png
I would like to have "LABEL" vertical, for better looks.
Thank you

I've found it easier to add Text Annotations to vertical markers for more control over their labels. Here's an example:
// vertical line marker and label
Marker updateMarker = new ValueMarker(dayOf, Color.black, dashedStroke, null, null, 1.0f);
XYTextAnnotation updateLabel = new XYTextAnnotation("Update", dayOf - labelOffset, labelHeight);
updateLabel.setFont(new Font("Sans Serif", Font.BOLD, 10));
updateLabel.setRotationAnchor(TextAnchor.BASELINE_CENTER);
updateLabel.setTextAnchor(TextAnchor.BASELINE_CENTER);
updateLabel.setRotationAngle(-3.14 / 2);
updateLabel.setPaint(Color.black);
plot.addDomainMarker(updateMarker, Layer.BACKGROUND);
plot.addAnnotation(updateLabel);
This rotation makes the label appear on the left side of the vertical marker line reading bottom-to-top. I use variables "labelOffset" and "labelHeight" to determine the exact position of the label in relation to the vertical line, but these could be set statically as well.

Rendering the marker's label is handled by the draw{Domain|Range}Marker() method of the plot's renderer using drawAlignedString(). You'd have to use drawRotatedString(), instead.

I couldn't find how to do this directly, but you can always put a rotated text Annotation in an appropriate place.

Related

Java Swing - JButton wrong border

I try to set new red border to my "Ok" button, but instead of I get from this. How to make 1-st picture? (it's photoshop)
I try
button.setBorder(new LineBorder(Color.RED, 1));
You overrided the button's default border. You should set a compound border using the existing one and a new red one.
Border innerBorder = button.getBorder();
Border outerBorder = new LineBorder(Color.RED, 1);
button.setBorder(new CompoundBorder(outerBorder, innerBorder));
Here is a one liner combined with using the BorderFactory :-)
button.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.RED, 1),
button.getBorder()));
Using BorderFactory - http://docs.oracle.com/javase/tutorial/uiswing/components/border.html#createapi

JPanel how to add a ToolTipText

I have a little problem, I need to add a ToolTipText to JPanel. How should I do this?
I want to have a tooltip when I have mouse over the circle.
This is part of my code.
JPanel component1 = new JPanel();
JPanel component11 = new JPanel();
okno.add(component1,"align left,cell 0 0, h 75!, grow,wrap");
component1.setLayout(new MigLayout("","[][grow][grow]", "[grow]"));
component1.add((okno.add(creLab("Kraj", i, czcionka, etykietki))),"left align, cell 0 0");
component1.add(t1,"cell 1 0,grow");
//component1.add(new circle1(),"right align, cell 2 0,h 50!, w 53!, gapleft 50, wrap");
component1.add(component11," right align, cell 2 0, h 30!, gapleft 300, wrap");
component11.setLayout(new MigLayout("","[]","[]"));
component11.add(new circle1(),"cell 0 0,h 50!, w 50!, dock north");
component11.setToolTipText("<html>W polu obok wpisz kraj pochodzenia towaru</html>");
I add also code of circle1:
class circle1 extends Applet{
public void paint(Graphics g){
setForeground(Color.yellow);
g.drawOval(0, 0, 50, 50);
g.fillOval(0, 0, 50, 50);
g.setColor(Color.black);
g.drawString("Jak", 14, 14);
g.drawString("wpisac", 3, 28);
g.setColor(Color.red);
g.drawString("kraj?", 14, 42);
//g.drawString(arg0, arg1, arg2)
}
}
Take a look at JComponent#getToolTipText(MouseEvent)
This will allow you to determine what text to return based on the location of the mouse.
It's difficult to determine for your code snippet, exactly where the circle is been drawen, but I would avoid drawing directly to the surface of the applet, but instead use a custom component (like a JPanel) instead (overriding its paintComponent method). This I would then either add to the applet or to the control panel.
This way your going to avoid issues with the mouse events been consumed
I would also take a look at Ellipse2D, which can be used to determine if the ellipse contains a given point
The first thing is to identify when the mouse is inside the circle. To do that you could verify the mouse position on a mouseMotionlister according to the circle area
http://www.java2s.com/Code/JavaAPI/javax.swing/JPaneladdMouseMotionListenerMouseMotionListenerlis.htm
Once you identify this situation you could proceed to change the tooltip
See Playing With Shapes. You can create a JLabel with a ShapeIcon. Then you just use the setToolTipText() method of the JLabel. You can then add the label to the panel like any other component.
Now that you can use a component to represent a Shape there is no need to do custom painting. Just create a panel add add components to the panel. You can also create JLabels for all your text strings.
Don't do custom painting, unless you have a good reason to do so.

Java Swing - setting margins on TextArea with Line Border

As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.
aboutArea = new JTextArea("program info etc.....");
Border border = BorderFactory.createLineBorder(Color.BLACK);
aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));
add(aboutArea);
I have tried each of these:
aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));
but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?
What if you try adding a CompoundBorder , won't this do, this will give you almost same thing
JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));

JFreeChart - wrong chart font

I have a problem with XYLineChart. I don't know how to set the chart font to look like Swing components. When I use this:
chart.setTitle(new TextTitle("Tahoma title, style plain, size 11", new Font("Tahoma", Font.PLAIN, 11)));
It is still wrong :(
EDIT: When I create the chart in a new frame, the font in the title is good. How do I set all the labels, axis titles, and other texts to the same font size, without bold?
SOLVED :)
public static void changeStyle(JFreeChart chart) {
final StandardChartTheme chartTheme = (StandardChartTheme)StandardChartTheme.createJFreeTheme();
final Font font = new Font("Tahoma", Font.PLAIN, 11);
final Color color = new Color(0, 0, 0);
chartTheme.setExtraLargeFont(font);
chartTheme.setLargeFont(font);
chartTheme.setRegularFont(font);
chartTheme.setSmallFont(font);
chartTheme.setAxisLabelPaint(color);
chartTheme.setLegendItemPaint(color);
chartTheme.setItemLabelPaint(color);
chartTheme.apply(chart);
}
If you want to change the existing title's font, do something like this:
chart.getTitle().setFont(new Font("Tahoma", Font.PLAIN, 11));
Addendum:
How do I change all items' fonts, e.g. labels, axis, etc.?
StandardChartTheme offers this capability by operating on individual chart components.
I don't know 2 things about XYLineCharts, but it looks like you might be setting the font on the wrong thing. Try setting it on different components.
Not much, but might get you going.
Good luck ;)
Damo

Java: Fonts and Pixels

I'm making a game and in the menu I want to display the text in the center of the screen.
Is there a way in Java to get/calculate the width of a piece of text in a specified font with specified size and style.
Martijn
The FontMetrics.stringWidth method does just that -- it will return the width in pixels for a given String.
One can obtain the FontMetrics from a Graphics object by the getFontMetrics method.
For example:
g.setFont(new Font("Serif", Font.BOLD, 24));
int width = g.getFontMetrics().stringWidth("Hello World!");
System.out.println(width);
The result was:
135
In the class Font you have methods such like getLineMetrics or getStringBounds that may help you.
Just use a JLabel that is center aligned and the proper layout manager and you don't have to worry about this.
JLabel label = new JLabel("Text");
frame.add(label , SwingConstants.CENTER);

Categories