I was wondering if there is a quick and easy way of trying to position an image at a particular position in GUI. In addition to this is it possible to put Jlables/Buttons on this picture. That picture should only form a section of the JFrame. I know you can use setbounds(width, height, width, height); but this takes alot of accuracy and very hard to get perfectly right.
Don't call setBounds, which implies that you are using a null layout. Always use a layout manager.
One approach would be to add a custom JPanel that overrides paintComponent and calls drawImage(Image, x, y, width, height, ...). JLabels/JButtons, etc. can still be added to the panel.
Don't forget to call super.paintComponent—this will ensure that your child components get painted.
Also, have a look at Background Panel.
Related
Im having a problem with JTextArea that has been quite annoying.
I am trying to make a 2D game for a project, and I am currently attempting to put text on my screen using JTextArea instead of just images.
My code for configuring the JTextArea is as follows:
Insets margin = new Insets(10,30,0,0); //bottom, left, top, right
textArea = new JTextArea();
textArea.setSize(textWidth, textHeight);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setMargin(margin);
textArea.setFont(halFont);
textArea.setForeground(Color.YELLOW);
textArea.setBackground(new Color(0,0,0,0));
textArea.setLocation(0, 600);
My problem is that when I "paint" it to the screen using:
textArea.paint(g);
It just paints to the position 0,0 however it keeps the rest of the textArea formatting including color, insets, font, etc.
When I run textArea.getLocation(); after painting, it claims it is at the position 0,600 even though its clearly at 0,0.
I have tried changing the x and y position calues, but the text still stays at 0,0 on my screen. Because of this i'm fairly certain that it is something to do with the .paint call rather than .setLocation call.
I realize that I should be using a format manager of some sort, but since i'm using .paint I get the feeling it won't help.
Q: How can I use the .paint call and change the position of the text being painted to the screen.
Thanks for your time!
When you are using Layout managers, the layout manager itself is the one that calls setLocation, and setSize, so any attempt you make will be overwritten. You need to use the layout manager and allow it to place the component where it thinks it should go. You influence its position by
1) what layoutmanager you choose
2) your use of setPreferredSize(Dimension d), setMinimumSize(Dimension d), setMaximumSize(Dimension d)
I am using netbeans to design a JFrame.
now when i compile and run.It executes and opens a window
now the problem is when i maximixe the window..I want the image or Jlabel to cover the width of the jframe.
SO how can i do it.Plz Help
First stop using visual designer. Stop now. Immediately. Forget that it exists. Visual designers create non-maintainable code. They save 5 minutes at the beginning and cause you to spend hours later.
Now, take a look on any java layouts tutorial. For example this one: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Once you read and understood it go back to your application. Think what layout(s) you need. Design the application. Implement it. If you still have problem that you cannot solve yourself within reasonable time ask more specific question again.
When you understand layouts good enough and want to implement real application with a lot of dialogs take a look on MigLayout.
A JLabel always paints the Icon at it actual size.
If you want dynamic scaling of the image then you need to do custom painting. Basically you would override the paintComponent(..) method of a JComponent with code like:
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
Of course you would need to pass the image as a parameter when you create the class. It also assumes you add the component directly to the content pane which uses a BorderLayout by default.
For fancier or more flexible solutions you could:
Use the Background Panel
Use Stretch Icon on a JLabel
Basically I'm drawing a lot of transparent JPanels; profiling shows that most time is spent in Component.paint(). It would be possible to optimize this quite radically since in most cases the real non-opaque area per JPanel is quite small, for example around the edges.
As it stands now, repainting() a component would trigger a repaint of all its parents, since the RepaintManager can't know the dirty parent area is in fact opaque and will go up the component hierarchy. I was thinking about calling markCompletelyClean() on all parents whenever a panel is invalidated and managing the dirty regions myself with addDirtyRegion().
However, is there a cleaner approach of marking only specific rectangles of a JPanel as opaque (or transparent, doesn't matter)?
Even if you do something fancy with markCompletelyClean() and addDirtyRegion(), I doubt you'll get much of a performance benefit. When swing goes for a repaint, it collates all of the dirty regions to paint and starts a repaint with the minimum bounding box of all of the dirty regions. Thus if you mark the perimeter of a JPanel as dirty, then the bounding box of the JPanel is the same as the entire JPanel, which means you'll be repainting the entire thing any way.
Consider using JLabel (non-opaque by default) instead of JPanel, then you are able (to the JLabel) you can add any JComponent same as to the JPanel, but you have to set LayoutManager, then you forgot care about Opacity/Transparency and for particulars Region(s)
Why can't you define your own method myRepaint() calling multiple
public void repaint(long tm, int x, int y, int width, int height)
for all the borders?
Also try to play with clipBounds of your Graphics to repaint only really necessary parts. You can set any custom Shape as clip.
I ama drawing a rectangle in a panel and i added a + button to increase my shape's size.how can i do it?
Read the section from the Swing tutorial on Custom Painting for the basics.
Now, in your class that does the custom painting you need to keep two variables:
rectangleWidth
rectangleHeight
You will also need to add a method to the class like "increaseRectangleSize()". Then when you click your button you invoke that method. That method will increase the values of those two variables and then invoke repaint() on itself.
If you need more help post your SSCCE that demonstrates the problem since your description of the problem is too vague.
If your question is how to draw a bigger rectangle. In the paintComponent method that you are drawing a the rectangle increase the size. If you are trying to make the JPanel bigger, I would highly suggest you looking using Layout Managers and possibly set the PreferredSize() of the panel.
I need to find a component's exact screen size in pixels when the main JFrame is resized.
I've tried several things and couldn't find an easy way to do it: it probably have missed something obvious.
BBBBB JFRAME BORDER BBB
BZZZZZZZZZZZZZZZZZZZZZB
BAAAAAAAAAAAAAAAAAAAAAB
BCC1................DDB
BCCC................DDB
BCCC................DDB
BCCC................DDB
BCCC................2DB
BEEEEEEEEEEEEEEEEEEEEEB
BBBBBBBBBBBBBBBBBBBBBBB
Resized, it could become this if, say, the user made the main JFrame shorter (vertically) and wider (horizontally):
BBBBBB JFRAME BORDER BBBBBB
BZZZZZZZZZZZZZZZZZZZZZZZZZB
BAAAAAAAAAAAAAAAAAAAAAAAAAB
BCC1....................DDB
BCCC....................2DB
BEEEEEEEEEEEEEEEEEEEEEEEEEB
BBBBBBBBBBBBBBBBBBBBBBBBBBB
What I want is to find the exact size in pixels, on screen, of the rectangle area indicated by dots.
I'm adding a ComponentListener to get the resizing events, which works fine.
The issue I'm having is that calling getWidth()/getHeight() on a Component does apparently not return the component's screen size but the component's actual size (and you can have, for example, a JPanel wider than the component it is into).
Any hint most welcome.
You're looking for JComponent.getVisibleRect().
getWidth() and getHeight() returns the width and height of the component. I've never heard of any difference between "actual" width/height and "screen" width/height.
Some layout managers however, does not fill the panel with a component it contains, and some space between the component and the panels edge may not be occupied by the component. In that case I usually setBackground(Color.BLUE) on the underlying panel to see what's going on.
This is a very rubbish way but you can use a wrapper Panel, where you put your main panel into. Than you can call getWidth() and getHeight() from the wrapper panel.