CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: GUI question.

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    GUI question.

    I got a code, where I used animation.
    The program gets some information via bluetooth and converts it, then it should move a picture to certain points. And it should keep going until all information is read.

    Now it receives all information, and image moves, but problem is that it make animation after all information have received. (i.e. it makes animation from start point and final point) But I need to make animation, which draws image immediately after receiving data. (step by step)

    This is my code:


    Code:
    public class Basic extends TimingTargetAdapter implements ActionListener {
        
       public static final int RACE_TIME = 900;    
        static Point start = new Point(94, 558);
        static Point end = new Point(94, 558);
        static Point current = new Point();
        protected Animator animator;
        TrackView track;
        RaceControlPanel controlPanel;
        // more parameters
    
    /** Creates a new instance of BasicRace */
        public Basic(String appName) {
            //Container container = getContentPane();
            RaceGUI basicGUI = new RaceGUI(appName);
            controlPanel = basicGUI.getControlPanel();
            controlPanel.addListener(this);
            track = basicGUI.getTrack();
            animator = new Animator(RACE_TIME, this);
        }
    
    /**
         * This receives the Go/Stop events that start/stop the animation
         */
        public void actionPerformed(ActionEvent ae) {
        	if (ae.getActionCommand().equals("Connect"))
        		connect();
    
            if (ae.getActionCommand().equals("Start")) {
    
                start();
                
            } else if (ae.getActionCommand().equals("Stop")) {
            	System.exit(0);
          	  	disconnect();
            }
        }
    
     public static void connect() { ...}
    
    public static void disconnect() { ... }
    
    public void start(){
    		  mazeInitial();
    		  xi = 0;
    		  yi = 7;
    		  try {
    			  outData.writeInt(1);
    			  outData.flush();
    			  while (true){
    				  transmitReceivedDirection = inData.readInt();
    				  transmitReceivedWall = inData.readInt();
    				  //System.out.println(transmitReceivedWall + " <<<------------");
    				  drawWall(transmitReceivedDirection, transmitReceivedWall, xi, yi);
    				  animator.stop();
    				  animator.start();
    
    			  }
            } 
    		  catch (IOException ioe) { System.out.println("\nIO Exception writeInt"); } 
    	  }
    
    public void mazeInitial() { ... }
    
    // Here I change position of the points
    public void dir(int number, int dir) {
    
    		  switch (number) {
    		  case 0: {  // R
    			  switch (dir){
    			  	case 0: { xi += 1; end.x += 70; break;}
    			  	case 1: { yi += 1; end.y += 70; break;}
    			  	case 2: { xi -= 1; end.x -= 70; break;}
    			  	case 3: { yi -= 1; end.y -= 70; break;}
    			  } break;
    		  }
    		  case 1: {  // R
    			  switch (dir){
    			  	case 0: { xi += 1; end.x += 70; break;}
    			  	case 1: { yi += 1; end.y += 70; break;}
    			  	case 2: { xi -= 1; end.x -= 70; break;}
    			  	case 3: { yi -= 1; end.y -= 70; break;}
    			  } break;
    		  }
                      ....
           }
    
    /**
         * TimingTarget implementation: calculate and set the current
         * car position based on the animation fraction
         */
        public void timingEvent(float fraction) {
        	
            // Simple linear interpolation to find current position
            current.x = (int)(start.x + (end.x - start.x) * fraction);
            current.y = (int)(start.y + (end.y - start.y) * fraction);
            
            // set the new position; this will force a repaint in TrackView
            // and will display the car in the new position
            track.setCarPosition(current);
          }
    
        public static void main(String args[]) {
            Runnable doCreateAndShowGUI = new Runnable() {
                public void run() {
                    Basic race = new Basic("Race");
                }
            };
            SwingUtilities.invokeLater(doCreateAndShowGUI);
        }   
    }
    
    
    class RaceControlPanel extends JPanel { ... }
    
    class RaceGUI {
    
        private TrackView track;
        private RaceControlPanel controlPanel;
    
        /**
         * Creates a new instance of RaceGUI
         */
        public RaceGUI(String appName) {
            ...
        }
        
        public TrackView getTrack() {
            return track;
        }
        
        public RaceControlPanel getControlPanel() {
            return controlPanel;
        }
    }
    
    class TrackView extends JComponent {
        
        BufferedImage car;
        BufferedImage track;
        Point carPosition;
        double carRotation = 0;
        int trackW, trackH;
        int carW, carH, carWHalf, carHHalf;
    
        /** Hard-coded positions of interest on the track */
        //static Point START_POS = new Point(94, 558);
        static Point START_POS = Basic.start;
        static Point END_POS = Basic.end;
        
        /** Creates a new instance of TrackView */
        public TrackView() {
            try {
                car = ImageIO.read(TrackView.class.getResource("qwe1.gif"));
                track = ImageIO.read(TrackView.class.getResource("track.jpg"));
            } catch (Exception e) {
                System.out.println("Problem loading track/car images: " + e);
            }
            carPosition = new Point(START_POS.x, START_POS.y);
            carW = car.getWidth();
            carH = car.getHeight();
            carWHalf = carW / 2;
            carHHalf = carH / 2;
            trackW = track.getWidth();
            trackH = track.getHeight();
        }
        
        public Dimension getPreferredSize() {
            return new Dimension(trackW, trackH);
        }
        
        /**
         * Render the track and car
         */
        public void paintComponent(Graphics g) {
            // First draw the race track
            g.drawImage(track, 0, 0, null);
            int x = 70, y = 530;
            
            // Now draw the car.  The translate/rotate/translate settings account
            // for any nonzero carRotation values
            Graphics2D g2d = (Graphics2D)g.create();
            g2d.translate(carPosition.x, carPosition.y);
            g2d.translate(-(carPosition.x), -(carPosition.y));
             
            // Now the graphics has been set up appropriately; draw the
            // car in position
            g2d.drawImage(car, carPosition.x - carWHalf, carPosition.y - carHHalf, null);
        }
        
        /**
         * Set the new position and schedule a repaint
         */
        public void setCarPosition(Point newPosition) {
            repaint(1, carPosition.x - carWHalf, carPosition.y - carHHalf, carW, carH);
            carPosition.x = newPosition.x;
            carPosition.y = newPosition.y;
            repaint(0, carPosition.x - carWHalf, carPosition.y - carHHalf, carW, carH);
        }
    }
    I tried to explain everything and paste almost whole code. (Maybe some unnecessary parts)
    I know it's annoying to look to somebody's code, but if you have time, and can understand it, could you help me?

    If have any question I'll explain it.

    Thank you in advance.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: GUI question.

    I'm guessing because I can't easily understand your code, it looks like loads of code is missing and don't have time to look in detail. But from a quick glance it looks like you are starting the animation on the event dispatch thread and then locking that thread into a permanent loop. Given that all GUI stuff is done on event dispatch thread nothing will happen until you release the thread.
    I suggest you look at using a SwingWorker.

    Code:
            } else if (ae.getActionCommand().equals("Stop")) {
            	System.exit(0);
          	  	disconnect();
            }
    Two things:
    1. Using System.exit(0) like this is a lazy way to stop an application.
    2. What's the point in having a call to disconnect() after System.exit(0), it will never get executed.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2011
    Posts
    2

    Re: GUI question.

    Quote Originally Posted by keang View Post
    Two things:
    1. Using System.exit(0) like this is a lazy way to stop an application.
    2. What's the point in having a call to disconnect() after System.exit(0), it will never get executed.
    Thank you for reply! It helps me.

    Yes, You are right about

    Quote Originally Posted by keang View Post
    Given that all GUI stuff is done on event dispatch thread nothing will happen until you release the thread.
    Could you provide more information, how to make it?
    I could not find same examples.
    I'm not good in Java. Thats why I use examples to change. If you have same examples that can help, could you provide it?

    I really appreciate any help!!

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: GUI question.

    I've already suggested you use SwingWorker. There are loads of examples on-line and failing that the API docs for the class give a good example of it's use.

    Just google "java SwingWorker"
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

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

Featured