这是我编的一个画线的小程序,反正无论如何就是画不出线啊,求哪位高手帮我看看啊~~~~~谢谢了~~~~~~~~~~ 15

packageline;importjava.awt.Canvas;importjava.awt.Color;importjava.awt.Graphics;import... package line;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class BresenhamLine extends JFrame
{
private static final long serialVersionUID = 1L;
public BresenhamLine()
{
super("画线");
this.setBounds(400,300,400,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(new BresenhamLineCanvas());
this.setVisible(true);
}
class BresenhamLineCanvas extends Canvas implements MouseListener
{
private static final long serialVersionUID = 1L;
private int dX;
private int dY;
private boolean isCalY;
private Point curPoint;
private Point start,end;
private int epsilon;
public BresenhamLineCanvas()
{
start = end = null;
this.addMouseListener(this);
}
public void mousePressed(MouseEvent me)
{
start = new Point(me.getX(),me.getY());
}
public void mouseReleased(MouseEvent me)
{
end = new Point(me.getX(),me.getY());
}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void drawPoint(Graphics g, Point point)
{
g.fillOval(point.x, point.y, 1, 1);
}
public void drawLine(Graphics g)
{
g.setColor(Color.BLACK);
Point curPoint = new Point();
for (begin(); hasNext();curPoint = next())
{
drawPoint(g, curPoint);
}
}
public void begin()
{
dY = end.y - start.y;
dX = end.x - start.x;
if (dX == 0 && dY == 0)
{
return;
}
else if (Math.abs(dY) <= Math.abs(dX))
{
if (dX < 0)
{
Point temp = start;
start = end;
end = temp;
dX = -dX;
dY = -dY;
}
isCalY = true;
}
else
{
if (dY < 0)
{
Point temp = start;
start = end;
end = temp;
dY = -dY;
dX = -dX;
}
int temp = start.x;
start.x = start.y;
start.y = temp;

temp = end.x;
end.x = end.y;
end.y = temp;

temp = dX;
dX = dY;
dY = temp;
isCalY = false;
}
if (dY >= 0)
{
epsilon = 2 * dY - 1;
}
else
{
epsilon = 2 * dY + 1;
}
curPoint = start;
return;
}
public boolean hasNext()
{
if (curPoint.x >= end.x)
{
return false;
}
else
{
return true;
}
}
public Point next()
{
Point point = new Point();
if (isCalY)
{
point = curPoint;
}
else
{
point.x = curPoint.y;
point.y = curPoint.x;
}
if (dY >= 0)
{
if (epsilon < 0)
{
curPoint.x++;
epsilon += 2 * dY;
}
else
{
curPoint.x++;
curPoint.y++;
epsilon += 2 * dY - 2 * dX;
}
}
字数限制,后面附一张图片~~~先谢谢大家拉
展开
 我来答
xianqinxin
2011-10-13 · TA获得超过243个赞
知道答主
回答量:58
采纳率:0%
帮助的人:39.7万
展开全部
一个画图要这么多代码?不就是获取坐标后重绘吗?你写的我看的都烦
我给你个:
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class draw extends JFrame implements MouseMotionListener,MouseListener{
int startX=0;
int startY=0;
public static void main(String args[])
{
draw d=new draw();//初始化对象,调用构造方法
d.setVisible(true);
d.setBounds(0, 0, 300, 300);
}
public draw()
{
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e) {
Graphics g=this.getGraphics();
g.drawLine(startX, startY, e.getX(), e.getY());
startX=e.getX();
startY=e.getY();
}
public void mousePressed(MouseEvent e) {
startX=e.getX();
startY=e.getY();
}
//后面这些用不到,但是因为是抽象的所以方法要全部实例化
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
追问
我的意思是不用JAVA自己的drawline函数,自己写函数画线,懂?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式