I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
This my paint method:
public void paintComponent (Graphics g) {
g.setColor(Color.red);
g.fillRect(dist, 0, 10, 10);
dist++;
}
Update:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
Is there a better solution for this problem?
You state:
I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
If the JPanel is drawing it, the short answer is: "you can't".
The longer answer will depend on just where you're trying to draw the JPanel and how the rest of your GUI is set up.
Edit
You now state:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
If I coded the way you were doing it, I wouldn't have these local JPanels draw the car but rather would have the car be its own sprite that exists on a different layer from the streets, perhaps using a JLayeredPane. It could exist in its own JPanel that encompasses your entire map, as long as this JPanel is not opaque. Then you could move the car any which way you'd like.
As said before you can't but if you want it to occupy and larger area you should either make the JPanel bigger or put the paintComponent in the parent component.
Related
I am creating a snake game and I am in need of using a JLayeredPane which will hold 2 Jpanels, this JLayeredPane will then be inserted into a JFrame. The reason I am doing this is because I need to have the base layer -- the game, and on top a layer which will spawn fruit onto the board. The reason I am not doing this directly onto the board is because the board is constantly being repainted, I need a method to randomly change the colour of the fruit for every time the player collects the fruit. The fruit however, will continuously change colour as the board is being repainted and a new random colour is generated based upon an array.
The problem with simply using a JLayeredPane is that I need the frame to fit perfectly with the components inside of it, setPreferredSize does not seem to do this as it does not take insets into account. When setResize(false), the insets do not match with the actual inset values, so I cannot simply add the insets.
So my question, how do I get the JLayeredPane to fit the JPanel's where the components within these JPanels have dimensions and then put this JLayeredPane into the JFrame.
As shown in How to Use Layered Panes: Setting a Component's Position Within Its Depth, use setBounds() to position components within a JLayeredPane.
Alternatively, override paintComponent() in a JCOmponent or JPanel and render the fruit layer beneath the snake layer. If no resampling is required, drawImage() is quite fast in this context.
i'm trying to overlap two different JPanels on a single JFrame. How can i do that? I have a GamePanel where i draw my game, and a BorderPanel, that draws a border for my game. BorderPanel is to expose the enemies and other objects of the game just after crossing the border. The border then must always be on the surface. Is it possible?
I am trying to write a code to generate a graph like this: http://www.mathgoodies.com/lessons/graphs/images/line_example1.jpg
I need more than one different line (I hope that's what they are called).
I'm just starting to learn awt and swing. After exhausting three hours of work, I couldn't manage a way to draw a line on top of any other drawing.
I'll try to explain my problem with an example.
Lets say I draw a square like this:
JFrame window = new JFrame();
window.setLayout(null);
window.setVisible(true);
Graph graph = new Graph();
window.add(graph);
//-------------------
public class Graph extends JPanel {
....
public void paintComponent (Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(150, 20, x, y);
}
....
}
How do I draw another line or anything else on top of this white square whithout drawing the line in the Graphs paintComponent method? How do I add another JPanel on top of another one, so that both of them are visible? (I'm using JPanel to add some buttons)
Hopefully you can understand what I'm asking.
Thank you!
How do I draw another line or anything else on top of this white square whithout drawing the line in the Graphs paintComponent method?
All custom painting should be done in the paintComponent() method. I'm not sure why you want to add another panel that paints on line. Keep it simple and keep all the painting code in one place.
If you want to add other components (like a JPanel) to the panel then you would use layout managers to lay out the components properly. You would also need to make the components non-opaque by using the setOpaque(...) method.
Another way to layer components is to use a JLayeredPane.
Start by reading the Swing tutorial. There are sections on:
Using Layout Managers
Using Layered Panes.
I have a JInternal Frame and I want to draw a circle(using 2Dgraphics) in it and make it flexible. I mean when I change the size of frame the circle become smaller or in making frame larger circle also become larger. Can somebody help?
You would draw in the paintComponent method of a JPanel or JComponent that is held in the JInternalFrame's contentPane, same as you would draw in any other JPanel. I'd get the dimensions of the JPanel at the start of the paintComponent method and use those values to tell how big to draw the circle.
Also, if you add the JPanel directly to the JInternalFrame's contentPane, it will be added by default BorderLayout.CENTER, and so when the JInternalFrame changes size, the JPanel also changes size, it's paintComponent will be called by the JVM, and the new drawing will be resized automatically.
add a WindowListener to the jInteralFrame and redraw whenever the size changes
I am writing a Tetris-based game, and I am using a GridLayout to display the actual shaft which the Tetris pieces fall down. The GridLayout is filled with little JLabel objects. The shaft is itself a JPanel inside another JPanel, the panel I am using to contain and control the entire game. When the game ends, I want the words "GAME OVER" to appear on top of the grid, possibly accompanied by a small image.
My shaft is an instance of ShaftPanel, which extends JPanel (so that I could override paintComponent) and is a nested private class (so it has access to private instance variables of the larger JPanel). The private instance variable it needs to access is boolean game, which is true when the game is in session and set to false when the game is over. Here is the code for my ShaftPanel class:
public class ShaftPanel extends JPanel {
public ShaftPanel(GridLayout g){
super(g);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(game)
return;
g.setColor(Color.WHITE);
g.setFont(new Font("Courier New", Font.BOLD, 15));
char[] c = {'G','A','M','E',' ','O','V','E','R'};
g.drawChars(c,0,c.length,45,45);
}
}
I have one method in the larger class calling the repaint() method of shaft at the appropriate time. Adding a print statement indicates that the paintComponent method is being called when I want it to, and the setColor, setFont, drawChars methods are all being called at the correct times. However, nothing shows up. I highly suspect that the text is being drawn underneath the grid of JLabels, so that it can't be seen, but I don't know how to fix this problem. The JLabels inside the grid have to stay opaque, because the program relies on them being different colors. Is there a way to tell paintComponent to draw the text on top of anything else in the panel?
See the section from the Swing tutorial on Using a Glass Pane for an example and explanation on how a glass pane works.
Another option is to use a JWindow to display a label with your message and icon.
I would probably not have made tetris this way, but one way to test this would be to remove the "if(game)" and see if game over is being written even when no JLabels are there (unless they are always there and sometimes are blank).
At any rate, I think what might be useful though I don't think I have ever used it is a GlassPanel.... This is a panel that can be overlayed on your current JFrame etx...
Look Here for more code and info: http://www.java2s.com/Code/Java/Swing-JFC/Paintonglasspane.htm