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<Integer> list = new ArrayList<Integer>();
				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();
	}


}

 

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值