Tim*_*Tim 4 java graphics swing shapes
所以我正在制作一个应用程序,我想跟踪添加到屏幕上的形状.到目前为止,我有以下代码,但是当添加一个圆时,它无法移动/更改.理想情况下,我想要像shift一样移动它/突出显示它.
我也想知道如何制作它以便你可以将一条线从一个圆圈拖到另一个圆圈.我不知道我是否在这里使用错误的工具,但任何帮助将不胜感激.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MappingApp extends JFrame implements MouseListener {
private int x=50; // leftmost pixel in circle has this x-coordinate
private int y=50; // topmost pixel in circle has this y-coordinate
public MappingApp() {
setSize(800,800);
setLocation(100,100);
addMouseListener(this);
setVisible(true);
}
// paint is called automatically when program begins, when window is
// refreshed and when repaint() is invoked
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(x,y,100,100);
}
// The next 4 methods must be defined, but you won't use them.
public void mouseReleased(MouseEvent e ) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
x = e.getX(); // x-coordinate of the mouse click
y = e.getY(); // y-coordinate of the mouse click
repaint(); //calls paint()
}
public static void main(String argv[]) {
DrawCircle c = new DrawCircle();
}
}
Run Code Online (Sandbox Code Playgroud)
使用java.awt.geom.*创建形状,使用字段引用它们,然后使用图形对象绘制它们.
例如:
Ellipse2D.Float ellipse=new Ellipse2D.Float(50,50,100,100);
graphics.draw(ellipse);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1435 次 |
| 最近记录: |