• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in using paint

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I am using below code to move a rectangle on a Canvas -
First time when i run my program, rectange comes fine but when i click on the move button it doesn't.
Below is my code to do that -

public void actionPerformed(ActionEvent ae)
{
for (int i=0;i<10 ;i++ )
{
(MyPictureAct.x)++;
mt.repaint(); ///mt is object reference of my Canvas object
System.out.println(MyPictureAct.x);

try{
Thread.currentThread().sleep(1000);
}catch(Exception e){}
}
}



public void paint(Graphics g)
{
g.draw3DRect(MyPictureAct.x,70,20,20,true);

}
}

Could you please tell me what is the wrong in this or is there any another good way of doing that.
Its printing the increasing value of x in for loop so loop is fine.

Thanks
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please read it as -

First time when i run my program, rectange comes fine but when i click on the move button it doesn't move.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure super.paint(g) is the first call in your paint method, then continue with your own code.
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i see in API,
The default operation is simply to clear the canvas. Applications that override this method need not call super.paint(g).
but even after using it no luck.
seems it not entering in the paint method when i call repaint.
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is my code -


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

public class MovingPic extends JApplet implements ActionListener
{
static int x= 10;
JButton b;
MyCanvas mt;

public void init()
{
mt = new MyCanvas();
b = new JButton("OK");

setLayout(new BorderLayout());

add(b, BorderLayout.SOUTH);
add(mt, BorderLayout.CENTER);
b.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
for (int i=0;i<10 ;i++ )
{
(MovingPic.x)++;
mt.repaint();
System.out.println(MovingPic.x);

try{
Thread.currentThread().sleep(1000);
}catch(Exception ae){}
}

}
}



class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
super.paint(g);
System.out.println("MMMMMMMMM");
g.draw3DRect(MovingPic.x,70,20,20,true);
}
}



While iterating through for loop its not going inside paint even though i am calling it via repaint().
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that you want to mix Swing components (JApplet) with AWT components (Canvas)? I think that doing this without need and knowledge is a recipe for disaster. Instead I recommend that you use a JPanel to draw on, that you override its paintComponent method, and that you call super.paintComponent(g) as the first call in this override.

Best of luck.

edit: you're also calling thread.sleep on the EDT, or Event Dispatch Thread, which will make your program almost completely non-responsive. The EDT is the single Swing thread responsible for user interaction (button presses and keyboard input, etc...) and for drawing components. When you put it to sleep, the app can't interact with the user or draw itself.

Instead of Thread.sleep, please look into using a Swing Timer here.

For instance:
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pete,
Even if i use Applet instead of JApplet samething happens. don't know as to why its not entering in paint method.
also with JPanel if i want to call paintComponent again as i did using repaint in canvas then we do have similiar method here too.
rePaint(x,y,wid,ht)... but again same results as i got with canvas.. it not entering inside paintComponent from for loop.
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, i didn't see your modified code earlier.. let me try this..
Thanks
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After reading Timer class i understood this code...
its really good..
Thanks a lot, pete.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so Manoj, is everything working as it should in your case?
i also just opened my own thread about issues with painting custom JComponents, but the problem is a little different i guess since i dont use any layout manager...
anyway, might still be helpful to somebody or even someone can give me a hint...

my thread about painting custom JComonents

regards, alex
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic