How do I change what's being displayed in Java Swing? - java

I don't really know how to word this question, but I can explain what I'm trying to do.
I have my program drawing a basic menu with a background image and 3 buttons along the bottom. I want to make it so that when I press one of the buttons, the menu and the background image change to a different scene. Like pressing play to start a game or pressing options to go to an option menu.
This is my code for the primary menu:
private final void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
add(panel);
panel.add(Box.createVerticalGlue());
JPanel bottom = new JPanel();
bottom.setAlignmentX(1f);
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
JButton plyBtn = new JButton("Play");
plyBtn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent event){
GameFrame_Test1 game1 = new GameFrame_Test1();
game1.setVisible(true);
}
});
JButton opBtn = new JButton("Options");
JButton quitButton = new JButton("Exit");
quitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
bottom.add(plyBtn);
bottom.add(Box.createRigidArea(new Dimension(100, 0)));
bottom.add(opBtn);
bottom.add(Box.createRigidArea(new Dimension(100, 0)));
bottom.add(quitButton);
bottom.add(Box.createRigidArea(new Dimension(100, 0)));
panel.add(new GraphicTest_1());
panel.add(bottom);
panel.add(Box.createRigidArea(new Dimension(0,15)));
setTitle("Justice GUI");
setSize(1280, 720);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DisplayFrame ex = new DisplayFrame();
ex.setVisible(true);
}
});
}
This is my code for the frame I want to change to so far:
class GameFrame_Test1 extends JPanel{
private Image mshi;
public GameFrame_Test1() {
loadImage();
setSurfaceSize();
}
private void loadImage() {
mshi = new ImageIcon("content/Placeholder_Map.png").getImage();
}
private void setSurfaceSize() {
Dimension d = new Dimension();
d.width = mshi.getWidth(null);
d.height = mshi.getHeight(null);
setPreferredSize(d);
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(mshi, 0, 0, null);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
I am very new to Java Swing, and this is a test program for me to learn how to use it. I have looked online for a solution, but not being able to word my question makes it hard. Thanks to anyone who can help!

I want to make it so that when I press one of the buttons, the menu and the background image change to a different scene.
You should be using a Card Layout as the layout manager of the content panel. Then you can swap a different panel on the frame when a button is clicked.
Check out the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Related

Issue when adding two JPanels

I'm trying to add two JPanels to a JFrame, one with a simple backround and another one with buttons etc. Either I get only buttons or only the background. I can't find a solution to my problem anywhere, so any help would be appreciated. I'm still new to Java, so please don't hate.
GuiMainMenu:
public class GuiMainMenu extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = -7936366600070922227L;
Color blue = new Color(114, 137, 218);
Color gray = new Color(44, 47, 51);
Color white = new Color(255, 255, 255);
ImagePanel panel = new ImagePanel(new ImageIcon("image.png").getImage());
public static int width;
public static int height;
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JLabel l = new JLabel();
JLabel l1 = new JLabel();
JLabel l2 = new JLabel();
JButton b = new JButton();
JButton b1 = new JButton();
JButton b2 = new JButton();
String title = "-";
public GuiMainMenu() {
setSize(m.X, m.Y);
setTitle(title);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
p.setLayout(null);
width = getWidth();
height = getHeight();
getRootPane().addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
width = getWidth();
height = getHeight();
addButtons();
addLabels();
}
});
addLabels();
addButtons();
add(p);
add(panel);
setVisible(true);
}
public void addLabels() {
l.setSize(width, height);
l.setLocation(5, -40);
l.setText("x");
l.setHorizontalAlignment(SwingConstants.LEFT);
l.setVerticalAlignment(SwingConstants.BOTTOM);
l.setForeground(blue);
l.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
l1.setSize(width, height);
l1.setLocation(-22, -40);
l1.setText("y");
l1.setHorizontalAlignment(SwingConstants.RIGHT);
l1.setVerticalAlignment(SwingConstants.BOTTOM);
l1.setForeground(blue);
l1.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
l2.setText("test label");
l2.setSize(width, height);
l2.setLocation(0, -75);
l2.setVerticalAlignment(SwingConstants.CENTER);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setForeground(blue);
l2.setFont(new Font("Trebuchet MS", Font.BOLD, 26));
p.add(l);
p.add(l1);
p.add(l2);
validate();
}
public void addButtons() {
b.setText("button0");
b.setFocusable(false);
b.setBorder(null);
b.setLocation(width / 2 - 200, height / 2 - 35);
b.setSize(400, 35);
b.setForeground(white);
b.setBackground(blue);
b.addActionListener(this);
b1.setText("button1");
b1.setFocusable(false);
b1.setBorder(null);
b1.setLocation(width / 2 - 200, height / 2 + 10);
b1.setSize(400, 35);
b1.setForeground(white);
b1.setBackground(blue);
b1.addActionListener(this);
p.add(b);
p.add(b1);
validate();
}
#Override
public void actionPerformed(ActionEvent arg0) {
}
#Override
public void keyPressed(KeyEvent arg0) {
}
#Override
public void keyReleased(KeyEvent arg0) {
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
ImagePanel Class:
class ImagePanel extends JPanel {
private static final long serialVersionUID = -7270956677693528549L;
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, GuiMainMenu.width, GuiMainMenu.height, null);
}
}
p.setLayout(null);
Don't use a null layout. Swing was designed to be used with layout managers.
Read the section from the Swing tutorial on Layout Managers for more information and working examples.
Either I get only buttons or only the background
add(p);
add(panel);
The default layout manager for a JFrame is the BorderLayout. If you don't specify a constraint both components get added to the CENTER. However only the last one added will be displayed.
Try the following to see the difference:
add(p, BorderLayout.PAGE_STRT);
add(panel, BorderLayout.CENTER);
one with a simple backround and another one with buttons etc.
However above is not what you want. Swing components have a parent/child relationship. So what you really need is:
backgroundPanel.add(buttonPanel);
add(backgroundPanel, BorderLayout.CENTER);
Note I used more meaningful names because "p" and "panel" and not descriptive. Use descriptive names for variable so people can understand what they mean.
I suppose you want to display a JFrame with a background image and various components. I think after reviewing your code that there is some misunderstanding on your part causing the problem.
I've created a short snippet of code that does the basics and maybe helps you to solve your problem. (Not tested!)
#SuppressWarnings("serial")
public class GuiMainMenu extends JFrame{
private BufferedImage imageBackground; // TODO: load your background image
public GuiMainMenu(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1024, 768));
setMinimumSize(new Dimension(800, 600));
// TODO: setLayout if needed
JPanel panel = (JPanel)add(new JPanel(){
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageBackground, 0, 0, this);
}
});
addOtherComponents(panel);
pack();
setLocationRelativeTo(null);
setTitle("Your Title her");
setVisible(true);
}
private void addOtherComponents(JPanel panel){
//TODO: add the needed Stuff
//panel.add ...
}
}

Glasspane does not work, java

I have been working on a little java application, and I wanted to add a waiting graphics on the glasspane of the rootpane of the application, here is the classes:
public class WaitPanel extends JPanel {
public WaitPanel() {
this.setLayout(new BorderLayout());
JLabel label = new JLabel(new ImageIcon("spin.gif"));
this.setLayout(new BorderLayout());
this.add(label, BorderLayout.CENTER);
this.setOpaque(false);
this.setLayout(new GridBagLayout());
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
me.consume();
Toolkit.getDefaultToolkit().beep();
}
});
}
public void paintComponent(Graphics g) {
g.setColor(new Color(0, 0, 0, 140));
g.fillRect(0, 0, getWidth(), getHeight());
}}
and the main class :
public class NewJFrame extends JFrame {
public NewJFrame() {
JButton button =new JButton("Click");
getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
}
});
}
but when I change the button action to :
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
Scanner sc=new Scanner(System.in);
String s=sc.next();
getRootPane().getGlassPane().setVisible(false);
It does not work.
Your problem (one of them) is that your code freezes the Swing event thread with its use of a Scanner based on System.in, and in so doing prevents the GUI from updating its graphics, including it's glass pane. Solution -- don't do that. If you want to block the GUI or pause it, use a Swing Timer, or a JOptionPane.
For instance, you could change
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
Scanner sc=new Scanner(System.in);
String s=sc.next();
getRootPane().getGlassPane().setVisible(false);
to something like this:
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
int delay = 4 * 1000; // 4 second delay
new javax.swing.Timer(delay, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRootPane().getGlassPane().setVisible(false);
((javax.swing.Timer) e).stop();
}
}).start();

KeyEvent won't change background color of JPanels

I want the KeyEvent to change the background color of my JPanels. Nothing happens when I press anything on the keyboard. One of my applications specifications is that I need a 'Customized component extended from JPanel.' which is why I have another class for my graphics panel.
My problem is when G is pressed nothing happens but my center panel should turn green...
Here is code for part of my application.
public class Maths extends JFrame implements KeyListener
{
private JPanel pNorth = new JPanel();
private JPanel pSouth = new JPanel();
private JPanel pCenter = new JPanel();
private JPanel pEast = new JPanel();
private JPanel pWest = new JPanel();
private File file;
private JPanel pDraw = new GraphicsPanel();
public static void main(String args[])
{
new Maths();
}
public Maths()
{
mainFrame = new JFrame();
mainFrame.setTitle("Maths Test Game");
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(1200, 800);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(pNorth, BorderLayout.NORTH);
mainFrame.add(pSouth, BorderLayout.SOUTH);
mainFrame.add(pCenter, BorderLayout.CENTER);
mainFrame.add(pEast, BorderLayout.EAST);
mainFrame.add(pWest, BorderLayout.WEST);
pNorth.setLayout(new FlowLayout());
pSouth.setLayout(new FlowLayout());
pCenter.setLayout(new FlowLayout());
pEast.setLayout(new FlowLayout());
pWest.setLayout(new FlowLayout());
addKeyListener(this);
setFocusable(true);
mainFrame.setVisible(true);
}
class GraphicsPanel extends JPanel
{
GraphicsPanel()
{
// set a preferred size for the custom panel.
setPreferredSize(new Dimension(250, 300));
}
#Override
public void paint(Graphics g)
{
super.paint(g);
// set blue color for drawing
g.setColor(Color.blue);
// face
g.drawOval(90, 70, 80, 80);
// eyes
g.drawOval(110, 95, 5, 5);
g.drawOval(145, 95, 5, 5);
// nose
g.drawLine(130, 95, 130, 115);
// mouth
g.drawArc(113, 115, 35, 20, 0, -180);
}
}
#Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_G)
{
pCenter.setBackground(Color.green);
}
repaint();
}
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
}
There are a multitude of reasons this is probably not working, generally, you don't want to attach KeyListeners to top level containers like JFrame, as they is simply to many things that can get in the way and prevent the frame from raising key events.
Instead, use the key bindings API. See How to Use Key Bindings for more details

how to paint images on clicking button in frame?

I've made two buttons in frame .I want to know how to display different images on clicking different buttons?
is there another way out or i have to make panel?I am at beginner stage
package prac;
import java.awt.*;
import java.awt.event.*;
public class b extends Frame implements ActionListener{
String msg;
Button one,two;
b()
{ setSize(1000,500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
one=new Button("1");
two=new Button("2");
add(one);
add(two);
one.addActionListener(this);
two.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
msg=e.getActionCommand();
if(msg.equals("1"))
{
msg="Pressed 1";
}
else
msg="pressed 2";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,300);
}
public static void main(String s[])
{
new b();
}
}
Use JLabel and change the icon when button is clicked.
Some points:
call setVisible(true) in the end after adding all the component.
use JFrame#pack() method that automatically fit the components in the JFrame based on component's preferred dimension instead of calling JFrame#setSize() method.
sample code:
final JLabel jlabel = new JLabel();
add(jlabel);
final Image image1 = ImageIO.read(new File("resources/1.png"));
final Image image2 = ImageIO.read(new File("resources/2.png"));
JPanel panel = new JPanel();
JButton jbutton1 = new JButton("Show first image");
jbutton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image1));
}
});
JButton jbutton2 = new JButton("Show second image");
jbutton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image2));
}
});
panel.add(jbutton1);
panel.add(jbutton2);
add(panel, BorderLayout.NORTH);

JPanel won't move if it fills the screen

First of all, sorry for the vague title I don't know how to word the question in a sentence.
I have a simple programme that slides one JPanel into view as another gets pushed out, when a button is clicked.
If the first JPanel's width is set as getWidth() then the JPanel will not move when the button is clicked, however if I change the width to getWidth() - 1 it works perfectly fine!?!
A simple example is shown below
public class SlidingJPanel extends JFrame{
public JPanel panel = new JPanel();
public JPanel panel2 = new JPanel();
public JLabel label = new JLabel(" SUCCESS!!!!!!!");
public JButton button = new JButton("TESTING");
public class MyJPanel extends JPanel implements ActionListener{
public int x = 0;
public int delay = 70;
final Timer timer = new Timer(delay,this);
public MyJPanel(){};
public void paintComponent(Graphics g)
{
super.paintComponent(g);
button.setBounds(10, 20, 100, 50);
button.addActionListener(this);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setBounds(x, 0, getWidth(), getHeight());
panel.add(button);
panel2.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBounds(x - getWidth(), 0, getWidth(), getHeight());
panel2.add(label);
add(panel);
add(panel2);
}
#Override
public void actionPerformed(ActionEvent arg0) {
timer.addActionListener(move);
timer.start();
}
ActionListener move = new ActionListener(){
public void actionPerformed(ActionEvent e){
repaint();
x++;
}
};
}
public static void main(String args [])
{
new SlidingJPanel();
}
SlidingJPanel()
{
Container container = getContentPane();
MyJPanel panel = new MyJPanel();
container.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setTitle("JPanel Draw Rect Animation");
setVisible(true);
}
}
ignore any coding conventions I may have ignored or missed this is just a rough draft.
Hope someone can help :)
The paintComponent() method is for painting only! There is no need for you to override this method.
You should NOT be:
updating the property of components (ie. bounds, border)
adding components to a container
If you want to animate a component then when the timer fires you can use setLocation(...) or setSize() or setBounds(). The component will automatically be repainted.
I don't know if fixing this will solve your problem, but the current approach is wrong.

Categories

Resources