PaintComponent Does not 'Fire'
apologies to all if this is a double post but I have posted already
and I cannot find my post (It is late at night).
I am creating my very first java app using the MVC framework and
attempting to place an image on a JPanel (See the GraphicalView class
below). My problem is that I get the Frame with a white background and
Exit button (As I would expect) but the PaintComponent does not fire
(I have run the app in debug and it does not fire).
Can anyone please tell me either what I am missing or what I am doing
wrong.
Many thanks in advance for any assistance offered.
----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JPanel;
public class ViewGraphical extends JFrame implements Observer
{
private Model model; // The Model to register with and
receive updates from
private Controller controller; // The controller for events
Image imgDog = null;
JFrame theFrame;
JPanel p;
public ViewGraphical(Model m)
{
String lPath = "C:\\.......\\src\\";
String fileDog = lPath + "Dog.jpg";
Image imgDog = Toolkit.getDefaultToolkit().getImage(fileDog);
JFrame theFrame = new JFrame("My First Appliction");
theFrame.setSize(800, 600);
p = new JPanel();
p.setBackground(Color.WHITE);
theFrame.getContentPane().add(p, BorderLayout.CENTER);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitControl());
theFrame.getContentPane().add(exitButton, BorderLayout.SOUTH);
theFrame.setVisible(true);
theFrame.setAlwaysOnTop(true);
model = m;
model.addObserver(this); // Register this class with the Model
controller = new Controller(model); // Make the Controller using
the same Model
}
public void paintComponent(Graphics gr)
{
if (imgDog != null)
{
gr.drawImage(imgDog,0,0,48,48,this);
}
}
public void update(Observable arg0, Object arg1)
{
if(arg1!=null)
{
int n;
try
{
}
catch(NumberFormatException e)
{
}
}
}
}