用鼠标在画布上画线:Java awt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10101673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing lines with mouse on canvas : Java awt
提问by hari_sree
The attempt is to enable drawing of figures(a line for now) with mouse on the awt canvas . Iam trying out java graphics for the first time . So not sure how to go about it . This is my first attempt :
尝试是在 awt canvas 上使用鼠标启用图形(现在是一条线)的绘制。我是第一次尝试 Java 图形。所以不知道如何去做。这是我的第一次尝试:
package def.grafi;
import java.awt.Canvas;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Dra {
Frame f = new Frame();
public void disp() {
f.setBounds(100, 100, 200, 200);
MosL ml = new MosL();
Can c = new Can();
f.add(c);
c.addMouseListener(ml);
c.addMouseMotionListener(ml);
f.setVisible(true);
}
public static void main(String[] args) {
Dra d = new Dra();
d.disp();
}
public class MosL extends MouseAdapter {
int sx = 0;
int sy = 0;
boolean onDrag = false;
@Override
public void mouseDragged(MouseEvent e) {
if (onDrag) {
int x = e.getX();
int y = e.getY();
Canvas comp = (Canvas) e.getSource();
Graphics g = comp.getGraphics();
// comp.repaint(); << for cleaning up the intermediate lines : doesnt work :(
g.drawLine(sx, sy, x, y);
return;
}
onDrag = true;
sx = e.getX();
sy = e.getY();
System.out.println("Draggg");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
if (onDrag)
onDrag = false;
}
}
public class Can extends Canvas {
@Override
public void paint(Graphics g) {
}
}
}
Problems : 1) When the windows is minimized and restored , the drawn line is gone (due to repaint) 2) What i want is the line should follow the mouse (when it is dragged) . the final line should extend from the point of pressing to the point of release of the mouse . Rite now , when the mouse moves , new lines are getting drawn . I am not sure how to clean up the intermediate lines from the canvas .
问题:1)当窗口最小化并恢复时,绘制的线消失了(由于重新绘制)2)我想要的是线应该跟随鼠标(当它被拖动时)。最后一条线应该从按下鼠标的点延伸到释放鼠标的点。现在,当鼠标移动时,新的线条会被绘制出来。我不知道如何清理画布中的中间线。
Can someone help me out on these problems ?
有人可以帮我解决这些问题吗?
回答by Mikle Garin
Here is a simple example of such "painting":
这是这种“绘画”的一个简单示例:
public static void main ( String[] args )
{
JFrame paint = new JFrame ();
paint.add ( new JComponent ()
{
private List<Shape> shapes = new ArrayList<Shape> ();
private Shape currentShape = null;
{
MouseAdapter mouseAdapter = new MouseAdapter ()
{
public void mousePressed ( MouseEvent e )
{
currentShape = new Line2D.Double ( e.getPoint (), e.getPoint () );
shapes.add ( currentShape );
repaint ();
}
public void mouseDragged ( MouseEvent e )
{
Line2D shape = ( Line2D ) currentShape;
shape.setLine ( shape.getP1 (), e.getPoint () );
repaint ();
}
public void mouseReleased ( MouseEvent e )
{
currentShape = null;
repaint ();
}
};
addMouseListener ( mouseAdapter );
addMouseMotionListener ( mouseAdapter );
}
protected void paintComponent ( Graphics g )
{
Graphics2D g2d = ( Graphics2D ) g;
g2d.setPaint ( Color.BLACK );
for ( Shape shape : shapes )
{
g2d.draw ( shape );
}
}
} );
paint.setSize ( 500, 500 );
paint.setLocationRelativeTo ( null );
paint.setVisible ( true );
}
it will remember all of the drawn shapes and with a small effort you can extend it to draw any other shapes you like.
它会记住所有绘制的形状,您只需稍加努力即可扩展它以绘制您喜欢的任何其他形状。
回答by GingerHead
Make use of Line2D object in the AWTpackage and do the following steps:
利用AWT包中的 Line2D 对象并执行以下步骤:
- Create mouse (X,Y)values for first and second clicks
- Create a
boolean
variable
to check if the click is the first or the second - Make a
List
container to contain yourLine2D
objects - Draw them in your
Can
object - Assign the beforeand after(X,Y)values through the mouse listener's event
- 为第一次和第二次点击创建鼠标(X,Y)值
- 创建一个
boolean
variable
检查点击是第一次还是第二次 - 制作一个
List
容器来包含您的Line2D
对象 - 在您的
Can
对象中绘制它们 - 分配之前和之后(X,Y)通过鼠标监听的事件值
The step number 5 could be achieved through:
第 5 步可以通过以下方式实现:
e.getX()
e.getY()
e.getX()
e.getY()
Where e is the mouse event and could be accesed through the parameter of the mouse listener method.
其中 e 是鼠标事件,可以通过鼠标侦听器方法的参数访问。
回答by Shawn Shroyer
You should use the Line2D object in the awt package, create x and y values for the first click and the second click, and a boolean determining whether it's the first or second click. Then make an ArrayList of Line2D and draw them in your Can object. So then you can assign the before and after x and y values with your event in the mouse listener by using MouseEvent.getX() and getY().
您应该使用 awt 包中的 Line2D 对象,为第一次单击和第二次单击创建 x 和 y 值,并使用一个布尔值来确定它是第一次单击还是第二次单击。然后制作 Line2D 的 ArrayList 并将它们绘制在您的 Can 对象中。因此,您可以使用 MouseEvent.getX() 和 getY() 为鼠标侦听器中的事件分配前后 x 和 y 值。