SWT display wave显示波形

package plug_in_test;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.GC;

public class TstDrawDisplay2 {
	
	/*
	 * wave #1
	 * **********************************************
	 *   /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /
	 *  /  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/
	 * **********************************************
	 */
	private static int[] wave1_data = new int[]{-100, 0, 100, 0, };
	/*
	 * wave #2
	 * **********************************************
	 *    _     _     _     _     _     _     _     _
	 *   / \   / \   / \   / \   / \   / \   / \   /
	 *  /   \_/   \_/   \_/   \_/   \_/   \_/   \_/  
	 * **********************************************
	 */
	private static int[] wave2_data = new int[]{-100, 0, 100, 100, 0, -100 };
	/*
	 * wave #3
	 * **********************************************
	 *     _       _       _       _       _       _  
	 *   _/ \_   _/ \_   _/ \_   _/ \_   _/ \_   _/ \_
	 *  /     \_/     \_/     \_/     \_/     \_/     \
	 * **********************************************
	 */
	private static int[] wave3_data = new int[]{-100, 0, 0, 100, 100, 0, 0, -100, -100, 0, 0, 100, 100, 0, 0, -100,};
	/*
	 * wave #4
	 * **********************************************
	 *     /\          /\          /\          /\
	 *    /  \    /\  /  \    /\  /  \    /\  /  \
	 *   /    \  /  \/    \  /  \/    \  /  \/    \  /  
	 *  /      \/          \/          \/          \/  
	 * **********************************************
	 */
	private static int[] wave4_data = new int[]{-200, -100, 0, 100, 200, 100, 0, -100, -200, -100, 0, 100, 0, -100, 0, 100, 200, 100, 0, -100 };

	private static int timerCounter = 0;

	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);

		shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
		
		shell.addPaintListener(new PaintListener() {

			public void paintControl(PaintEvent e) {

				GC gc = e.gc;

				int x = e.x, y = e.y, width = e.width, height = e.height;

				/*
				 * draw x axis.
				 */
				gc.setLineWidth(3);
				gc.setForeground(display.getSystemColor(SWT.COLOR_YELLOW));
				gc.drawLine(x, y + height/2, x + width, y + height/2);

				/*
				 * draw x coordinate.
				 */
				gc.setLineWidth(1);
				gc.setLineStyle(SWT.LINE_DOT);
				gc.setForeground(display.getSystemColor(SWT.COLOR_DARK_YELLOW));
				for (int i = 0 ; (y + height/2 - 40*i) > y ; i++) {
					gc.drawLine(x, y + height/2 - 40*i, x + width, y + height/2 - 40*i);
					gc.drawLine(x, y + height/2 + 40*i, x + width, y + height/2 + 40*i);

				}

				/*
				 * draw y coordinate.
				 */

				int xSum=0;
				for (int i = 0; i*60 < width; i++) {
					gc.drawLine(x+60*i, y, x+60*i, y + height);
					xSum = i;
				}

				/*
				 * draw y axis
				 */
				gc.setLineWidth(3);
				gc.setForeground(display.getSystemColor(SWT.COLOR_YELLOW));
				gc.drawLine(x+60*(xSum/2+1), y, x+60*(xSum/2+1), y + height);


				/*
				 * draw wave
				 */
				gc.setLineWidth(3);
				gc.setLineStyle(SWT.LINE_SOLID);
				gc.setForeground(display.getSystemColor(SWT.COLOR_GREEN));

				gc.drawPolyline(getWave(xSum, x, y, height, wave1_data));
//				gc.drawPolyline(getWave(xSum, x, y, height, wave2_data));
//				gc.drawPolyline(getWave(xSum, x, y, height, wave3_data));
				gc.drawPolyline(getWave(xSum, x, y, height, wave4_data));
				
			
			}

			private int[] getWave(int xSum, int x, int y, int height, int[] wave_data) {
				List list = new ArrayList();
				for(int i = 0; i <= xSum + 1; i++) {
					list.add(0);
					list.add(0);
				}

				for(int i = 0; i <= xSum + 1; i++) {
					list.set(2*i, x+60*i);
				}
				
				int wave_start_index = timerCounter % (wave_data.length);
				for(int i = 0; i <= xSum + 1; i++) {
					list.set(2*i+1, y + height/2 + wave_data[(wave_start_index+i) % wave_data.length]);
				}

				int[] points = new int[list.size()];
				for(int i = 0 ; i < points.length; i++)
					points[i] = list.get(i);
				return points;
			}
			
		});

		Timer timer = new Timer(true);
		timer.schedule(new TimerTask() {
			public void run() {
				timerCounter ++;
				display.syncExec(new Runnable() {
					public void run() {
						if ( null != shell && !shell.isDisposed() )
							shell.redraw();
					}
				});
			}

		}, 200, 300);

		shell.setSize(600, 500);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}


}

 

 


SWT display wave显示波形_第1张图片
 

你可能感兴趣的:(Java,SWT,Image,图像)