`
ice_key
  • 浏览: 17723 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

用swing写点在矩形内做弓形运动

 
阅读更多

点运动的类MotionTrace.java

 

package org.lyw.sh.test;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Ellipse2D;
import java.text.DecimalFormat;

public class MotionTrace {

	private static int count = 0;

	private static boolean runFlag = true;

	private double startX;
	private double startY;

	private double minX;
	private double minY;

	private double maxX;
	private double maxY;

	private double increase = 0.0;

	private double distance;

	private double rate;

	private double directionX = 1.0;

	private double directionY = 1.0;

	public MotionTrace() {
		startX = 0;
		startY = 0;

		minX = 0;
		minY = 0;

		maxX = 500.0;
		maxY = 500.0;

		distance = 30;

		rate = 0.000009;
	}

	public void directionY(Graphics2D g2d) {
		System.err.println("--------------Y:" + directionY + "--------------");
		// --------------(+0.000000,0.000000+)--------------
		// --------------(+0.009000,0.009000+)--------------
		// System.out.println("--------------(+" + doubleFormat(minX * rate) +
		// ","
		// + doubleFormat(minY * rate) + "+)--------------");
		// System.out.println("--------------(+" + doubleFormat(maxX * rate) +
		// ","
		// + doubleFormat(maxY * rate) + "+)--------------");

		if (startX > maxX) {
			directionX = -directionX;
			startX = startX + distance * directionX;
			count = 1;
			runFlag = false;
			return;
			// directionX();
		} else if (startX < minX) {
			count = 1;
			directionX = -directionX;
			startX = startX + distance * directionX;
			// directionX();
			runFlag = false;
			return;
		}

		if (0 == count) {

			increase = startY;

		} else {

			increase = startY + distance * count * directionY;
		}

		if (!isBeyond(increase, minY, maxY)) {
			Paint paint = new GradientPaint(0, 0, Color.RED, 222, 222,
					Color.green, true);
			g2d.setPaint(paint);
			Ellipse2D circle = new Ellipse2D.Double(startX, increase, 4, 4);
			g2d.draw(circle);

			System.err.println("(" + doubleFormat(startX * rate) + ","
					+ doubleFormat(increase * rate) + ")");
		} else {
			count = -1;
			directionY = -directionY;
			startY = increase + distance * directionY;
			startX = startX + distance * directionX;
			//
			// if (startX > maxX) {
			// directionX = -directionX;
			// startX = startX + distance * directionX;
			// count = 0;
			// directionX();
			// } else if (startX < minX) {
			// count = 0;
			// directionX = -directionX;
			// startX = startX + distance * directionX;
			// directionX();
			// }

		}
		// } else if (increase > maxY) {
		// System.err.println("TOP");
		// count = 1;
		// directionY = -directionY;
		// startY = increase + distance * directionY;
		// startX = startX + distance * directionX;
		// } else if (increase < minY) {
		// System.err.println("BUTTON");
		// count = 1;
		// directionY = -directionY;
		// startY = increase + distance * directionY;
		// startX = startX + distance * directionX;
		// }
		count++;
	}

	public void directionX(Graphics2D g2d) {
		System.err.println("--------------X:" + directionX + "--------------");
		// --------------(+0.000000,0.000000+)--------------
		// --------------(+0.009000,0.009000+)--------------
		// System.out.println("--------------(+" + doubleFormat(minX * rate) +
		// ","
		// + doubleFormat(minY * rate) + "+)--------------");
		// System.out.println("--------------(+" + doubleFormat(maxX * rate) +
		// ","
		// + doubleFormat(maxY * rate) + "+)--------------");

		if (startY > maxY) {
			directionY = -directionY;
			startY = startY + distance * directionY;
			count = 1;
			runFlag = true;
			return;
			// directionY();
		} else if (startY < minY) {
			count = 1;
			directionY = -directionY;
			startY = startY + distance * directionY;
			runFlag = true;
			return;
			// directionY();
		}

		if (0 == count) {

			increase = startX;

		} else {

			increase = startX + distance * count * directionX;
		}

		if (!isBeyond(increase, minX, maxX)) {

			System.err.println("(" + doubleFormat(increase * rate) + ","
					+ doubleFormat(startY * rate) + ")");
		} else {
			// System.err.println("BEYOND");
			count = -1;
			directionX = -directionX;
			startX = increase + distance * directionX;
			startY = startY + distance * directionY;

			// if (startY > maxY) {
			// directionY = -directionY;
			// startY = startY + distance * directionY;
			// count = 0;
			// directionY();
			// } else if (startY < minY) {
			// count = 0;
			// directionY = -directionY;
			// startY = startY + distance * directionY;
			// directionY();
			// }
		}
		// } else if (increase > maxX) {
		// System.err.println("TOP");
		// count = 1;
		// directionX = -directionX;
		// startX = increase + distance * directionX;
		// startY = startY + distance * directionY;
		// } else if (increase < minX) {
		// System.err.println("BUTTON");
		// count = 1;
		// directionX = -directionX;
		// startX = increase + distance * directionX;
		// startY = startY + distance * directionY;
		// }
		count++;
	}

	private String doubleFormat(double dbl) {

		DecimalFormat dft = new DecimalFormat("#####0.000000");

		return dft.format(dbl);
	}

	private boolean isBeyond(double ori, double min, double max) {

		if (ori < min || ori > max) {
			return true;
		}
		return false;

	}

	public double getDirectionX() {
		return directionX;
	}

	public void setDirectionX(double directionX) {
		this.directionX = directionX;
	}

	public double getDirectionY() {
		return directionY;
	}

	public void setDirectionY(double directionY) {
		this.directionY = directionY;
	}

	public static boolean isRunFlag() {
		return runFlag;
	}

	public static void setRunFlag(boolean runFlag) {
		MotionTrace.runFlag = runFlag;
	}

}

 swing画图,显示点的运动MotionRun.java

 

package org.lyw.sh.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

public class MotionRun {

	public static void main(String[] args) {
		ClockFrame cf = new ClockFrame();
		cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		cf.setVisible(true);
	}

}

class ClockFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	private ClockPanel panel;
	private Calendar calendar;

	public ClockFrame() {
		setTitle("Clock");
		// setResizable(false);

		this.setBounds(0, 0, 5000, 5000);
		// Toolkit kit = Toolkit.getDefaultToolkit();
		// Dimension screen = kit.getScreenSize();
		// int screenX = screen.width;
		// int screenY = screen.height;
		// setLocation(screenX / 3, screenY / 3);
		final MotionTrace motion = new MotionTrace();
		ActionListener listener = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				calendar = getClock();

				String h = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
				String m = String.valueOf(calendar.get(Calendar.MINUTE));
				String s = String.valueOf(calendar.get(Calendar.SECOND));

				TextPanel tp = new TextPanel(h, m, s);
				add(tp, BorderLayout.NORTH);

				panel = new ClockPanel(h, m, s, motion);
				add(panel, BorderLayout.CENTER);

				pack();
			}
		};

		Timer timer = new Timer(1000, listener);
		timer.start();
	}

	public Calendar getClock() {
		Calendar calendar = Calendar.getInstance();
		return calendar;
	}
}

class TextPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private JTextField hourField;
	private JTextField minuteField;
	private JTextField secondField;

	public TextPanel(String h, String m, String s) {
		setBackground(Color.WHITE);

		add(new JLabel("Hour:"));
		hourField = new JTextField(h, 3);
		hourField.setEditable(false);
		add(hourField);

		add(new JLabel("Minute:"));
		minuteField = new JTextField(m, 3);
		minuteField.setEditable(false);
		add(minuteField);

		add(new JLabel("Second:"));
		secondField = new JTextField(s, 3);
		secondField.setEditable(false);
		add(secondField);
	}
}

class ClockPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private double seconds = 0;
	private double minutes = 0;
	private double hours = 0;

	private int RADIUS = 100;

	private double SECOND_HAND_LENGTH = 0.8 * RADIUS;
	private double MINUTE_HAND_LENGTH = 0.8 * RADIUS;
	private double HOUR_HAND_LENGTH = 0.7 * RADIUS;

	private int centerX = 0;
	private int centerY = 0;
	MotionTrace motion;

	public ClockPanel(String h, String m, String s, MotionTrace motion) {
		setBackground(Color.BLACK);

		hours = Double.parseDouble(h);
		minutes = Double.parseDouble(m);
		seconds = Double.parseDouble(s);
		this.motion = motion;

		// setPreferredSize(new Dimension(2 * RADIUS + 100, 2 * RADIUS + 100));
		setPreferredSize(new Dimension(5000, 5000));
	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		Graphics2D gs = (Graphics2D) g;

		// --------------------------

		if (MotionTrace.isRunFlag()) {
			motion.directionY(gs);
		} else {
			motion.directionX(gs);
		}
		// --------------------------

		centerX = this.getWidth() / 2;
		centerY = this.getHeight() / 2;

		gs.setColor(Color.GREEN);
		gs.drawString("MAN 表", centerX - 20, centerY - RADIUS / 2);
		gs.drawString("MADE IN CHINA", centerX - 40, centerY + RADIUS / 2);

		gs.setColor(Color.WHITE);
		for (double nlLoop = 0; nlLoop <= 2; nlLoop = nlLoop + 0.1) {
			Ellipse2D circle = new Ellipse2D.Double(centerX - RADIUS + 15
					+ nlLoop, centerY - RADIUS + 15 + nlLoop,
					2 * (RADIUS - 15), 2 * (RADIUS - 15));
			gs.draw(circle);
		}
		for (double nlLoop = 0; nlLoop <= 2; nlLoop = nlLoop + 0.1) {
			Ellipse2D circle = new Ellipse2D.Double(centerX - RADIUS - 9
					+ nlLoop, centerY - RADIUS - 9 + nlLoop, 2 * (RADIUS + 9),
					2 * (RADIUS + 9));
			gs.draw(circle);
		}

		String[] timer = { "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ",
				"Ⅺ", "Ⅻ" };
		gs.setColor(Color.RED);
		int gX = centerX - 3;
		int gY = centerY - RADIUS + 5;
		for (int i = 0; i < 12; i++) {
			int x = (int) (gX + RADIUS * Math.sin(2 * Math.PI * (i + 1) / 12));
			int y = (int) (gY + RADIUS
					* (1.0 - Math.cos(2 * Math.PI * (i + 1) / 12)));

			gs.drawString(timer[i], x, y);
		}

		double secondAngle = Math.toRadians(360 * seconds / 60);
		drawHand(gs, secondAngle, SECOND_HAND_LENGTH, "s");

		double minuteAngle = Math
				.toRadians(360 * (minutes + seconds / 60) / 60);
		drawHand(gs, minuteAngle, MINUTE_HAND_LENGTH, "m");

		double hourAngle = Math.toRadians(360 * (hours - 12) / 12 + 360
				* minutes / (60 * 12));
		drawHand(gs, hourAngle, HOUR_HAND_LENGTH, "h");
	}

	private void drawHand(Graphics2D gc, double angle, double hand, String l) {
		Point2D center = new Point2D.Double(centerX, centerY);
		Point2D end = new Point2D.Double(centerX + hand * Math.sin(angle),
				centerY - hand * Math.cos(angle));
		if (l.equals("h")) {
			gc.setColor(Color.RED);
		} else if (l.equals("m")) {
			gc.setColor(Color.green);
		} else if (l.equals("s")) {
			gc.setColor(Color.BLUE);
		}
		gc.draw(new Line2D.Double(center, end));
	}

}
分享到:
评论

相关推荐

    swing圆角矩形按钮

    根据给定的信息,本文将详细解释如何在Java Swing框架中创建具有圆角效果的自定义按钮。这将涉及Swing的基本概念、自定义组件的方法以及如何实现特定的视觉效果。 ### Swing简介 Swing是Java平台的一个图形用户...

    用Java swing写的魔板小游戏.zip

    用Java swing写的魔板小游戏.zip用Java swing写的魔板小游戏.zip 用Java swing写的魔板小游戏.zip用Java swing写的魔板小游戏.zip 用Java swing写的魔板小游戏.zip用Java swing写的魔板小游戏.zip 用Java swing写的...

    java的课程作业,用swing写一个小游戏.zip

    java的课程作业,用swing写一个小游戏.zipjava的课程作业,用swing写一个小游戏.zip java的课程作业,用swing写一个小游戏.zipjava的课程作业,用swing写一个小游戏.zip java的课程作业,用swing写一个小游戏.zip...

    java 自己写的小球在一个矩形框内反弹程序

    标题中的“java 自己写的小球在一个矩形框内反弹程序”揭示了这是一个基于Java编程语言的项目,目的是实现一个动态的小游戏。在这个游戏中,一个小球会在一个矩形区域内运动,并且当它碰撞到边界时,会按照物理定律...

    Swing 移动矩形

    "Swing 移动矩形"这个主题主要涉及如何在Swing组件上动态地绘制并移动一个矩形。这篇博客文章可能探讨了如何利用Swing的绘图功能来实现一个交互式的图形应用,让用户能够通过鼠标或键盘操作移动图形。 1. **Swing...

    24点游戏swing _24点_

    标题中的“24点游戏Swing”指的是使用Java Swing库开发的一款基于24点规则的桌面游戏。Swing是Java的一个图形用户界面(GUI)工具包,它提供了丰富的组件和功能来创建桌面应用,包括游戏。在这个游戏中,玩家需要通过...

    javaSwing写的日历控件

    在Java Swing中,我们可以创建各种用户界面元素,包括一个交互式的日历控件。这个"javaSwing写的日历控件"是专为展示日期、进行日期选择以及可能的日期操作而设计的一个组件,非常适合用于日程管理或者日期相关的...

    Java写的Swing界面系统

    在本Java写的Swing界面系统中,你将找到完整的源代码,可以直接运行并学习Swing的基本用法和高级特性。 1. **Swing组件基础**:Swing 提供了各种组件,如 JButton、JLabel、JTextField、JTextArea、JComboBox、...

    用Swing写的一个RPG游戏

    《用Swing写的一个RPG游戏》是一款基于Java Swing库开发的角色扮演游戏(Role-Playing Game,简称RPG)。Swing是Java提供的一种用于构建桌面应用的GUI(图形用户界面)工具包,它允许开发者创建丰富的交互式用户界面...

    用Swing写的QQ聊天室

    “用Swing写的QQ聊天室”项目展示了Swing在创建交互式桌面应用中的强大功能,涉及了登录注册、好友管理、聊天功能等多个模块。通过学习和实践这个项目,开发者可以深入了解Swing组件的使用,以及如何构建一个完整的...

    Swing写的局域网Socket 聊天系统

    标题中的“Swing写的局域网Socket 聊天系统”揭示了这是一个使用Java Swing库构建的、基于Socket通信协议的局域网聊天应用程序。Swing是Java提供的一种图形用户界面(GUI)工具包,它允许开发者创建丰富的桌面应用。...

    swing写的三国杀源代码

    例如,使用JTable展示手牌,用JLabel显示角色头像和血量,用JTextArea记录游戏过程。 三、调试与优化 1. Bug查找:描述中提到游戏已能运行,但可能存在bug,因此源代码的调试是必不可少的。开发者可以通过IDE的...

    Swing写的一个简易记事小软件

    标题中的“Swing写的一个简易记事小软件”指的是使用Java Swing库开发的一款简单应用程序,主要用于记录日常事项。Swing是Java提供的一种图形用户界面(GUI)工具包,它允许开发者创建桌面应用,包括窗口、按钮、...

    实时联机对战小游戏 一个用java swing写的实时联机对战小游戏.zip

    实时联机对战小游戏 一个用java swing写的实时联机对战小游戏.zip 实时联机对战小游戏 一个用java swing写的实时联机对战小游戏.zip 实时联机对战小游戏 一个用java swing写的实时联机对战小游戏.zip 实时联机对战小...

    java swing写的计算器

    java swing做的计算器,不能连续运算xxxxxxxxxxxxxxxxxxxx

    Java Swing组件写的游戏客户端.zip

    Java Swing组件写的游戏客户端Java Swing组件写的游戏客户端 Java Swing组件写的游戏客户端Java Swing组件写的游戏客户端 Java Swing组件写的游戏客户端Java Swing组件写的游戏客户端 Java Swing组件写的游戏客户端...

    用Java Swing写的记事本

    用java swing 写的基于图形界面的记事本程序,全部源代码

    swing写的迅雷界面

    swing写的迅雷界面 .

Global site tag (gtag.js) - Google Analytics