Calling paint() from another class in Java - Yet Again

Viewed 58

I have searched this site for answers but cannot see a solution to my problem.

I have a JComponent on a JFrame with a mouse listener and other computational classes in my program. When I call ShowImage(..) from MousePressed I get the image and see the printouts I have used to track the flow

ShowImage
Now 1 true
Now 2 true

If I call ShowImage(..) from another class I see

ShowImage
Now 1 true

This means that repaint() is not activating paintComponent by the call from another class! Can any one assist with this?

public class Main {
    public static void main(String[] args) {
        MyFrame MyFrame = new MyFrame();
        Game Game = new Game();
        Game.StartGame();
    }
}

public class Game {

    public void StartGame()
    {
        Display Display = new Display();
        // lots of other stuff here
        Display.ShowImage(139, 120, true);
    }
}

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class MyFrame extends JFrame implements ActionListener{
    public static final int Width = 1500;
    public static final int Height = 900;
    Display Display  = new Display();    

    public void DisplayImage()
    {
        Display.ShowImage(139, 120, true);
    }

    MyFrame()
    {
        this.setBounds(0, 0, Width, Height);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(Display);
        this.setLayout(null);   
        this.getContentPane().addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                System.out.println("From Mouse Pressed on MyFrame");
                Display.ShowImage(e.getX(), e.getY(), true);
            }
        });   // end of listener
        this.setVisible(true);
    }
}  // end class

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Display extends JComponent
    {
    public static BufferedImage img = null;
    private int xx;
    private int yy;
    private boolean Now=false;
    
    public Display() 
    {
    this.setBounds(0, 0, MyFrame.Width-200, MyFrame.Height);
    this.setLayout(null);   
    }

    public void ShowImage(int xx, int yy, boolean Now)
    {
        System.out.println("ShowImage");
        this.xx = xx;
        this.yy = yy;
        this.Now = Now;
        System.out.println("Now 1 "+ Now);
        repaint();
    }

    public BufferedImage GetImage()
    {
        try {
            img = ImageIO.read(new File("10C.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return img;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (Now)
        {
            System.out.println("Now 2 "+ Now);
            g.drawImage(GetImage(),xx,yy,null);
        }
    }
}  // end of class
0 Answers
Related