Hi guys, It is argent situation please help
I have 3 classes One of them is named
Canvas which Contains methods for drawing some shapes such
as (Rectangle, Circle, and String).The Second class is named
animationController which makes some animation
such as moving string, circle by using instance of the
Canvas Class .The third class is named
Frame which
extends JFrame and Contains jButton. What I am trying to do is that, when I click on the jButton the showing
should start on the Canvas .The displaying is sequence (for example after the text movement is finished, the movement
of the circle should start).The problem is that when I click on the JButton the shapes do not appear on the Canvas until
all the drawing is finished. It does not show the movement of the shapes. My experience with graphic is very poor, but I
guess the problem is related to paintComponent () method which might be called once. That because when I invoke the
animationController inside the constructor of the Frame class it works right and shows the movement of the shapes, but I need it
to work when I click on JButton, first what is the reason behide this and second how can I do that using anyway even going around
the construcor . this structure of the classes.
public class Canvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColour;
private Image canvasImage;
private static Canvas canvasSingleton;
/**
* Factory method to get the canvas singleton object.
*/
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("BlueJ Shapes Demo", 700, 700,
Color.white);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
public Canvas(String title, int width, int height, Color bgColour)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColour = bgColour;
frame.pack();
}
result;
}
// here is some of the motheds Methods
public boolean drawImage(Image image, int x, int y)
{
boolean result = graphic.drawImage(image, x, y, null);
canvas.repaint();
return result;
}
public void drawString(String text, int x, int y)
{
graphic.drawString(text, x, y);
canvas.repaint();
}
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e) { }
}
.
.
. // the rest of the methods
private class CanvasPane extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(canvasImage, 0, 0, null);
}
}
}
//here is the class animationController I did not past all the class methodes just some of them
Class animationController
public class animationController
{
private int height ,width, xPosition, yPosition,Position;
private String Pointer;
public animationController ()
{
height = 120; width=120; xPosition = 250; yPosition = 150;Pointer="^";
Position=0;
show();
}
public void show()
{
height = 40; width=100;
xPosition = 12; yPosition = 70;
color=Color.red;
for(int i=0;i<5;i++)
{
MovingStringVertical("Step2",xPosition, yPosition,i,color);
yPosition=yPosition+55;
}
xPosition = 0;
yPosition = 400;
for(int x=0;x<5;x++)
{
xPosition= xPosition+width;
draw(xPosition,yPosition,height,width,x);
}
}
private void MovingStringVertical(String text ,int x,int y,
int distance,Color color)
{
Canvas canvas = Canvas.getCanvas();
Int delta = 8;
int check=0;
for(int i = 0; i < distance; i++)
{
check++;
y += delta;
canvas.setForegroundColour(color);
canvas.drawString(text,x,y);
canvas.wait(100);
if((check==4))
{
color=Color.blue;
for(int j=0;j<6;j++)
{
MovingStringHorizontal("Reserving the size of the Array",x,y+40,j,color,false);
x=x+25;
}
}
else
canvas.eraseString(text,x,y);
}
}
private void draw(int xPosition,int yPosition,int height,int width,int c)
{
Canvas canvas = Canvas.getCanvas();
canvas.drawString(Pointer,xPosition+width/2,yPosition-height/2);
canvas.setForegroundColour(Color.green);
canvas.drawString(Integer.toString(Position++),xPosition+width/2,yPosition-1);
canvas.drawRect(xPosition,yPosition,width,height);
canvas.wait(500);
if (c<4)
canvas.eraseString(Pointer,xPosition+width/2,yPosition-height/2);
canvas.wait(300);
}
//this code inside the class Frame where the user suppse to see the animation
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
animationController ac=new animationController();
}