We help IT Professionals succeed at work.

how to request redraw of window

DJ_AM_Juicebox
on
514 Views
Last Modified: 2008-02-26
Hi,
I have a java applet I'm using to ltest out drawing functions. It overrides the following two functions:

    public void render(Graphics g);
    public void paint(Graphics g)

which is fine but how do you ask the system to perform a 'repaint'? For instance, I have a mouse click function after which I'd like the window to redraw itself, but I can't call render() or paint() directly because I have no Graphics obhect to pass them! In win32 drawing you can simply Invalidate() the display and paint() will be called automatically. But this is a completely different game. So how do you do it?

Thanks
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
repaint();
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
repaint() will queue a request to repaint the applet, which will trigger paint() to get called.

Author

Commented:
hmm that doesn't seem to work, though I'm not sure which of the 2 get executed. I have render() and paint() which look like:

public void paint(Graphics g)
{
        super.paint(g);
        render(g);
}

public void render(Graphics g)
{
    // Draw some graphics stuff in here...
    g.drawImage(m_ImgFnl, 0, 0, this);
}

Yeah so no update - I have to drag another window on top then remove it for the repaint to occurr. What am I doing wrong?

Thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
where are you calling repaint()?
CERTIFIED EXPERT
Top Expert 2016

Commented:
Is there any other processing going which could be blocking that painting thread?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
And make sure you are calling reapint() on the component that needs repaint. eg. your applet

Author

Commented:
hmm it looks like some operation is doing like a... silent crash? that prevents the full code to execute. When I comment out the problem block repaint() is called and it goes through ok. So I have to investigate this problem area.

repaint() seems to do a full erasure of the background then draws to the screen. Is there a way to override the erasing to block it? (looks like that's what's goin on anyways)

Thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sounds like an exception is being thrown inside your processing block
wrap a try/catch around it to see what happening

>         super.paint(g);

get rid of that line to remove the full erasure
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
though the approach u are using is going to give you a pretty poor refresh rate.

Author

Commented:
it's still flickering - this wasn't happening before, I think it's because of the repaint() call - before I had some insane thing where I'd save the Graphics instance as a member variable then call paint() directly with it! Wouldn't flicker then but I have no idea if that's a valid thing to do!
CERTIFIED EXPERT
Top Expert 2016

Commented:
If you know the coordinates you can call repaint in the this overload:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#repaint(long,%20int,%20int,%20int,%20int)

You may as well move the render code into paint
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
no thats not a valid thing to do :)
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Wouldn't flicker then but I have no idea if that's a valid thing to do!

No. The reference is only valid at the moment it's passed by the system
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
move your rendering into a JPanel (subclass), andf add that panel to your applet

public ImagePanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
      // do your rendering here
   }

    ...
CERTIFIED EXPERT
Top Expert 2016

Commented:
Why?

Author

Commented:
ehh well I doubt it has anything to do with where I'm placing my code. I think it's a problem with the fact that I'm iterating over each pixel in an image buffer, performing some calculation on it - everytime the mouse moves.

I wonder if there's a way to do pointer arithmetic with java? Or is that hidden in the language? I'm basically converting this C++ code to java but I've had to use array indexes which is kinda slow even in C++:

    for (int y = 0; y < cy; y++)
    for (int x = 0; x < cx; x++) {
        m_DstImg[y * cx + x] = some_pixel_value;
    }

yeah it goes a lot faster with pointer arithmetic.

Thanks

Author

Commented:
yeah the other problem is that this java BufferedImage class doesn't allow direct pixel access as far as I can tell, so I have to go through a really slow set of interface functions sadly. Anyway around these issues?

Thanks
CERTIFIED EXPERT
Top Expert 2016

Commented:
It allows access the the raster. See WritableRaster i mentioned earlier
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
i already told u that would be a problem :)
and where u place your code will make a difference, an applet subclass is noyt the place to do custom rendering.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> yeah the other problem is that this java BufferedImage class doesn't allow direct pixel access as far as I can tell, so I have to go > through a really slow set of interface functions sadly. Anyway around these issues?

read the comment i posted in earlier question which u have appeared to have ignored.

CEHJ,

Your comments continue to confuse me, have u actually ever done *any* real image processing in java?

Author

Commented:
ughhh ok maybe you did tell me, but I had to try of course! You think this WritableRaster class will improve performance? There must be some solution, I'm sure the java world has not forgotten about fast 2D drawing?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You think this WritableRaster class will improve performance?

It improves access to the pixel data of a BufferedImage
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
WritableRaster probably won't make much difference, depends what u r trying to do

Author

Commented:
alright well this has gotten off the original topic, I'll start a new post,

Thanks
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.