participate


Swing - problem with Graphic in swing
This question is not answered.

<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Fabris
Posts:5
Registered: 9/19/09
problem with Graphic in swing   
Nov 2, 2009 8:42 PM
 
 
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();                                
              
    }                  
 
 
DarrylBurke
Posts:15,598
Registered: 7/2/07
Re: problem with Graphic in swing   
Nov 2, 2009 8:52 PM (reply 1 of 5)  (In reply to original post )
 
 
For animation effects, don't use Thread#sleep(...), use a javax.swing.Timer. Follow the link to the Swing Tutorials from the topic listing for this forum and go through the "Concurrency in Swing" trail.

db
 
Fabris
Posts:5
Registered: 9/19/09
Re: problem with Graphic in swing   
Nov 3, 2009 5:54 AM (reply 2 of 5)  (In reply to #1 )
 
 
thanks alot for your help
 
DarrylBurke
Posts:15,598
Registered: 7/2/07
Re: problem with Graphic in swing   
Nov 3, 2009 8:50 AM (reply 3 of 5)  (In reply to #2 )
 
 
If that solved your problem, it is expected of you that you mark the question as answered.

db
 
Fabris
Posts:5
Registered: 9/19/09
Re: problem with Graphic in swing   
Nov 3, 2009 8:22 PM (reply 4 of 5)  (In reply to #3 )
 
 
It doesn't work I still have the same problem even when I used Timer
 
Encephalopathic
Posts:13,715
Registered: 12/20/07
Re: problem with Graphic in swing   
Nov 3, 2009 8:33 PM (reply 5 of 5)  (In reply to #4 )
 
 
Fabris wrote:
It doesn't work I still have the same problem even when I used Timer
Sorry, but "it doesn't work" tells us squat. Without seeing your new code, who knows what the heck you're doing wrong now. Much luck!
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums