SWT中使用Opengl画圆的例子

import org.eclipse.opengl.GL;
import org.eclipse.opengl.GLU;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.opengl.GLCanvas;

public class MouseHandler implements MouseListener, MouseMoveListener {
	
	private boolean isMouseDown; //鼠标是否按下
	private Point beginPoint;
	private Point endPoint;
	
	private double degtorads = 3.14159265 / 180.0;
	
	private Window window;
	
	public MouseHandler(Window window) {
		this.window = window;
	}
	
	public void mouseDoubleClick(MouseEvent e) {
		//Nothing to do
	}
	
	public void mouseDown(MouseEvent e) {
		if(e.button == 1) {
			isMouseDown = true;
			beginPoint = screenToMode( new Point(e.x, e.y) );
		}
	}
	
	public void mouseUp(MouseEvent e) {
		isMouseDown = false;
	}
	
	public void mouseMove(MouseEvent e) {
		
		if(isMouseDown) {
			
			GLCanvas canvas = window.canvas;
			endPoint = screenToMode( new Point(e.x, e.y) );
			
			GL.glClear(GL.GL_COLOR_BUFFER_BIT);
			GL.glBegin(GL.GL_LINE_STRIP);
					
			double r = Math.sqrt((endPoint.x - beginPoint.x) * (endPoint.x - beginPoint.x) +
					(endPoint.y - beginPoint.y) * (endPoint.y - beginPoint.y));
			
			for(float theta = 0.0f; theta <= 360; theta += 1.0) {
				GL.glVertex2d(beginPoint.x + r * Math.cos(theta * degtorads), 
						beginPoint.y + r * Math.sin(theta * degtorads));
			}
	
			GL.glEnd();
		
			canvas.swapBuffers();
		}
	}
	
	public Point screenToMode(Point point) {
		Rectangle rect = window.canvas.getClientArea();
		return new Point(point.x - rect.width / 2, (rect.height - point.y) - rect.height / 2);
	}
}
 
import org.eclipse.opengl.GL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Window {
	
	private static Window instance = null;
	
	public Display display;
	public Shell shell;
	public GLCanvas canvas;

	private Window() {
		display = new Display();
		shell = new Shell(display);
		
		createContents(shell);
	}
	
	public void createContents(Composite composite) {
		composite.setLayout(new FillLayout());
		GLData data = new GLData();
		data.doubleBuffer = true;
		canvas = new GLCanvas(composite, SWT.NONE, data);
		canvas.setCurrent();
		
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				update();
			}
		}); 
		
		canvas.addControlListener(new ControlAdapter() {
			public void controlResized(ControlEvent e) {
				update();
			}
		});
		
		MouseHandler mh = new MouseHandler(this);
		canvas.addMouseListener(mh);
		canvas.addMouseMoveListener(mh);
		
	}
	
	public void mainLoop() {
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	public void dispose() {
		display.dispose();
	}
	
	public void show() {
		shell.open();
		
		mainLoop();
		dispose();
	}
	
	public void update() {
		Rectangle rect = canvas.getClientArea();
		
		GL.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		GL.glColor3f(0.0f, 0.0f, 1.0f);
		GL.glMatrixMode(GL.GL_PROJECTION);
		GL.glLoadIdentity();
		
		GL.glOrtho(-rect.width / 2, rect.width / 2, -rect.height / 2, rect.height / 2, -1.0, 1.0);
		GL.glViewport(0, 0, rect.width, rect.height);
		
		GL.glClear(GL.GL_COLOR_BUFFER_BIT);
		
		canvas.swapBuffers();
	}
	
	public static Window getInstance() {
		if(instance == null) {
			instance = new Window();
		}
		
		return instance;
	}
}
 
public class Test {

	public static void main(String[] args) {
		Window window = Window.getInstance();
		window.show();
	}
}

 上面的程序是用鼠标画圆的例子,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SWT可以使用Java发送验证码,具体步骤如下: 1. 导入JavaMail和Java Activation Framework(JAF)的jar包到SWT项目。 2. 创建一个JavaMail的Properties对象,设置SMTP服务器的相关信息,例如端口号、认证方式、用户名、密码等。 3. 创建一个Session对象,将Properties对象作为参数传入。 4. 创建一个MimeMessage对象,设置发送者、接收者、主题、内容等信息。 5. 使用Transport类的sendMessage()方法发送邮件,将MimeMessage对象作为参数传入。 以下是具体的代码实现: ```java import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendMail { public static void main(String[] args) { String to = "recipient@example.com"; // 收件人地址 String from = "sender@example.com"; // 发件人地址 String host = "smtp.example.com"; // SMTP服务器地址 String user = "username"; // 用户名 String password = "password"; // 密码 // 创建Properties对象,设置SMTP服务器的相关信息 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable", "true"); // 创建Session对象 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); try { // 创建MimeMessage对象 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("验证码"); // 设置主题 message.setText("您的验证码为:123456"); // 设置内容 // 发送邮件 Transport.send(message); System.out.println("邮件已发送。"); } catch (MessagingException e) { System.out.println("发送邮件失败。"); e.printStackTrace(); } } } ``` 注意:需要替换上述代码的收件人地址、发件人地址、SMTP服务器地址、用户名和密码等信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值