0

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Jon doe
  • 13
  • 3
  • 1
    Your question is broad and is asking several things all at once, and I advice you not to do this -- fix one thing at a time, and ask about one thing, at a time. Having said that, 1) don't override paint, override paintComponent. Call the super's painting method in your own painting method (again, super.paintComponent(g)). 3) Never call repaint within a painting method. 4) If you need to redraw a static background image, then draw it as a BufferedImage and then re-draw just that where needed. 5) read any tutorial on Swing graphics. – Hovercraft Full Of Eels Jul 13 '23 at 19:13
  • And you are unable to Google the issue because you haven't yet *isolated* your separate issues. Again, do this first, and Google each individual issue in isolation. – Hovercraft Full Of Eels Jul 13 '23 at 19:14
  • And this does not appear to have anything to do with an issue with CardLayout – Hovercraft Full Of Eels Jul 13 '23 at 19:14
  • @HovercraftFullOfEels Actually I am asking about 1 thing in this post I am asking the cause of performance issues. – Jon doe Jul 13 '23 at 19:15
  • If you're asking about performance, then post a question where the question can be answered, including [mre] code that reproduces the issue for us. Again, I posit that this question, as written, is far too broad. – Hovercraft Full Of Eels Jul 13 '23 at 19:17
  • But yeah, get rid of the repaint, use paintComponent, and use a Swing Timer as your animation loop – Hovercraft Full Of Eels Jul 13 '23 at 19:17
  • @HovercraftFullOfEels I have posted a minimal repodrucible example this is the code I am running and I described what happens when I run it. – Jon doe Jul 13 '23 at 19:18
  • Then follow the recommendations that I've given you above. – Hovercraft Full Of Eels Jul 13 '23 at 19:19
  • Tutorials: [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). [Swing Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/index.html). [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html). – Hovercraft Full Of Eels Jul 13 '23 at 19:20
  • @HovercraftFullOfEels I am not sure you understand the question. – Jon doe Jul 13 '23 at 19:23
  • I understand that you are doing many many things wrong in this code, several of which can impact on "performance" none of which involve a problem with CardLayout. Again, fix all those bugs mentioned. – Hovercraft Full Of Eels Jul 13 '23 at 19:26
  • 1
    You are producing an "endless loop" by calling `repaint` inside of your implementation of the `paint` method. This will invoke `paint` again. But still: have a look at the linked duplicate, it should give you insights about performant painting in Swing. Also have a look at the javadoc of `paint` and `repaint`. – cyberbrain Jul 13 '23 at 19:27
  • Yes, @cyberbrain is correct, and his suggestions are as I already have mentioned above: never call paint within a painting method. Use a Swing Timer for your animation loop. This code is full of errors. – Hovercraft Full Of Eels Jul 13 '23 at 19:31
  • I'm not sure of the purpose of the `board()` method, since it is never called, and thus doesn't belong in your [mre]. If you do call it multiple times and if this drawing portion doesn't change (a non-dynamic or "static" image), then *again*, get rid of the method and instead create a BufferedImage with that drawing and simply draw the background with the buffered image. – Hovercraft Full Of Eels Jul 13 '23 at 19:37
  • Correction: never call `repaint()` within any painting method. This is a "poor-man's" animation loop, one that you cannot control in any reasonable way. – Hovercraft Full Of Eels Jul 13 '23 at 19:47
  • @HovercraftFullOfEels I understand what you mean now from the other lad too I needed things explained in simpeliar terms to understand it I sincerly thank you for your effort. I have now despite it not being related fixed the performence issue of my previous project. and will now be on my way for this one. – Jon doe Jul 13 '23 at 19:53
  • @cyberbrain thank you too. – Jon doe Jul 13 '23 at 19:53

0 Answers0