I have canvas with GraphicsContext on it and I am wondering if it is possible to format .fillText() method in any way?
GraphicsContext gc = getGraphicsContext2D();
gc.setTextAlign(TextAlignment.CENTER);
gc.setTextBaseline(VPos.CENTER);
gc.fillText(someString, Math.round(width / 2), Math.round(height / 2));
I want to make every character in someString with different color and keep this text centered
And it is not possible. This Canvas and Pane are resizable (Canvas width and height properties are binded to Pane width and height and Canvas is redrawn every time Pane size changes). So the question is, how can i create Text object or any other text object so it would be always in the center of Pane
Related
I have a JPanel with a JLabel in it, added to a JScrollPane. I have an actionListener that calls JLabel.setIcon("file.jpg");. The image is displayed in the JScrollPane correctly and is full size. The scrollbars appear perfectly. I am trying to position the vertical and horizontal scrollbars in the center by default so you are looking at the center of the image by default.
Is there a JScrollPane method that will position the viewport on the center of the image? Or could I manually set the position of each scrollbar to max size divided by 2?
I have tried
JScrollPane.getVerticalScrollBar().setValue(JScrollPane.getVerticalScrollBar().getMaximum() / 2);
While it compiles it does not center the scrollbar. I have also tried setting the layout manager of my JPanel to GridBagLayout but that doesn't work either.
Basically, you need to know the size of the viewport's viewable area.
Rectangle bounds = scrollPane.getViewport().getViewRect();
Then you need the size of the component, but once it's added to the scroll pane, you can get this from the view port...
Dimension size = scrollPane.getViewport().getViewSize();
Now you need to calculate the centre position...
int x = (size.width - bounds.width) / 2;
int y = (size.height - bounds.height) / 2;
Then you need to simply adjust the view port position...
scrollPane.getViewport().setViewPosition(new Point(x, y));
Now, remember, this is only going to work once the scroll pane has being realised on the screen (or at least it has being laid out within it's parent container)
I'm guessing that the image has not been read at the time you try to execute your code so try something like the following:
label.setIcon( new ImageIcon("...") );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Rectangle bounds = scrollPane.getViewport().getViewRect();
JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
horizontal.setValue( (horizontal.getMaximum() - bounds.width) / 2 );
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( (vertical.getMaximum() - bounds.height) / 2 );
}
});
This will add code to the end of the Event Dispatch Thread so hopefully it executes after the image has been read in completely and the scrollbar values have all been updated.
Add an AdjustmentListener and see if that helps. It will let you know if the value of the component changes. That way, when the image is added, the scroll bar's properties will change and you will be notified. You can then try to set the caret position to the mid.
Tutorial: http://examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/
I know Window.sizeToScene() will resize the window to the size that its scene needs, but the position of the window does not adjust accordingly (i.e. the stationary point is top-left corner of the window). Is there any way to make the window resize itself, and keep the window's center at the same place (i.e. make the stationary point at the center of the window)?
Do something like:
public void resize(Window win) {
double x = win.getX();
double y = win.getY();
double width = win.getWidth();
double height = win.getHeight();
win.sizeToScene();
win.setX(x + ((width - win.getWidth()) / 2));
win.setY(y + ((height - win.getHeight()) / 2));
}
The code above caches the position before the window is resized to the scene, then it moves the window the appropriate amount to keep the window centered in the same area. This code does not take into account where the window will be once it is moved/resized. You might want to add checks to make sure the window doesn't end up going off the screen.
I have a JPanel with a JLabel in it, added to a JScrollPane. I have an actionListener that calls JLabel.setIcon("file.jpg");. The image is displayed in the JScrollPane correctly and is full size. The scrollbars appear perfectly. I am trying to position the vertical and horizontal scrollbars in the center by default so you are looking at the center of the image by default.
Is there a JScrollPane method that will position the viewport on the center of the image? Or could I manually set the position of each scrollbar to max size divided by 2?
I have tried
JScrollPane.getVerticalScrollBar().setValue(JScrollPane.getVerticalScrollBar().getMaximum() / 2);
While it compiles it does not center the scrollbar. I have also tried setting the layout manager of my JPanel to GridBagLayout but that doesn't work either.
Basically, you need to know the size of the viewport's viewable area.
Rectangle bounds = scrollPane.getViewport().getViewRect();
Then you need the size of the component, but once it's added to the scroll pane, you can get this from the view port...
Dimension size = scrollPane.getViewport().getViewSize();
Now you need to calculate the centre position...
int x = (size.width - bounds.width) / 2;
int y = (size.height - bounds.height) / 2;
Then you need to simply adjust the view port position...
scrollPane.getViewport().setViewPosition(new Point(x, y));
Now, remember, this is only going to work once the scroll pane has being realised on the screen (or at least it has being laid out within it's parent container)
I'm guessing that the image has not been read at the time you try to execute your code so try something like the following:
label.setIcon( new ImageIcon("...") );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Rectangle bounds = scrollPane.getViewport().getViewRect();
JScrollBar horizontal = scrollPane.getHorizontalScrollBar();
horizontal.setValue( (horizontal.getMaximum() - bounds.width) / 2 );
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( (vertical.getMaximum() - bounds.height) / 2 );
}
});
This will add code to the end of the Event Dispatch Thread so hopefully it executes after the image has been read in completely and the scrollbar values have all been updated.
Add an AdjustmentListener and see if that helps. It will let you know if the value of the component changes. That way, when the image is added, the scroll bar's properties will change and you will be notified. You can then try to set the caret position to the mid.
Tutorial: http://examples.javacodegeeks.com/desktop-java/awt/event/adjustmentlistener-example/
How can I get the size of a component when using a layout for my frame?
I can just note that I'm using a BorderLayout for a JFrame and I want the size of a JPanel.
You can try the JPanel methods, getWidth() and getHeight():
int getWidth();
int getHeight();
They will return the height and width of the JPanel.
You can also get the bounds of the JPanel using the method getBounds():
Rectangle getBounds();
It will return a Rectangle of the bounds. You can use it to get the location and size of the JPanel.
I'm starter with Canvas and Paint. I want to paint a text in a Canvas but it can be longer than the original Bitmap. So the text go out the Bitmap.
Is there some kind of automatic manager for this making a new line when the end is reached? or should I play with heights and distances? Thanks
yes, you can manage this with a StaticLayout or DynamicLayout
The best way is to draw text with StaticLayout:
// init StaticLayout for text
StaticLayout textLayout = new StaticLayout(
gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// get height of multiline text
int textHeight = textLayout.getHeight();
// get position of text's top left corner
float x = (bitmap.getWidth() - textWidth)/2;
float y = (bitmap.getHeight() - textHeight)/2;
// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
canvas.restore();
See my blogpost for more details.
I would suggest that you also look at this code snippet found here:
https://stackoverflow.com/a/15092729/1759409
As it will manage the writing of your text within a certain width and height and automatically draws onto the canvas correctly.