Manage ImageIcon in jPanel - swing

I have a problem when loading a image using jLabel to Imageicon into a Jpanel. When The image have big size (width and height), it makes a Jpanel in my Splitpane so big too in display. How to make this image have default size?
And, how to implement mouse or mouse wheel when big size image is loaded. So ,It makes the image can be dragged or pull up and down... ?
My code like this...
public FileImage() {
try {
image = ImageIO.read(new File("/media/Data/workspace/final_tugas_akhir/tmp/hasil/akhirnya.png"));
} catch (IOException ex) {
System.out.println("Failed to Load Image...!!! ");
}
JLabel jLabel = new JLabel(new ImageIcon(image));
jLabel.setPreferredSize(new Dimension(10, 10));
jPanel3.add(jLabel); // jpanel3 is my panel in main class
}
and in main class just use this
pnlProsesAlgoritma.add(new FileImage()); // pnlProsesAlgoritma is panel to "stick" jpanel3
this is my screenchot before image not yet loaded :https://www.dropbox.com/s/euslx1jj3n21x7j/before.png
after the image loaded looked like this https://www.dropbox.com/s/0mghkg0bs5cmo9d/after.png
P.S : I am using splitPanel to separate two panel. When the image is loaded, the other panel is shifted...
Thanks 4 the help

Maybe you can use the Stretch Icon. It will automatically resize itself to fill the space available.

Insert your JPanel inside a JScrollPane.
This way, your sizing problem will be solved, and you can navigate large image easily too.

Related

Textbutton not working and scaling issues

I've created a table with scened2d that contains buttons, but i have many issues with thoses buttons.
First issue is scaling, whenever i try to set a button's with and height an issue appear
when i set it with :
table.add(btn).with(100).height(30);
or
btnStyle.up.setMinWidth(80);
btnStyle.up.setMinHeight(40);
btnStyle.down.setMinWidth(80);
btnStyle.down.setMinHeight(40);
the button is correctly scaled but the background is acting weird
But if i don't set their with and height, their background are normal but the buttons are way too big
Second issue, the buttons don't "work" as i can't click on then (nothing change)
here is the code
// style
final Skin skin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));
final BitmapFont font = skin.getFont("font");
final TextButton.TextButtonStyle btnStyle = new TextButton.TextButtonStyle();
btnStyle.font = font;
btnStyle.up = skin.getDrawable("button");
btnStyle.down = skin.getDrawable("button-down");
btnStyle.up.setMinWidth(80);
btnStyle.up.setMinHeight(40);
btnStyle.down.setMinWidth(80);
btnStyle.down.setMinHeight(40);
// buttons
Textbutton btn = new TextButton("some text", btnStyle);
how can i scale my buttons correctly and what could cause the buttons to stop working ?
(there is already a question about buttons scaling but it didn't helped much)
For getting the clicks on the button, you have to add a ChangeListener.
The assets you are using as button background are too big, or their padding is too big. Use the Skin Composer to get a preview for your skin.
With the buttons not working, did you remember to call setInputProcessor on your stage?

How can I avoid sprite distortion as the frame's bounding box changes?

I have a spritesheet (packed by the wonderful TexturePacker) that looks like so:
and the animation as an animation works well using the current code.
However, when the animation moves to a frame where the bounding box is smaller (e.g. when the legs are closer, when the figure is kneeling, etc.), the image distorts so that the smaller bounding box fills the bounding box of the sprite.
How can I fix this?
The spritesheet above is a frame-extraction from a sample Adobe flash animation.
Easier way:
You can make all the textureregions the same size.
Hard way:
Keep it like this, but you need to save and calculate the size and position of every textureregion to draw.
I personally recommend you the first option, just make all the image the same size.
cant give you a link but here is what i do for these situations
while making the texture packer strip the white space
make a array of AtlasSprite for images
then in my render method
public void render(float delta) {
atlasSprite = animation.getKeyFrame(statetime, Animation.LOOPING);
atlasSprite.draw(batcher)
};
in my update method, which gives exact rectangular bounds of the current image displayed. Since you have stripped the whitespace and used AtlasSprite so bounds will be without blank whitespace.
public void update(float delta) {
bounds = sprite.getBoundingRectangle();
}
P.S. this is just a pseudo code and many syntatical error could be there its just to give you a idea

Top color of a Scene2d.ui window?

I want to create a UI window in libgdx where the top part (the part containing the title and which allows you to drag the window) has a different background color from the rest of the window. I can only set the background color for the entire window, is it possible to change it for just this top part?
Code so far:
WindowStyle windowStyle = new WindowStyle(new BitmapFont(), Color.WHITE, skin.newDrawable("white", Color.BLACK));
Window window = new Window("test", windowStyle);
window.setMovable(true);
window.padTop(20);
stage.addActor(window);
window.setPosition(100, 100);
window.setSize(500, 300);
The top bar for UI Windows in libgdx uses the same texture as the whole window. In order to change the color for the top bar the underlying texture file will need to be modified. libgdx treats it as a whole and there is no built-in way to specify separate colors for each component of a window.
There are other ways, but they are non-trivial and requires writing your own widget class to replicate most of the behavior of the built-in Window class.

JScrollPane will not reduce in size

I wrote a simple gui with DesignGridLayout as layout manager.
I have a 2 JTextFields, beneath them a JScrollPane containing a JTextArea, and right under it I have a button.
Upon maximizing the JFrame, everything is stretching out nicely to fill the screen.
The problem starts when I try to restore the Frame to it's original size. the JScrollFrame Vertical size remains too large, and the Button is not shown, since it is covered by the too big Jscrollpane.
The problem is the same for manual resizing. The JScrollPane sets a new value for it's vertical size according to how much I stretch it.
I tried overriding the getScrollableTracksViewportHeight() method from Scrollable interface, but it had no affect. In addition, I had this entire setting inside a Jpanel which I removed, but it's still the same.
Any insights would be appreciated.
edit: added samples of code.
This code creates most of the Gui:
DesignGridLayout layout = new DesignGridLayout(frame);
layout.row().grid().empty().add(new JLabel("someText1"), someText1);
layout.row().grid().empty().add(new JLabel("someText2"), someText2);
layout.emptyRow();
layout.row().grid().add(new JSeparator());
layout.row().grid().empty().add(titleLabel).empty();
layout.row().grid().add(textarea("",5,6));
layout.row().grid().empty().add(button).empty();
This code creates The JScrollPane:
private JScrollPane textarea(String content, int rows, int columns)
{
JTextArea textArea = new JTextArea(rows, columns);
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}

Width and Height of JPanel inside Container of JFrame is 0, why?

I am having a simple problem I guess, that u should know how to solve..
I am trying to do this:
I have a JFrame.
Then I create a JPanel and set his layout to GridLayout(5,5)
Finnaly I add this JPanel to the Container of my JFrame.
When I try to get the width of my panel it shouldnt give me 0 right?
I do this: System.out.println(mypanel.getSize().getWidth()); and it says that is zero :c
why ??
I wanna know the size so I can divide per 5 and know how much to paint for each label, this will be a grid of labels..
Thanks alot in advance...
public janela(){
window = new JFrame("teste");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
contentor = window.getContentPane();
interior = new JPanel();
interior.setLayout(new GridLayout(5,5));
contentor.add(interior);
System.out.println(interior.getSize().getWidth()); //it says 0 ?!?! why??
}
thanks alot in advance guys!!!!
Panels are sized based on their layout. specifically when they are shown and doLayout() is called for you. doLayout() figures out what the appropriate size for your panel is based on the contents of the panel, and the layout manager in force. Since your panel has nothing in it, the appropriate size is 0, 0.
If you will never have anything in the panel, and want to circumvent this (0, 0) size, you can override getPreferredSize() of the panel to return a size that you want the panel to respect for layout.