求一个用JAVA写的最简单的画图工具;能画直线就行,其他不需要。 5

 我来答
小猫钓鱼1986
2010-12-25 · TA获得超过456个赞
知道小有建树答主
回答量:140
采纳率:100%
帮助的人:164万
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

class LinePanel extends JPanel{

private static final long serialVersionUID = 1L;
private Vector<Line2D> lines = new Vector<Line2D> ();//存储画板上所有的直线
private Line2D line = new Line2D.Double() ;
private Line2D cur = new Line2D.Double() ;//起点到当前光标的直线
public LinePanel(){
super();
this.setBackground(new Color(255,255,255));

}
public void setLines(Vector<Line2D> lines){

this.lines = lines;
}
public void setCur(Line2D cur){
this.cur =cur;
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for(Line2D l2d:lines){
line = l2d;
g2d.draw(line);
}
if(cur!=null){
g2d.draw(cur);
}
}
}
public class DrawLine extends JFrame{

private static final long serialVersionUID = 1L;

private LinePanel panel;
private Vector <Line2D>lines;
private Point start;
public DrawLine(){
panel= new LinePanel();
lines = new Vector<Line2D>();

this.setBounds(100, 200, 400, 400);
this.setLayout(new BorderLayout());
this.add(panel,BorderLayout.CENTER);
this.setPanelListener();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void setPanelListener() {
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
start = new Point(e.getX(),e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
Line2D line = new Line2D.Double(start.x,start.y,e.getX(),e.getY());
lines.add(line);
panel.setLines(lines);
line = null;
}
});
panel.addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseDragged(MouseEvent e) {
panel.setCur(new Line2D.Double(start.x,start.y,e.getX(),e.getY()));
panel.repaint();
}
});
}

public static void main(String[] args) {
new DrawLine();
}
}
猪猪哒琳
2011-01-04 · TA获得超过1953个赞
知道小有建树答主
回答量:957
采纳率:0%
帮助的人:529万
展开全部
你没有重写 paint,你写的是 repaint,当然不对了,而且原来的 repaint 也不带参数的。

------------------------------------------------------
把 repaint 改成 paint;
然后在 paint 方法的第一行加上 super.paint(g);
然后在 actionPerformed 最后加上 repaint();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式