Java cardlayout performence issues, is it the code or caused by something else?

25 Views Asked by At

So in my previous question I was informed of cardlayout and I tried to implement it in my code but I am having quite severe issues running the code. I am running Linux mint and visual studio code if that matters.

First when I press the red X to close the Jframe there is a 2 second delay before it closes. Secondly, when I try to move around the frame it pretty much messes my whole screen and everything on it. When I say everything I mean it I have to minimize Firefox to get it back to normal.

Essentially my question is since I cannot figure out from googling if the issue is caused by something in the code or something else I ask here.

The main class below essentially just copied code.

public class SpaceInvaders extends JFrame implements ActionListener  
{    
  
CardLayout crd;    
  
// button variables to hold the references of buttons  
JButton btn1, btn2, btn3;    
Container cPane;   
Board board = new Board();
  
// constructor of the class  
SpaceInvaders()  
{    
  
cPane = getContentPane();    
  
//default constructor used  
// therefore, components will   
// cover the whole area  
crd = new CardLayout();    
  
cPane.setLayout(crd);    
  
// creating the buttons  
btn1 = new JButton("Apple");      
  
// adding listeners to it  
btn1.addActionListener(this);       
  
cPane.add("a", btn1);
cPane.add(board); // first card is the button btn1  
//cPane.add("b", btn2); // first card is the button btn2  
//cPane.add("c", btn3);  // first card is the button btn3  

            
}    
public void actionPerformed(ActionEvent e)   
{    
// Upon clicking the button, the next card of the container is shown  
// after the last card, again, the first card of the container is shown upon clicking  
crd.next(cPane);    
}    
  
// main method  
public static void main(String argvs[])   
{     
// creating an object of the class CardLayoutExample1  
SpaceInvaders crdl = new SpaceInvaders();   
crdl.setTitle ( "Space invaders" );//Title of the screen
crdl.setSize ( 900, 900 );//Size of the windowc  
// size is 300 * 300          
crdl.setVisible(true);    
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);    
}    
} 

The board class with some random drawn things.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import javax.swing.JPanel;

public class Board extends JPanel {

    public Board(){

        setFocusable(true);
    }

    public void paint(Graphics g) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, 900, 900);
        g.setColor(Color.WHITE);            
        g.drawRect (20, 20, 864, 624);  
        g.setColor(Color.BLACK);
        g.fillRect (21, 21, 863, 623);
        g.setColor(Color.WHITE);    
        g.setFont(new Font("arial",Font.PLAIN,14));
        repaint();
    }

    public void board(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Stroke stroke1 = new BasicStroke(4f);
        g2d.setColor(Color.white);
        g2d.setStroke(stroke1);
        g2d.drawRect(20, 50, 850, 600);
        g2d.setColor(Color.white);
        float[] dashingPattern2 = {10f, 4f};
        Stroke stroke2 = new BasicStroke(4f, BasicStroke.CAP_BUTT,
        BasicStroke.JOIN_MITER, 1.0f, dashingPattern2, 0.0f);
        g2d.setStroke(stroke2);
        g2d.drawLine(448, 50, 448, 650);
        g.setFont(new Font("arial",Font.PLAIN,30));
    }


}

I have tried to google the issue but it does not seem to come up so I assume it caused by something else in the code.

I am thinking of coding the menu in a different way making a new menu class but I wanna have it done using Jbuttons.

0

There are 0 best solutions below