在Swing界面中,有2个椭圆。当发生一个事件以后,这二个椭圆会用一根曲线连接起来。问题出来了,怎么做出来这条曲线是2个椭圆距离最近的曲线。这个怎么做出来啊另外一个就是这条曲线上,有2个点,就是平分这条曲线三段。这个点可以拖拽,曲线的形状就会变化不知道高手们有什么好的意见吗
解决方案 »
- 为什么label无法显示?
- socket接收线程的写法
- java代码打包成jar以后,如何运行包里面的chm文件?
- 关于读取xml中格式的问题?请问回车是否能够读取到?
- jndi+ssl访问domino ldap报错"Bind failed: Username / password binds not allowed"?
- 怎样将java.util.Date和java.sql.Date类型互换?
- 关于定时器的制作的问题(在线等)
- 程序中有一按钮,点击执行一存储过程,但此存储过程执行时间较长(10分钟左右),如何利用线程,使得在执行过程中可进行另外的操作?谢谢
- 我是新手,请问Jbuilder中的控件怎样调整大小和位置,请大虾多多指点
- ★jbuilder和weblogic配置问题
- laf 的问题
- 现在搞J2SE C/S模式开发的人多吗?
1. 对用来画之个图形的组件, 如JPanel增加鼠标事件处理: addMouseListener和addMouseMotionListener
2. 画两个小四边形(Rectangle)在这2个点处, 大小随意,宽跟高为2, 3, 4等都可试试, 找出最太平间的来, 用来处理拖拽。
3. 当鼠标按下后, 检查鼠标的坐标是否在这2个小四边形中的某一个内。
4. 如果在, 那么当鼠标被拖动时, 修改被拖动的小四边形的坐标, 也同时去计算修改那条曲线的数据, 重绘。
正解。
偶都几年没有用swing啦,都忘记光啦。嘻嘻!
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;public class RectDragged extends JPanel { private Rectangle2D rect1;
private Rectangle2D rect2;
private boolean mouseInRect1 = false;
private boolean mouseInRect2 = false; public RectDragged() {
rect1 = new Rectangle2D.Float(20, 20, 8, 8);
rect2 = new Rectangle2D.Float(60, 20, 8, 8); this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (rect1.contains(e.getPoint())) {
mouseInRect1 = true;
} else if (rect2.contains(e.getPoint())) {
mouseInRect2 = true;
}
} @Override
public void mouseReleased(MouseEvent e) {
mouseInRect1 = false;
mouseInRect2 = false;
}
}); this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (mouseInRect1) {
rect1.setFrame(e.getX(), e.getY(), rect1.getWidth(), rect1
.getHeight()); RectDragged.this.repaint();
} if (mouseInRect2) {
rect2.setFrame(e.getX(), e.getY(), rect2.getWidth(), rect2
.getHeight()); RectDragged.this.repaint();
}
}
});
} @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(rect1);
g2d.setColor(Color.GREEN);
g2d.fill(rect2);
} private static void createGUIAndShow() {
JFrame frame = new JFrame(); RectDragged rd = new RectDragged();
frame.getContentPane().add(rd); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RectDragged.createGUIAndShow();
}
});
}}
this.addMouseMotionListener(new MouseAdapter() {
C:\RectDragged.java:35: 方法未覆盖其父类的方法
@Override
^
C:\RectDragged.java:34: 无法将 java.awt.Component 中的 addMouseMotionListener(java.awt.event.MouseMotionListener) 应用于 (<匿名 java.awt.event.MouseAdapter>)
this.addMouseMotionListener(new MouseAdapter() {
^
2 错误处理已完成。
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (mouseInRect1) {
rect1.setFrame(e.getX(), e.getY(), rect1.getWidth(), rect1
.getHeight()); RectDragged.this.repaint();
} if (mouseInRect2) {
rect2.setFrame(e.getX(), e.getY(), rect2.getWidth(), rect2
.getHeight()); RectDragged.this.repaint();
}
}
});