java 线程和Applet

一起来玩玩呗 2012-11-15 06:20:09
编写Applet程序,当鼠标单击小程序窗口区域的某一点,由该点的下方打上一光束,以单击点为中心向四周呈同心圆散开,其效果类似放烟花,尽可能使制作的烟花更逼真。

求教啊
...全文
139 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一起来玩玩呗 2012-11-16
  • 打赏
  • 举报
回复
引用 6 楼 wapigzhu 的回复:
这个不会是作业吧,我会被骂的 Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485……
非常感谢!这个不是作业
ace62 2012-11-16
  • 打赏
  • 举报
回复
给你一个思路: 单击后,取得该点坐标,确定圆心位置,启动线程或定时器; 在相应的线程或定时器处理程序中,实现具体绘图功能。 定时改变圆的半径,画圆(应该只是上半部分的扇形),并填充
wapigzhu 2012-11-16
  • 打赏
  • 举报
回复
这个不会是作业吧,我会被骂的

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JApplet;

public class TApplet extends JApplet {

	abstract class Action {
		protected int clickX;
		protected int clickY;
		private boolean isFinish = false;
		private Action nextAction;

		public Action(int x, int y) {
			clickX = x;
			clickY = y;
		}

		public Action getNextAction() {
			return nextAction;
		}

		public boolean isFinish() {
			return isFinish;
		}

		public abstract void render(Graphics2D g);

		protected void setFinish(boolean flag) {
			isFinish = flag;
		}

		public void setNextAction(Action action) {
			nextAction = action;
		}

		public abstract void update(long timeMillions);
	}

	class BeginAction extends Action {
		private int beginX;
		private int beginY;
		private int currentX;
		private int currentY;

		public BeginAction(int x, int y) {
			super(x, y);
			beginX = currentX = x;
			beginY = currentY = y + 300;
		}

		@Override
		public void render(Graphics2D g) {
			g.setColor(Color.red);
			g.drawLine(beginX, beginY, currentX, currentY);
		}

		@Override
		public void update(long timeMillions) {
			if (!isFinish()) {
				currentY -= 2;
				if (currentY < clickY)
					setFinish(true);
			}
		}
	}
	
	class DiffuseAction extends Action{
		private int radius = 0;
		
		public DiffuseAction(int x, int y) {
			super(x, y);
		}
		@Override
		public void render(Graphics2D g) {
			g.setColor(Color.red);
			g.drawRoundRect(clickX - radius / 2, clickY - radius / 2, radius, radius, radius, radius);
		}

		@Override
		public void update(long timeMillions) {
			radius += 2;
			if(radius > 400)
				setFinish(true);
		}
		
	}

	private static final int HEIGHT = 600;
	private static final int WIDTH = 600;

	private Action currentAction;
	private Thread currentThread;
	private BufferedImage image;

	@Override
	public void init() {
		super.init();
		setSize(WIDTH, HEIGHT);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				final int x = e.getX();
				final int y = e.getY();
				if(currentThread != null)
					currentThread.interrupt();
				currentThread = new Thread() {
					public void run() {
						currentAction = new BeginAction(x, y);
						currentAction.setNextAction(new DiffuseAction(x, y));
						try {
							while (true) {
								Thread.sleep(30);
								if(currentAction == null)
									break;
								if(!currentAction.isFinish())
									currentAction
										.update(System.currentTimeMillis());
								else
									currentAction = currentAction.getNextAction();
								repaint();
							}
						} catch (InterruptedException e) {
						}
					}
				};
				currentThread.start();
			}
		});
	}


	public void paint(Graphics g){
		super.paint(g);
		if (currentAction != null)
			currentAction.render((Graphics2D) g);
	}
	
	@Override
	public void update(Graphics g) {
		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = image.createGraphics();
		paint(g2);
		g.drawImage(image, 0, 0, null);
	}
}

Zerone2013 2012-11-16
  • 打赏
  • 举报
回复
引用 3 楼 ace62 的回复:
给你一个思路: 单击后,取得该点坐标,确定圆心位置,启动线程或定时器; 在相应的线程或定时器处理程序中,实现具体绘图功能。 定时改变圆的半径,画圆(应该只是上半部分的扇形),并填充
按上述操作之后定义一个数组,数组为圆的半径,比如int [] a ={1,2,3,4,5,4,2,1,0} 然后根据数组,用线程启动,然后划圆,应该能达到那种效果
Zerone2013 2012-11-16
  • 打赏
  • 举报
回复
引用 2 楼 q745401990 的回复:
。。。。。
按上述操作之后定义一个数组,数组为圆的半径,比如int [] a ={1,2,3,4,5,4,2,1,0} 然后根据数组,用线程启动,然后划圆,应该能达到那种效果
一起来玩玩呗 2012-11-15
  • 打赏
  • 举报
回复
。。。。。
一起来玩玩呗 2012-11-15
  • 打赏
  • 举报
回复
在线等 求解啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧