This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so:
class BackgroundPanel extends JPanel {
private ImageIcon imageIcon;
public BackgroundPanel() {
this.imageIcon = Icons.getIcon("foo");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this);
}
}
But now, how do you add a component on top of that background?
When I go
JFrame w = new JFrame() ;
Container cp = w.getContentPane();
cp.setLayout(null);
BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);
JPanel b = new JPanel();
b.setSize(new Dimension(30, 40));
b.setBackground(Color.red);
cp.add(b);
w.pack()
w.setVisible(true)
It shows the little red square (or any other component) and not the background, but when I remove cp.setLayout(null);, the background shows up but not my other component. I'm guessing this has something to do with the paintComponent not being called by the null LayoutManager, but I'm not at all familiar with how LayoutManagers work (this is a project for college and the assignment specifically says not to use a LayoutManager).
When i make the image the background has to display null (and so, transparant (??)) the red square shows up so it might be that the background is actually above my other components.
Does anyone anyone have any ideas?
Thanks
When using null layout (and you almost never should) you have to supply a bounds for every component, otherwise it defaults to (0 x,0 y,0 width,0 height) and the component won't display.
BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);
isn't supplying a bounds. You'll need to do something like:
BackgroundPanel bg = new BackgroundPanel();
bg.setBounds(100, 100, 100, 100);
cp.add(bg);
Which would make bg size 100 x 100 and place it at 100 x, 100 y on the frame.
Look in the documentation on the Root Pane for all the information you need. Note the availability of the layered pane and the glass pane as well as the content pane.
By default all components have a 0 size. Just because you do some painting on a component doesn't give the component a size. You are still responsible for setting the size. That is why you should always use a layout manager. It looks after all this size stuff for you so you don't have to worry.
I don't know why newbies always think they can't use a layout manager. Yes it takes a couple of more minutes to learn, but it saves you a lot of grief in the long run.
Background Panel shows a couple of approaches. Again they both assume you use a layout manager, so you may need to set the size manually.
Related
I'm pretty new to GUI and Java as a whole so I hope that I can explain this well enough and understand people's answers.
For a school project, I need to put a bunch of stuff on some rectangles but I'm having issues even adding one rectangle properly.
From researching online, this is what I have (the JPanel and GridBagConstraints are just there to show what I'd like to use):
public class GUI extends JPanel
{
public static void main (String [] args)
{
GUI g = new GUI();
JFrame window = new JFrame("Java Window");
window.setSize(1280, 960);
window.add(g);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel layout = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
}
#Override
public void paintComponent(Graphics g)
{
Color boxColour = new Color(194, 190, 190);
super.paintComponent(g);
g.setColor(boxColour);
g.fillRect(10, 10, 100, 100);
}
}
So right now, the rectangle appears in the window. But how can I add constraints to it? Is that possible? I would think that I should use JPanel to keep everything more organized since there will be many components so I tried adding this:
layout.add(g);
window.add(layout);
window.setVisible(true);
However, the rectangle no longer appeared. What am I doing wrong and how can I fix it and add constraints to my shapes? Thanks!
In your first scenario, the default layout manager of the frame is the BorderLayout and you are adding your "g" panel to the BorderLayout.CENTER. So based on the rules of the BorderLayout your "g" panel will take up all the space available in the frame. So you have plenty of space to paint your rectangle.
However, in your second scenario, your "layout" panel is using the default layout manager of a JPanel which is a FlowLayout which respects the preferred size of any component added to it.
The preferred size of the "g" panel is 10 x 10. So, when you add the "g" panel to the "layout" panel there is nothing to see because all you custom painting is done outside the bounds of the panel.
You need to override the getPreferredSize() method of your GUI panel to return a preferred size of (120, 120) so you can see your rectangle painted with a 10 pixel border around all the edges.
Read the section from the Swing tutorial on Custom Painting for more information and working examples demonstrating how to override the `getPreferredSize() method.
You will also need to read the Swing tutorial on How to Use GridBagLayout for example of using the constraints to add multiple components.
I want to animate several jpgs in a JFrame, I'll show you some extracts:
My class constructor that extends JFrame
super(title);
setLayout(null);
setResizable(false);
setSize(Settings.windowWidth, Settings.windowHeight);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
My class constructor that extends JPanel
i = new ImageIcon(image).getImage();
setSize(i.getWidth(this),i.getHeight(this));
setBounds(x, y, i.getWidth(this), i.getHeight(this));
The overwritten method
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(i, getX(), getY(), null);
}
Yes, I know null layout isn't preferrable, but unless you have a better idea for absolute positioning I'll stick with it for now.
Above code does paint the image, starting at (x,y), but not completely.
For 50 and 100 it shows this:
Which is pretty much: It only paints the image within a 256x256 box (image dimensions) from 0,0, no matter where it has been relocated to.
Any advice, help, solutions, suggestions?
If you need more code, ask me, just don't feel like putting everything around it in here, too ;)
There is no need for custom painting:
Add the ImageIcon to a JLabel and add the JLabel to a JPanel
Change the location of the label on the panel when you want to animate it.
Or, if you do custom painting then there is no need for a null layout.
You override the getPreferredSize() method of the JPanel and add your panel to the frame.
Then in the paintComponent() method you can paint the image where every you want withing the bounds of the preferred size that you set.
My gut feeling is you don't understand how component painting actually works...
First, you do this...
i = new ImageIcon(image).getImage();
setSize(i.getWidth(this),i.getHeight(this));
setBounds(x, y, i.getWidth(this), i.getHeight(this));
Then you do this...
g.drawImage(i, getX(), getY(), null);
which seems to be painting the image at a offset position from the components origin, but since the component is sized to match the size of the image, the image is cropped at the component boundaries.
When a component is painted, the Graphics context's origin is set to the components location, meaning that 0x0 is now the components top/left corner.
You can test this by using setBorder(new LineBorder(Color.RED)), which will show you the physical bounds of the component
In your case, something like
g.drawImage(i, 0, 0, this);
In your case, you should be moving the component not the image.
Personally, I'd add the JPanel to the JFrame using a BorderLayout, then you can simply move the image anywhere within the context of the component itself. Remember to override getPreferredSize to return an appropriate size for your purposes so the frame can be packed around it more effectively.
It's tricky to do animation with components (not impossible, there's just a lot to take into account), generally it's just easier to paint directly to a canvas like a JPanel, but that's me
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works
So, I have been having trouble drawing multiple objects on a JFrame, and I know I need to use Layout managers, so I decided to test it with one object before I do multiple, however when I run this code:
fps = 30;
panel = new JPanel();
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.addKeyListener(new key());
running = true;
update = true;
ball = new Ball(0,0,1);
//panel.setBackground(Color.BLACK);
panel.add(ball);
panel.setVisible(true);
frame.add(panel, BorderLayout.CENTER);
frame.requestFocus();
frame.setVisible(true);
startTime = System.currentTimeMillis();
nothing draws (There is more to the code, i just didnt want to inlcude all of it). However, when I remove the comment and set the background to black, the JFrame turns black. So why is it that it wont draw my Ball object (which i know works) but will change the background? Is there a specific way you need to draw on a JPanel?
panel = new JPanel();
By default a JPanel uses a FlowLayout which respects the size of any component you add to the panel.
ball = new Ball(0,0,1);
I have no idea what the code in your Ball class looks like, but I would guess the preferred size is (0, 0).
You need to override the getPreferredSize() method of your Ball class to return the size of the Ball so that your layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
I know I need to use Layout managers,
The problem with a layout manager in this case is that the layout manager will control the position of the Ball which may or may not be what you want. If you want balls in random positions, then you will need to use a null layout. Then you will need to use the setSize() and setLocation() methods to control each Ball component.
Another option is to do custom painting of all your Balls. In this case you would add an object that you want to paint to an ArrayList. Then the custom painting code would iterate through the object in the list and paint them individually. See Playing With Shapes for ideas on this approach.
I would like to be able to have a JPanel within my JFrame of a fixed size 400x400.
I would also like the to be a 20px wide border all around it.
The main problem is the following code doesnt stick it its size.` JScrollPane runningAni = new JScrollPane(new views.cRunningAnimation(
model));
runningAni.setMaximumSize(new Dimension(400,400));
this.setSize(new Dimension(600,600));
this.add(runningAni,BorderLayout.CENTER);`
When doing this the runningAni panel just strethces accross the whole frame.
public void paint(Graphics g) {
this.setBackground(new Color(0,255,0));
}
I know this because my full frame paints itself green rather than just the JPanel (The above paint code is for my panel not the frame)
How would i create the panel so it always stays the same size and so there is always a 20px colored border around it?
BorderLayout ignores the size. You need to set a LayoutManager that either allows you to set the size to a fixed size or one that cares for the sizes set. There are different layout managers that allow this (e.g. GrindBagLayout or no layout manager at all). Some are not that easy to use (e.g. GridBagLayout). What to use depends on the rest of the layout.
You could probably use a layout panel that contains your custom panel. The layout panel needs an appropriate layout manager and could be put into the center of the BorderLayout. This would mean nearly no modifications to existing layout code.
The whole point of BorderLayout is to fill the center with the center component.
Don't override the paint() method to set the color of the panel. Use:
panel.setBackground(...);
When you create the panel.
How would i be able to set a border around my Jpanel
See How to Use Borders.
Just set your layout to null, to what ever class your adding your JPanel.
Then use the setBounds() method to set your location and size!
For example:
public class Main extends JFrame{
YourPanelClass panel = new YourPanelClass();
public Main(){
// I didn't want to put all the, everyday JFrame methods...
setLayout(null);
/*
First two coordinates indicate the location of JPanel inside JFrame.
The seconds set of coordinates set the size of your JPanel.
(The first two coordinates, 0 and 0, tell the JPanel to start at the
top left of your JFrame.)
*/
panel.setBounds(0, 0, 100, 100);
add(panel);
}
}
And i would GREATLY recommend using the paintComponent() method.
For instance:
(Obviously you put this in your JPanel's class.)
public void paintComponent(Graphics g){
super.paintComponent(g); // don't forget this if you are going to use this method.
//Basically this makes your JPanel's background green(I did it this way because I like doing it this way better.)
g.setColor(new Color(0, 255, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}
Please don't forget to thumbs up if this helped!
setPreferredSize()
setMinimumSize()
setMaximumSize()
should do the trick
I've got a JPanel inside a JScrollPane. I draw things in the JPanel, and at some point I might draw past the width of the JScrollPane. In this case, I'd like the horizontal scroll bar to appear, and I'd like to be able to scroll around to view different parts of the JPanel. However, I end up clearing the JScrollPane.
frame = new JFrame();
frame.setBounds(100, 100, 1000, 800);
localScrollPane = new JScrollPane();
localScrollPane.setBounds(768, 6, 226, 350);
frame.getContentPane().add(localScrollPane);
localView = new JPanel();
localScrollPane.setViewportView(localView);
drawSomeThings(localView.getGraphics());
// wait for user input
int newWidth = drawThingsPastTheWidth(localView.getGraphics());
// these next two lines clear it
localView.setPreferredSize(new Dimension(newWidth, localView.getHeight()));
localView.revalidate();
What am I doing wrong? Thanks!
drawSomeThings(localView.getGraphics());
Don't use the getGraphics() method to do painting. The painting will be lost the next time Swing determines the components needs to be repainted.
Instead custom painting is done by overriding the paintComponent() method of your component.
Do not use setPreferredSize method, here's a related thread.
Do not specify explicetly the size of the JScrollPane with setBounds. Let the LayoutManager of it's parent take care of this.
JScrollPane should use a
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED by default. So
the scrollbar should appear automatically when the preferred size of
the child component is higher than the displayed area. Try to
revalidate the JScrollPane instead of the JPanel.
It would appear that redraw is being called at some point, I've not used swing for a while so I'm not entirely sure what the problem is but try debugging and running through the code step by step to see where redraw is being called would be a good starting place.
you should certainly use repaint instead of revalidate. revalidate only marks all the container upto the top level as invalid.