求一个用JAVA写的最简单的画图工具;能画直线就行,其他不需要。 5
2个回答
展开全部
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();
}
}
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();
}
}
展开全部
你没有重写 paint,你写的是 repaint,当然不对了,而且原来的 repaint 也不带参数的。
------------------------------------------------------
把 repaint 改成 paint;
然后在 paint 方法的第一行加上 super.paint(g);
然后在 actionPerformed 最后加上 repaint();
------------------------------------------------------
把 repaint 改成 paint;
然后在 paint 方法的第一行加上 super.paint(g);
然后在 actionPerformed 最后加上 repaint();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询