我们最近开始使用 Applet 并用它做一些事情,所以最后一个类是画线。因此,在编写成功的代码后,我出于某种我不知道的原因更改了一行代码,现在它不画一条线,而是画一条奇怪的射线。 这是代码,因此我们将不胜感激。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class SimplePaint extends Applet {
private int lastx;
private int lasty;
@Override
public void init() {
this.setForeground(Color.BLUE);
this.addMouseListener(new PositionRecorder());
this.addMouseMotionListener(new LineDrawer());
}
public int getLastx() {
return lastx;
}
public void setLastx(int lastx) {
this.lastx = lastx;
}
public int getLasty() {
return lasty;
}
public void setLasty(int lasty) {
this.lasty = lasty;
}
private class PositionRecorder extends MouseAdapter{
public void mousePressed(MouseEvent e) {
setLastx(e.getX());
setLasty(e.getY());
}
}
private class LineDrawer extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e) {
int x=e.getX();
int y=e.getY();
Graphics g;
g=getGraphics();
g.drawLine(lastx, lasty, x, y);
}
}
}
我清楚地记得弄乱了这部分代码(如果有帮助)
private class PositionRecorder extends MouseAdapter{
public void mousePressed(MouseEvent e) {
setLastx(e.getX());
setLasty(e.getY());
最佳答案
在 AWT 中绘画是一个有据可查的过程。您应该首先阅读:
- Painting in AWT and Swing ,特别是,我建议您阅读有关 The Paint Method 的部分
- A Closer Look at the Paint Mechanism
引自《仔细观察绘画机制》
the paint method (defined by java.awt.Component.) This method will be executed by the painting subsystem whenever you component needs to be rendered.
在 AWT 中,绘制是破坏性的,任何时候 paint
被调用时,您都将完全重新绘制组件状态。这提出了许多您需要解决的问题。
对于需要绘制的任何内容,您需要一种可靠的方式来存储这些状态并在调用 paint
时重新应用它们。在您的情况下,像 ArrayList 这样简单的东西就适合,但您也可以使用 BufferedImage 作为后备缓冲区并将提交的绘图元素绘制到其中。 p>
第一种方法的好处是提供撤消更改的能力,但就绘画而言,并不像简单地绘画图像那么快。
更聪明的方法是同时使用两者。维护 ArrayList
中的绘制状态,在提交到 BufferedImage
时渲染每个状态,并且当您想要执行撤消时,从新的 BufferedImage
开始code> 并将所有状态绘制到您不需要的程度...但这超出了问题的范围
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class SimplePaint extends Applet {
private Point clickPoint;
private Point dragPoint;
@Override
public void init() {
this.setForeground(Color.BLUE);
this.addMouseListener(new PositionRecorder());
this.addMouseMotionListener(new LineDrawer());
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (clickPoint != null && dragPoint != null) {
g.drawLine(clickPoint.x, clickPoint.y, dragPoint.x, dragPoint.y);
}
}
private class PositionRecorder extends MouseAdapter {
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
// This is where I'd reset the clickPoint and dragPoint
// to null and apply the line to some structure which can
// redraw it in the `paint` method, maybe something like
// a ArrayList
// Alternativly, you could render the result to a BufferedImage
// and paint that inside the paint method
}
}
private class LineDrawer extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
dragPoint = e.getPoint();
repaint();
}
}
}
关于java - 现在用Applet画线全乱了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058707/