IllegalArgumentException when setting Image in Jigloo - java

I am trying to make GUIs in SWT using Jigloo and when I want to set images to Labels or Buttons, I can see them in the preview pane. However, when i compile and run it, it throws
SWTResourceManager.getImage: Error getting image img/game/front/test.png, java.lang.IllegalArgumentException: Argument cannot be null
The code it generates looks like this:
opIcon = new Label(this, SWT.NONE);
FormData opIconLData = new FormData();
opIconLData.width = 64;
opIconLData.height = 64;
opIconLData.left = new FormAttachment(0, 1000, 12);
opIconLData.top = new FormAttachment(0, 1000, 12);
opIcon.setLayoutData(opIconLData);
opIcon.setImage(SWTResourceManager.getImage("img/game/front/test.png"));
The SWTResourceManager class seems to be largely undocumented so I'm kinda lost here. Any help?

This method requires an absolute path,
SWTResourceManager.getImage("/home/img/game/front/test.png");

Related

.snapshot() method isn't accepting SnapshotParameters()

I am trying to take a snapshot of a scene (which is essentially a diagram maker) and store it as a .png file. However, as the top part of the scene has menu bars, I want to ensure that only the part of the scene below the menu bars is getting captured. I thought of using Snapshot Parameters to ensure this, but for whatever reason the snapshot method doesn't accept my SnapshotParameters class.
Here is the method that takes the snapshot. The issue is that scene.snapshot will not accept the SnapshotParameters param. The code below will work perfectly accept it isn't able to "crop out" the top part of the scene which contains the menubars.
public void exportDiagram() throws IOException {
TextInputDialog inputDialog = new TextInputDialog();
inputDialog.getDialogPane().setContentText("Save image as:");
inputDialog.showAndWait();
TextField userInput = inputDialog.getEditor();
Scene scene = main.getScene();
SnapshotParameters param = new SnapshotParameters();
param.setViewport(new Rectangle2D(0, 23, 600, 377));
WritableImage imgReturn = scene.snapshot(null);
File file = new File(userInput.getText() + ".png");
ImageIO.write(SwingFXUtils.fromFXImage(imgReturn, null), "png", file);
}

Libgdx nine patch text button height issue

I am using a TextButton to create a dialog bubble effect in my game.
I have created the nine patch file as on the picture
everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.
You can see that a multilines text extends the TextButton height but not correctly.
Here is the code
Load the bitmap font
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;
Initialize the TextButtonStyle
NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;
actual rendering
dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);
Does someone see what the problem is?
I finally found the problem.
I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.
Bad font
Good font
You can see that the base line and capheight are not correctly setted on the bad font.
I hope this will help other people facing the same problem.

swt: how to leave space on the side?

I'm designing an application and I want to set a space on the side. My problem now is, I'm using the FormLayout, and the FormAttachment can only define the percentage of the layout; so whenever the editor get wider(i have scrolledComposite in the background), the side space gets wider too, in which I hope it can remain as a same size. Does anyone has idea about how to solve this problem? thanks in advanced.
FormData data = new FormData();
data.left = new FormAttachment(5);
data.top = new FormAttachment(5);
data.right = new FormAttachment(95);
Label1.setLayoutData(data);
You can specify an offset value on the FormAttachment to specify the number of pixels to offset the attachment, so:
data.left = new FormAttachment(0, 50);
data.right = new FormAttachment(100, -50);
positions at the left and right with a fixed 50 pixel offset.

Issue regarding dynamically loading Images in loop using Java Swing

public String[] imagesArray = {Images.firstImage, Images.secondImage};
String imagesPath = "/testproject/images/";
for(int i = 0; i<imagesArray.length; i++) {
URL imageURL = this.getClass().getResource(imagesPath+imagesArray[i]);
ImageIcon orignalImageIcon = new ImageIcon(imageURL);
Image newImage = orignalImageIcon.getImage().getScaledInstance(100, 90, java.awt.Image.SCALE_SMOOTH);
ImageIcon newImageIcon = new ImageIcon(newImage);
JButton receiptButton = new JButton(newImageIcon);
receiptButton.setBorder((new EmptyBorder(0,0,0,0)));
toolBar.add(receiptButton);
add(toolBar);
}
Images not shown in my design layout?
The problem is most likely the asynchronous loading nature of using an ImageIcon to load the original images.
If that is the problem:
There is an easy way to test it. Add the orignalImageIcon to the button and see if they all appear.
There is an easy way to fix it. Load the images using ImageIO.read(URL) - a method that will block until the image is completely loaded.

How to modify letter-spacing in a JTextPane?

I'm need to modify letter-spacing (font tracking) in a JTextPane, and I can't get it to work.
When I'm using a JTextArea, I can just do:
Font font = new Font("Courier New", Font.PLAIN, 10);
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
attrs.put(TextAttribute.TRACKING, -0.1);
font = font.deriveFont(attrs);
textArea.setFont(font);
but as I need to change line spacing, I need to use a JTextPane, and doing:
textPane.setFont(font)
as I did with the JTextArea doesn't work. another thing I tried was:
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -0.2);
StyleConstants.setFontFamily(set,"Courier New");
StyleConstants.setFontSize(set, 10);
set.addAttribute(TextAttribute.TRACKING, -0.1);
ta.setParagraphAttributes(set, true);
But the tracking attribute doesn't work.
What am I doing wrong?
Do you mean kerning? This one shows how to specify custom kerning and some more text effect
http://java-sl.com/gp_effects.html

Categories