Results 1 to 4 of 4

Thread: GUI question.

  1. #1
    ryainad is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default 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:


    Java 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
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    I think one post is enough
    GUI question.
    GUI question.

    I recommend in the future posting an SSCCE, especially if you have classes that are not defined in the standard java libraries (what is Animator?). It looks like you are piling the reading onto the EDT - as the EDT is responsible for drawing it won't draw until your finished reading. Use a SwingTimer, SwingWorker, or multi-thread the application.

  3. #3
    ryainad is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you for reply.

    I'm new in Java, I didn't know where to post this question... :confused: Sorry for that...

    I used tutorial, I don't remember the source. Could you show some example code, which is related to this issue with buttons. I'm not good in JAva, but I can change some code...
    Last edited by ryainad; 04-02-2011 at 02:34 AM.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Like I said, this most likely isn't directly related to buttons. Swing is single threaded - painting occurs on a single thread - pile work onto that thread and the UI cannot be repainted. Based upon the code you posted this is my guess...an SSCCE would help you break the problem down and help others like myself reproduce the problem to more thoroughly diagnose. And suggested reading: Threads and Swing

Similar Threads

  1. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  2. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  3. DNS look up question
    By DWASH5000 in forum Networking
    Replies: 2
    Last Post: 10-27-2010, 02:22 AM
  4. question
    By berkeley in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 08:56 PM
  5. JDO Question...
    By Kam in forum JDBC
    Replies: 0
    Last Post: 01-07-2010, 06:11 PM

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
  •