Project

General

Profile

Revision 511

Added by Greg Tress about 16 years ago

added image buffering

View differences:

Colonet.java
861 861
		int BORDER = 16;  // this is arbitrary. it makes the image look nice inside a border.
862 862
		int BOT_RADIUS = 40;
863 863
		volatile BufferedImage img;
864
		BufferedImage buffer;
864 865
	
865 866
		public WebcamPanel () {
866 867
			super();
868
			buffer = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
867 869
		}
868 870
		
869 871
		public synchronized void setImage (BufferedImage newimg) {
......
874 876
			img = newimg;
875 877
			repaint();
876 878
		}
877
		
878 879
				
879 880
		public synchronized void paint (Graphics g) {
880
			
881 881
			if (img == null)
882 882
				return;
883
			// Place the image on the screen, inside the panel
884 883
			
884
			// Calculate scaling
885 885
			int maxWidth = getWidth() - 2*BORDER;
886 886
			int maxHeight = getHeight() - 2*BORDER;
887 887
			double widthRatio = 1.0 * maxWidth / img.getWidth();
......
906 906
			    y = (maxHeight - newHeight) / 2 + BORDER;
907 907
			}
908 908
			
909
			// Draw everything onto the buffer
910
			Graphics2D bufferedGraphics = (Graphics2D)buffer.getGraphics();
911
			bufferedGraphics.setColor(Color.GRAY);
912
			bufferedGraphics.fillRect(0, 0, this.getWidth(), this.getHeight());
909 913
			Image imgScaled = img.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST);
910
			g.drawImage(imgScaled, x, y, this);
914
			bufferedGraphics.drawImage(imgScaled, x, y, this);
911 915
			
912 916
						
913 917
			// Draw Identifiers and battery levels
914 918
			if (robotIcons == null)
915 919
				return;
916 920
				
917
			((Graphics2D)g).setStroke(new BasicStroke(2));
921
			bufferedGraphics.setStroke(new BasicStroke(2));
918 922
			for (int i = 0; i < robotIcons.size(); i++) {
919 923
				RobotIcon r = robotIcons.get(i);
920
				g.setColor(r.color);
924
				bufferedGraphics.setColor(r.color);
921 925
				// Identifier circle
922 926
				int px = (int) (x + r.x * scale);
923 927
				int py = (int) (y + r.y * scale);
924
				g.drawOval(px-RADIUS, py-RADIUS, 2*r.RADIUS, 2*r.RADIUS);
928
				bufferedGraphics.drawOval(px-RADIUS, py-RADIUS, 2*r.RADIUS, 2*r.RADIUS);
925 929
				// Battery
926 930
				//if (r.battery >= 0) {
927
				    g.setColor(Color.GREEN);
928
				    g.fillRect(px+20, py+20, 30, 10);
929
				    g.setColor(Color.BLACK);
930
				    g.drawRect(px+20, py+20, 50, 10);
931
				    bufferedGraphics.setColor(Color.GREEN);
932
				    bufferedGraphics.fillRect(px+20, py+20, 30, 10);
933
				    bufferedGraphics.setColor(Color.BLACK);
934
				    bufferedGraphics.drawRect(px+20, py+20, 50, 10);
931 935
				//}
932 936
				// If the robot has a destination, draw the vector
933 937
				if (r.destx >= 0) {
934
				    g.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale));
938
				    bufferedGraphics.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale));
935 939
				}
936 940
			}
937 941
			
938 942
			// Identify currently-selected robot
939 943
			if (selectedBot < 0)
940 944
			    return;
941
			g.setColor(Color.BLACK);
945
			bufferedGraphics.setColor(Color.BLACK);
942 946
			RobotIcon r = robotIcons.get(selectedBot);
943 947
			int px = (int) (x + r.x * scale);
944 948
			int py = (int) (y + r.y * scale);
945
			g.drawOval(px-RADIUS-6, py-RADIUS-6, 2*r.RADIUS+12, 2*r.RADIUS+12);
949
			bufferedGraphics.drawOval(px-RADIUS-6, py-RADIUS-6, 2*r.RADIUS+12, 2*r.RADIUS+12);
950
			
951
			//Display buffered content
952
			g.drawImage(buffer, 0, 0, null);
946 953
		}
947 954
	
948 955
	}

Also available in: Unified diff