• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update method not called???  RSS feed

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I don't know why but update method is not being called in the program. Here's my code:

/**
*File Name = DrawArea.java
*Class Name = DrawArea
*
*Copyright (c) 2005
*
* @author Shyam Prasad Murarka
* @date Thursday, May 05, 2005
*/

import javax.swing.*;
import java.awt.*;

public class DrawArea extends JPanel implements Variables
{
int currentShape = CIRCLE;
Color currentColor = Color.black;
int currentMode = AUTOMATIC;
int currentAutomaticDirection = VERTICAL;
int currentSpeed = SLOWER;

final int paperw =400 , paperh =280;

public DrawArea()
{
repaint();
}//end of constructor

public void update(Graphics g)
{
System.out.println("Reached");
Image offScreenImage = createImage(paperw, paperh);
Graphics offScreenGraphics = offScreenImage.getGraphics();
offScreenGraphics.setColor(Color.white);
offScreenGraphics.fillRect(0,0,paperw,paperh);
offScreenGraphics.setColor(Color.black);
offScreenGraphics.drawRect(1,1,paperw-4,paperh-4);
paint(offScreenGraphics);
g.drawImage(offScreenImage,0,0,this);
}//end of method update()

public void paint(Graphics g)
{
System.out.println("OKKKKKKKK");
g.drawRect(0,0,32,37);
}//end of method paint()
}//end of class DrawArea
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you expect it to be called? I can't see where anything will be called. Do you have another class that is calling this? In the class you've posted there is no call to that method, not to mention there's no main method to get the program started. There must be more code that you haven't posted.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Hentay, I must tell you that whenever a call is given to repaint() then the program always executes the update() method before going to paint() method.
And about other classes, Yes, there is a class but that class does not do much it just adds this class(which extends panel) to its frame.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you meant to name this method paintComponent()? If you do so, then the event handler thread will call it automatically. Notice that it has to have the exact same signature as the paintComponent() method in the JComponent class, which is an ancestor to all Swing components including JPanel.

Layne
[ May 05, 2005: Message edited by: Layne Lund ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!