Results 1 to 3 of 3

Thread: java graphics-animation

  1. #1

    Thread Starter
    Lively Member maki's Avatar
    Join Date
    Mar 2006
    Location
    Greece
    Posts
    82

    java graphics-animation

    Hallo!

    I am new in java programming and i am trying to create animation in java graphics. What i am trying to do is give motion to a simple line. The code that i have created so far is the following:
    Code:
     import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class My extends JPanel implements ActionListener {
    
    
        private JFrame f;
       
        private JMenuBar jmb;
        private JMenu selectMenu;
        private JMenuItem quitItem;
        private JMenuItem againItem;
        private JMenuItem helloItem;
    
        private int horizontialDimension;
        private int verticalDimension;
    
        private int index;
    
        Graphics display;
    
        Font font;
    
        InnerThread in;
    
        Thread tr; 
    
    
        public My() {
    
         setSize(600,400);
    
         f = new JFrame("My");
    
         f.setSize(600, 400);
    
         jmb = new JMenuBar();
         selectMenu = new JMenu("Select");
         quitItem = new JMenuItem("Quit");
         againItem = new JMenuItem("Do it Again");
         helloItem = new JMenuItem("Say Hello");
         quitItem.addActionListener(this);
         againItem.addActionListener(this);
         helloItem.addActionListener(this);
    
         selectMenu.add(quitItem);
         selectMenu.add(againItem);
         selectMenu.add(helloItem);
         jmb.add(selectMenu);
         f.getContentPane().add(jmb, BorderLayout.NORTH);
         f.getContentPane().add(this, BorderLayout.CENTER);
    
         horizontialDimension = getSize().width;
         verticalDimension = getSize().height;
    
         f.setVisible(true);
    
         display = getGraphics();
    
         font = new Font("TimesRoman", Font.BOLD, 36);
       
         
         } 
    
    
         public void paint(Graphics g) {
    
           g.setColor(Color.blue);
           g.drawLine(220,100,220,120);
          //left arrow    
         
    	   
        
    }
         public void update(Graphics g) {
    	   for (int x=220; x<300; x++){
    	    int y1=(int)(x);
            int y2=(int)(x);
         
    		g.drawLine(y1,100,y2,120);
            
    		
     }
    	           
        }
    
        public void actionPerformed(ActionEvent ae){
    
          if(ae.getSource()==quitItem) {
         
           System.exit(0);
    
          } else if(ae.getSource()==againItem) {
    
           in = new InnerThread();
    
           tr = new Thread(in);
    
           tr.start();
    	   
    
           System.out.println("Again!!!");
         
          } else {
      
            System.out.println("Hello!!!");
     
          }
    
       
         }
    
    
    
    
          public static void main(String[] args){
          
           My st = new My();
    
           InnerThread stin = st.new InnerThread();
    
           Thread t = new Thread(stin);
    
           t.start();
    
          }
    
    
    
    
          public class InnerThread implements Runnable {
    
          
            public void run() {
    
               
    
               My.this.update(display);
    
                try {
     
                Thread.currentThread().sleep(100);
    
                } catch(InterruptedException ie) {}        
    
            }
    
    
          }
    
    
    
    }
    I think that the problem is in the class update. When i run the program i get a rectungle instead of a moving line. Please Help me!

  2. #2
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: java graphics-animation

    in your update() code, you were not updating the line, instead, you draw a new line everytime it loop, the for loop, looped 80 times, and u drew 80 blue lines, but you forgot to clear the old line, that's why all the new lines and old lines all piled up, forming a rectangle

  3. #3

    Thread Starter
    Lively Member maki's Avatar
    Join Date
    Mar 2006
    Location
    Greece
    Posts
    82

    Re: java graphics-animation

    The problem is that i do not know how to update the line

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width