I have a program which displays one ellipses (Ellipse2D) .
Should I directly add the ellipse to a JFrame or should I add it to a JPanel, which ultimately is added to the JFrame? (Adding a JPanel is more work)
Which one will help me in the long run? (I might consider putting keybindings.)
To make things clearer:
Should I do:
public class Test extends JFrame{ // This is a JFrame
Ellipse ellipse = new Ellipse(); // I have an ellipse class
Test(){
...
add(ellipse);
...
}
}
Or should I do:
public class Test extends JFrame{
Test2 test2 = new Test2();
Test(){
...
add(test2)
...
}
}
public class Test2 extends JPanel{ // This is a JPanel
Ellipse ellipse = new Ellipse(); // I have an ellipse class
Test2(){
...
add(ellipse);
...
}
}
Is Eclipse a JPanel (or extends some Java Swing container component) or not.
If not, then go with JPanel solution it will be somewhat treated as a component. If it is then first add a layout manager (BorderLayout perhaps) to JFrame and then add the panel to it may be in CENTER (make your own choice).
My approach for desktop application development has been to:
Add a layout manager to JFrame then add JPanels to JFrame based on the layout. This makes GUI more manageable and easy to update/change in future.
You might always just draw the ellipse(s) in a BufferedImage, add the image to an ImageIcon and add the icon to a JLabel.
Related
I know this question has been asked before but i cant seem to implement any of the other answers to my project. So i have my paint method in my player class here.
public void paintComponent(Graphics g)
{
//makes player(placeholder for real art)
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(x,y,50,30);
}
Then I have my main class here.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* Write a description of class Main here.
*
* #author Richard Zins
* #V01
*/
public class Main extends JFrame
{
public static void main(String[]args)
{
Player p1 = new Player();
Main m = new Main(p1);
}
public Main(Player p1)
{
JFrame ar = new JFrame();
JLabel background = new JLabel(new ImageIcon("/Users/rizins/Desktop/PacManTestBackGround.jpg"));
ar.setTitle("Runner Maze");
ar.setSize(800,600);
ar.add(background);
ar.setVisible(true);
ar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ar.add(p1);
}
}
Now I cant seem to get my player object to paint over my background any help would be appreciated!
There are a couple of mistakes...
JPanel by default is opaque, so you need to change it to be transparent
JFrame uses a BorderLayout by default, so only one component will be shown at the (default) center position, in this case, the last thing you add.
You should call setVisible last
Instead, set a layout manager for the JLabel and add your player class to it. Instead of adding the JLabel to the frame, you should make the label the contentPane for the frame, for example...
p1.setOpaque(false);
JFrame ar = new JFrame();
ar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel background = new JLabel(new ImageIcon("/Users/rizins/Desktop/PacManTestBackGround.jpg"));
ar.setTitle("Runner Maze");
ar.setContentPane(background);
ar.setLayout(new BorderLayout());
ar.add(p1);
ar.pack();
ar.setVisible(true);
I should point out that using a JLabel to display a background image like this could cause you problems, as the JLabel only uses the text and icon properties to calculate its preferred size, this could cause some child components to be laid out beyond its visible range.
See How to set a background picture in JPanel for more details and a possible solution
You can make the JPanel transparent by setting the opaque to false. e.g:
panel.setOpaque(false)
Try is if this work for you.
Use a JLayeredPane and add your background at the index 0 (indexed with an Integer not an int). Then you add another JPanel that is not opaque (like in #Bahramdun Adil 's answer) and add your player to that.
This way you can have a background and display your player at the same time.
I have strange problem with JComponent. I am trying to create my own JComponent and so I need to compose my JComponents together.
I wanted to paint JButton in my JComponent JDial:
public class JDial extends JComponent {
private static final long serialVersionUID = 3364481508702147328L;
public JDial() {
JButton b = new JButton("test");
this.add(b);
}
}
But that just paint nothing. Even more interesting is that this one works well:
public class JDial extends JPanel {
private static final long serialVersionUID = 3364481508702147328L;
public JDial() {
JButton b = new JButton("test");
this.add(b);
}
}
JPanel inherits from JComponent and paints JButton inside. How JPanel do this magic?
Thanks in advance
Generally you would extend JComponent when you want to do custom painting by overriding the paintComponent() method.
If you just want to add a bunch of components then you should use a JPanel.
The difference between the two is that by default JPanel uses a FlowLayout so it know how to layout any component added to it. To make JComponent like a JPanel you would need to set the layout manager and add custom painting to paint the background.
While JComponent also descends from Container and does have all code to repaint properly sized and positioned children, it does not have any capability to resize or layout them. And you do not set neither size nor location for your JButton so zero size is assumed by default.
Differently, JPanel is created with FlowLayout by default, this layout manager that will set component sizes mostly depending on they computed preferred sizes. In general, it is unusual to use JComponent as container directly, use JPanel.
I'm creating an applet which consists of a class which extends JApplet, with a menubar and a class which extends a JPanel.(So there is a menubar and a JPanel shown in the applet).
In this class I add and remove some textfields to the JPanel. This all works fine. Here's where it gets tricky: it only works the first time. When I add some new textfields to the JPanel, they are added and visible in the JPanel, but the menubar in the JFrame stops working.
Since the code is too extensive I'll only post parts of it.
Here's the code where I add the JPanel to the JApplet:
public class Simulator extends JApplet implements ItemListener, ActionListener {
Container pane = getContentPane();
canvas = new DrawCanvas();
pane.add(canvas, BorderLayout.LINE_END);
}
Here's the code of the JPanel:
class DrawCanvas extends JPanel {
public void paintComponent(Graphics g) {
if(textfield != null)
remove(textfield);
textfield = new JTextField();
this.add(textfield);
}
}
This works the first time(when nothing is removed), but the second time the menubar stops working.
When I leave out the this.add(textfield); line, the menubar keeps working.
I once had similar problems with popup menus beeing painted behind other components.
Try calling static JPopupMenu.setDefaultLightWeightPopupEnabled(false); or the setLightWeightPopupEnabled on your specific submenu. This will make (all) popup menus (i.e. submenus) to heavy weight components that have a native peer.
I believe you are running into issues with threading. Adding and removing JComponents during painting might mess up the EDT (which is calling the paint method in the first place).
I'm trying to close a frame yet open a new frame.
My application has page A, a JPanel with some controls and a specific button, and when the user clicks the button, I want page A to disappear and page B to appear (page B has controls that depend on the choices that are made by the user on page A).
This has been asked before, but there was no satisfactory answer. Inside the ActionListener implementation, namely public void ActionPerformed(ActionEvent e) from my jpanelForPageA class, I can comfortably write this.setVisible(false), but how can I set page B to a visible state?
You can do the removal of panel a and then the addition of panel b trick. Another is to use a CardLayout.
When you create your panels, you add them to a containing JPanel that you initialize with a CardLayout:
JPanel container = new JPanel(new CardLayout());
containter.add(getPanelA(), "PANEL_A");
containter.add(getPanelB(), "PANEL_B");
Then, in your actionPerformed, when you want to show panelB, you do this:
CardLayout cl = (CardLayout) container.getLayout();
cl.show("PANEL_B");
Take a look at this tutorial for some more ideas.
For some reason, I can never to get setVisible() to work for me to do what you're describing. Instead, I do this:
frame.remove(panelA);
frame.add(panelB);
"frame" is just the JFrame you want to put the panels in. Try this if the setVisible() method doesn't work :)
To your original question, all you have to do is (like aioobe said):
panelB.setVisible(true);
((btw, posting some of your code would help me figure out what you're trying to ask))
And this is just a guess as to what you're trying to do -- I'm guessing your JPanels are in different classes. Then, you'll need to do this:
class pages extends JFrame implements ActionListener
{
public pages()
{
panelA a = new panelA(this)
}
changeToA(panelB b)
{
remove(panelB);
add(new panelA(this));
}
changeToB(panelA a)
{
remove(panelA);
add(new panelB(this));
}
}
class panelA extends JPanel implements ActionListener
{
pages p;
public panelA(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToB(this);
}
class panelB extends JPanel implements ActionListener
{
pages p;
public panelB(pages p)
{
this.p = p
}
// all that actionlistener code stuff
p.changeToA(this);
}
You pass the pages class to the panels so the panels can tell the pages class to remove themselves.
((I don't know if there is an easier way, but this is what I do all the time))
I hope I helped :)
You have to remove Panel A from the frame, add Panel B to the frame, and call invalidate on the frame (or containing panel). At least in Swing, I'm not sure about AWT, there you might need repaint or revalidate instead of invalidate.
You could also just create a whole new JFrame and dispose the one containing panel A.
My program have 3 classes. 1) main, 2) frame, 3) drawingBoard. The logic of my program is that, a new drawing will be displayed every times user click on New pattern button (and this working fine).
1st class - main method
public class mainPage {
public static void main(String[]args){
JFrame appFrame = new Frame();
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
}
}
2nd class - describe the layout (I use Grid Bag Layout)
public class Frame extends JFrame implements ActionListener {
public Frame (){
GridBagLayout m = new GridBagLayout();
Container c = (Container)getContentPane();
c.setLayout (m);
GridBagConstraints con;
JButton bPattern = new JButton("New Pattern");
....
bPattern.addActionListener(this);
JPanel pDraw = new JPanel();
.....
pDraw.add(new drawingBoard()); //drawing will be placed in this panel
}
public void actionPerformed(ActionEvent e) {
repaint();
}
}
3rd class - run drawing functions e.g. paintComponent (), etc.
public class drawingBoard extends JPanel {
public drawingBoard(){}
public void paintComponent(Graphic g){}
....
}
The problem is that, when I look on the console, it seems that even though the user did not click on the button, the program call the class 'drawingBoard' and repaint. The paint component is in the 3rd class (drawingBoard). Although this seem not to give me a problem (e.g. no drawing displayed on the panel unless the user click the button), I am just curious how this happened. is that because I wrote this code at FRAME class (). My intention to write this code is to make sure the drawing should be place in this specific panel (I have 3 panels) but not to call the 3rd class unless the button has been clicked.
JPanel pDraw = new JPanel();
pDraw.add(new drawingBoard()); //place drawing here
The repaint method (and subsequently, the paintComponent method) is not only called by the JFrame but also by Swing itself as well, when there needs to be a repaint of the contents of the JPanel.
The Painting in AWT and Swing article is a good place to start to get information on how painting works.
In this case, the repaint method is being called by events which the article calls System-triggered Painting:
In a system-triggered painting
operation, the system requests a
component to render its contents,
usually for one of the following
reasons:
The component is first made visible on the screen.
The component is resized.
The component has damage that needs to be repaired. (For example,
something that previously obscured the
component has moved, and a previously
obscured portion of the component has
become exposed).