Win a copy of Eclipse Collections Categorically: Level up your programming game this week in the Open Source Projects forum!
Forums Login/signup

problem in using paint

+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Make sure super.paint(g) is the first call in your paint method, then continue with your own code.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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().
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
oops, i didn't see your modified code earlier.. let me try this..
Thanks
+Pie Number of slices to send: Send
After reading Timer class i understood this code...
its really good..
Thanks a lot, pete.
+Pie Number of slices to send: Send
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
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders


reply
reply
This thread has been viewed 1403 times.
Similar Threads
How does canvas, an exteded JPanel obj have methods getSize(), createImage(size.width, size.height)?
Adding a shape to a canvas
What am I doing wrong here?
Having two problem on paint
Why does my canvas not display in side a gridbaglayout?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jul 16, 2025 04:10:04.
close