0

After making an instance in a previous frame, I'm trying to the background image on the next frame but as a result, I just saw the debugged result and found out that the paint method was not called. From what I know, the paint method is inherited by the JFrame class and with this logic, I've made it overrided. As I guess, the reason happen the logical error is from what I used the event handler and made the instance in the EventHandlerClass.

    if(e.getActionCommand().equals(ButtonTo))       
        if(idString.equals("USER"))
                {                       
                    {
                        if("1234".equals(pwSt))     
                        {
                            System.out.println("Wellcome");
                            if(gs==null)
                            {
                                gs=new GameStart();
                            }
                        }
                    else
                    {
                         System.out.println("Confirm your password");
                    }               
                    }           
                }

This is a code that If an action is performed it will make an instance(gs). After doing this, I noticed that the instance has been used as to make a new console frame.

class GameStart extends JFrame {
    private Image screenImage;
    private Graphics screenGraphic;
    private Image introBackgroundImage;
    private ImageIcon img;

    GameStart()
    {
        JFrame jf=new JFrame("Game Set");
        jf.setBounds(300, 300, 400, 200);
        jf.setLayout(new BorderLayout());

        JButton bt1=new JButton("Start");
        JButton bt2=new JButton("Exit");    
        JPanel panel1=new JPanel();
        panel1.add(bt1);panel1.add(bt2);

        setContentPane(panel1);

        jf.add(panel1, BorderLayout.SOUTH);
        bt1.addActionListener(new Choice());
        bt2.addActionListener(new Choice());
        jf.setVisible(true);    
        img=new ImageIcon("./Images/backGroundImage.jpg");
        System.out.println("1");
    }

    public void paint(Graphics g) {
        screenImage=createImage(200, 200);  
        screenGraphic=screenImage.getGraphics();
        screenDraw(screenGraphic);
        g.drawImage(screenImage, 0, 0, null);
        System.out.println("2");
    }

    public void screenDraw(Graphics g) 
    {   
        this.repaint();
        System.out.println("3");
    }

Now, With making a frame and some buttons, I expect to show all the numbers(1, 2, 3) that indicate the result but Just did number 1.

acarlstein
  • 1,799
  • 2
  • 13
  • 21
Akses
  • 21
  • 2
  • 1
    Don't override paint() on a JFrame. Custom painting is done by overriding `paintComponent(...)` of a JPanel and then you add the panel to the frame. Read the section from the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and working examples to get your started. You can also check out [Background Panel](https://tips4java.wordpress.com/2008/10/12/background-panel/) which is a class that you can use to easily display an image as a background. – camickr Apr 10 '19 at 19:35
  • 1
    Can you please flesh out your code example? In particular, you have an `if` statement that should be inside a method which is inside a class. – Code-Apprentice Apr 10 '19 at 19:48
  • *"in a previous frame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 11 '19 at 03:21
  • 1
    Please post [mcve] including `createImage()`. **Do not** call `repaint` from within `paint` – c0der Apr 11 '19 at 09:31

1 Answers1

0

There are some errors in your code that I can see at first glance:

  1. You're extending JFrame, but you're not adding any extra functionality to it, see: Extends JFrame vs. creating it inside the program. Instead, build your GUI towards the use of JPanels and override their paintComponent(...) method and not the paint(...) one.

  2. You're breaking the paint-chain: After doing the above point, in paintComponent(), call super.paintComponent(...)

Maybe there are others but I'm currently busy and can't test your code, but the ones above should help with your issue.

Frakcool
  • 10,915
  • 9
  • 50
  • 89