求一个JAVA小程序的源代码,要求如下

写一个GUI程序,允许用户选择画圆形或者三角形,并画出图形。
当用户选择了圆形时,用户可以用鼠标点两次,第一次点击的位置设为圆心,第二次点击的位置设为圆上一点,然后程序画出圆形。
若用户选择了画三角形,则允许用户点击三次鼠标,以这三个点画出三角形。
GUI上面添加两个按钮来允许用户选择将要绘制的图形。

没有源代码的话,希望可以提供通过鼠标两次点击绘制圆形和鼠标三次点击绘制三角形的设计思路。
注意,绘制一个图形,是先点击按钮选择之后,然后鼠标点击两下或三下之后程序绘制出图形,不是鼠标拖动或轨迹形成图形的。

大概是这个样子。
------------------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DateCheck extends JFrame implements ActionListener {
private boolean isOval = true;
public DateCheck() {
setSize(567, 419);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Oval");
btnNewButton.addActionListener(this);
btnNewButton.setBounds(80, 10, 93, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Rect");
btnNewButton_1.setBounds(203, 10, 93, 23);
btnNewButton_1.addActionListener(this);
getContentPane().add(btnNewButton_1);
JPanel panel = new MyPanel();
panel.setBounds(12, 47, 537, 337);
getContentPane().add(panel);
new Timer().schedule(new MyTimesk(), new Date(), 100);
setVisible(true);
}
public static void main(String[] args) {
new DateCheck();
}
public void actionPerformed(ActionEvent event) {
isOval = "Oval".equals(event.getActionCommand());
System.out.println(isOval);
}
class MyPanel extends JPanel implements MouseListener {
Point point1 = null;
Point point2 = null;
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// }
int click = 0;
public MyPanel() {
addMouseListener(this);
}
public void paint(Graphics g) {
super.paint(g);
Point p = this.getMousePosition();
if (p == null && click % 2 == 1) {
return;
}
if (click % 2 == 0 && (point1 == null || point2 == null)) {
return;
}
if (click % 2 == 0) {
if (isOval) {
int w = point2.x - point1.x;
int h = point2.y - point1.y;
int r = (int) Math.sqrt(w * w + h * h);
g.drawOval(point1.x - r / 2, point1.y - r / 2, r + r / 2, r
+ r / 2);
} else {
g.drawRect(point1.x, point1.y, point2.x - point1.x,
point2.y - point1.y);
}
return;
}
if (isOval) {
if (click % 2 == 1) {
int w = p.x - point1.x;
int h = p.y - point1.y;
int r = (int) Math.sqrt(w * w + h * h);
g.drawOval(point1.x - r / 2, point1.y - r / 2, r + r / 2, r
+ r / 2);
}
} else {
g.drawRect(point1.x, point1.y, p.x - point1.x, p.y - point1.y);
}
}
public void mouseClicked(MouseEvent mouseevent) {
click++;
if (click % 2 == 1) {
point1 = mouseevent.getPoint();
} else {
point2 = mouseevent.getPoint();
}
}
public void mouseEntered(MouseEvent mouseevent) {
}
public void mouseExited(MouseEvent mouseevent) {
}
public void mousePressed(MouseEvent mouseevent) {
}
public void mouseReleased(MouseEvent mouseevent) {
}
}
class MyTimesk extends TimerTask {
public void run() {
repaint();
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-16
百度,想飞社区,在资源里找,有JAVA画板的源码程序,额,在JAVA开发-JAVA实例里,包括源码和说明文档,你参考一下吧
第2个回答  2012-07-17
JAVA 的图形界面早就过时了 , 现在比较流行的及时Android 和 web 服务了
第3个回答  2012-08-16
awt swing 应该过时了吧,现在各种UI
第4个回答  2012-07-18
百度吧
第5个回答  2012-07-16
作业题也来?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网