How to paint graphics from multiple classes to a single panel - java

At the moment, I have two JPanel classes that draw images and shapes to a JFrame (I will have more in the future), I'm doing this to make things organised.
At first I tried to add each JPanel to the JFrame, but one JPanel would paint over the other.
Each class should be able to call other classes that may draw images to screen as well.
The problem I have is that I cannot get them to draw to the screen.
Should I use paintComponent or paintAll? And how should they be used?
Thank you for any help :)

It sounds as though you are adding both panels to the same location in the JFrame probably in the BorderLayout.CENTER position. One solution is to use a GridLayout with 2 columns for the JFrame and add the 2 panels.
paintComponent is the correct method to override in your panels.
Follow the custom painting trail to see how it should be used.

Related

Add more than one panel on a frame on top of each other

I'm working on a project where I am displaying a square and a circle.
The circle moves on its own, but the user moves the square via the arrow keys. Whenever the circle touches the square, it rebounds.
Square and circle are different classes (2 different panels). I want to add those two to a frame, one on top of the other such that both are visible.
Can someone tell me how to do so?
JFrame n = new JFrame();
n.setTitle("Background Color for JFrame");
n.setSize(1000,600);
n.setLocationRelativeTo(null);
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n.setResizable(false);
n.add(new Ball());
n.add(new Team());
n.setVisible(true);
User interface components at the same level in a hierarchy are assumed to be non-overlapping by default. You can explicitly work around this by making your components transparent with setOpaque(false), assuming you are taking care to only draw whats needed in the components, e.g. in case of a JPanel ensure that its background is not drawn. Its still somewhat random (implementation dependent) which component has precendence over another when doing this.
There is a component explicitly designed for that: JLayeredPane (https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html), which manages "layers" in which components can be put, giving you full control which overlays which.
Games often implement this by themselves, since the full features of JComponent are not needed to represent a simple graphical element. In that case a single component is used as a "canvas" to paint self-defined objects onto making use of an override of paintComponent (See: https://docs.oracle.com/javase/tutorial/uiswing/painting/)
If you want to do this in swing, as it sounds, I would really recommend making a new class that extends JPanel, and override it's paintComponent method. In this method you use the Graphics in the argument to paint to the canvas. Then you can add this custom panel instead of two separate components to your JFrame, and handle the rendering in there. This render panel can then keep track of all objects that needs to be rendered, preferable implementing some interface (Drawable?) with a draw(Graphics g) method. That way you can make your classes that needs to be rendered implement your Drawable interface, keep then as a list in your render panel and just iterate through them in your paintComponent method and call draw on your Drawables.

Clarify the difference and connection of canvas, frame, pane and panel

I'm quite confused and I when I try to find an answer with google I get bombarded with tutorials.
What is the purpose of each one?
How are they connected?
What is the purpose of the connection?
In java canvas is area used to draw something by java graphics. For ex. drawing an image or rectangle.
Frame is used as JFrame(swing), a top level container which can contain canvas, panels, pane(DesktopPane, ScrollPane) etc..
Panel or JPanel is a subcontainer used to contain textboxes, buttons, canvas etc.
Jframe can contain multiple panels, but panel can't contain JFrame.
Textboxes, buttons can directly be added to Jframe but it decreases flexibility, Suppose we want to hide a set of buttons from ui, then we need to hide them one by one from JFrame. If those text boxes are added to panel then we just need to hide that panel only. There are so many cases about using panel in jframe.

How to Display a Panel in another Panel?

I'm coding a simple graphics program in Java.
So, I have 3 classes.
The first class is the GUI w/c extends JFrame, it load the menu bar and panel (drawing class)
The second class is the drawing class, it extends JPanel, and it has simple Graphics commands.
The third class is an animation class, it displays an animation. It also extends JPanel.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class. When I try to place it there, it displays a tiny box beside the drawing class panel. I am not very good at frames and panels. Thank you very much in advance!
All JPanels have a LayoutManager which, rather handily, manages how Components are laid out. The default layout is FlowLayout, which default mode will simply place each component to the right of the last component.
If you want to change the layout to something more useful, there are many options; BorderLayout, GridLayout, GridBag layout are popular ones. Myself, I use MigLayout, an external library which is very powerful :).
As for it appearing small, try manually enforcing the size with setSize(w, h) or setPreferredSize(w, h).
You might want to write
GUIFrame.remove(drawingPanel);
GUIFrame.add(animationPanel);
GUIFrame.pack();
However the behavior may vary if you have other components added to your GUIFrame. It is difficult to help you exactly because you have not posted an SSCCE.
So my question is this, how do I display the animation class in the panel of the GUI class. I want it to be displayed instead of drawing class.
Use a Card Layout.

Changing the border of rectangle in jPanel

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.

How to make a Java Canvas look raised from its container

I have a simple GUI component written in Java. The class draws an analog clock in a java.awt.canvas.
This canvas is then contained in a JFrame - What I want to do is give the canvas a 3d "raised" effect - almost like adding a drop shadow to a photo.
Is there a simple way to do this?
If you are using a JFrame, then you have two options:
Add your own component to a JPanel first and then add this to the JFrame.
Instead of inheriting from java.awt.Canvas, you can inherit from JComponent. Then you would have to do all your painting in the paintComponent() Method instead of just paint() (you can just rename your current paint method).
In both cases you can now set a border with the setBorder() Method (on the JPanel or your component) you can get from BorderFactory.
See also: How to Use Borders
If you were using a Swing element, you'd use the createRaisedBevelBorder() method of the BorderFactory and set the canvas' border to the resulting border. Canvas is an AWT component, so you'll need to wrap it in a Swing component to which you can set the border.

Categories

Resources