急!!!!!!!!!!帮我分析这个程序 你的处理方式很独特呀。把东西都放在Vector里面。你不要保存那么多也许就没事了。及时的清空。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. 把第一个addMouseListener(...)去掉.2. 改下面的函数成: public void paint(Graphics g) { super.paint(g); if(vx2.size()==0) return; x1 = ((Integer)vx2.get(0)).intValue(); y1 = ((Integer)vy2.get(0)).intValue(); for(int i = 1;i<vx2.size();i++) { x2 = ((Integer)vx2.get(i)).intValue(); y2 = ((Integer)vy2.get(i)).intValue(); g.drawLine(x1,y1,x2,y2); x1=x2;y1=y2; } } to helpall照你的方法,线是可以画出来不过是连续 的线他要画几根直线 程序很乱,结构不清晰,此外Vector的使用也不妥当,我的感觉是你没有理解Vector.是不是老师布置的作业啊。我学Vc的时候做过类似的作业。 试试import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;public class Drawline extends JFrame { private Draw dr; public Drawline() { dr = new Draw(); dr.setBackground(Color.white); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(dr); setSize(300,300); show(); } public static void main(String args[]) { Drawline dl = new Drawline(); dl.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }} import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;class Draw extends JPanel{ private int x1,y1,x2,y2; final Vector vx1,vy1,vx2,vy2; public Draw() { vx1 = new Vector (1); vy1 = new Vector (1); vx2 = new Vector (1); vy2 = new Vector (1); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { vector1(e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { vector2(e.getX(),e.getY()); repaint(); } } ); } public void vector1(int x,int y){ vx1.add(Integer.valueOf(""+x)); vy1.add(Integer.valueOf(""+y)); } public void vector2(int x,int y){ vx2.add(Integer.valueOf(""+x)); vy2.add(Integer.valueOf(""+y)); } public void paint(Graphics g) { super.paint(g); if(vx2.size()==0) return; x1 = ((Integer)vx2.get(0)).intValue(); y1 = ((Integer)vy2.get(0)).intValue(); for(int i = 1;i<vx2.size();i=i+2) { x2 = ((Integer)vx2.get(i)).intValue(); y2 = ((Integer)vy2.get(i)).intValue(); g.drawLine(x1,y1,x2,y2); if(i != vx2.size()-1){ x1 = ((Integer)vx2.get(i+1)).intValue(); y1 = ((Integer)vy2.get(i+1)).intValue(); } } } public Dimension getPreferredSize() { return new Dimension(150,100); }} //终结版import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class DrawLine extends JFrame { private Draw dr; public DrawLine() { dr = new Draw(); dr.setBackground(Color.white); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(dr); setSize(300,300); show(); } public static void main(String args[]) { DrawLine dl = new DrawLine(); dl.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); }}class Line { public int x0,y0,x1,y1;}class Draw extends JPanel{ Vector lines = new Vector(); Line cur; public Draw() { setPreferredSize(new Dimension(150,100)); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if(cur == null) cur = new Line(); cur.x0 = e.getX(); cur.y0 = e.getY(); } public void mouseReleased(MouseEvent e) { cur.x1 = e.getX(); cur.y1 = e.getY(); lines.add(cur); cur = null; } }); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ cur.x1 = e.getX(); cur.y1 = e.getY(); repaint(); } }); } public void paint(Graphics g) { super.paint(g); for(int i = 0;i<lines.size();i++){ Line tmp = (Line)lines.elementAt(i); g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1); } if(cur != null ) g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1); }} 请问一个Map集合的问题.. 求一款免费好用的GUI画图工具 请大家帮帮忙 关于时区的一个问题 使用Java如何在创建文件时,选择要创建文件的编码(注意:不是文件中内容的编码) JFrame中的什么方法可以使最大化按钮失去作用! 关于static的问题 请教!!!在线等 关于图形的问题!急!!!!!! java Swing中JButton上设置的自定义图片有时候显示有时候不显示,当鼠标划过的时候才显示。 关于取组件的大小的问题,请教高手 向量类及下拉框的应用?
public void paint(Graphics g)
{
super.paint(g);
if(vx2.size()==0)
return;
x1 = ((Integer)vx2.get(0)).intValue();
y1 = ((Integer)vy2.get(0)).intValue();
for(int i = 1;i<vx2.size();i++)
{
x2 = ((Integer)vx2.get(i)).intValue();
y2 = ((Integer)vy2.get(i)).intValue();
g.drawLine(x1,y1,x2,y2);
x1=x2;y1=y2;
}
}
照你的方法,线是可以画出来
不过是连续 的线
他要画几根直线
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;public class Drawline extends JFrame {
private Draw dr; public Drawline()
{
dr = new Draw();
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
setSize(300,300);
show();
} public static void main(String args[])
{
Drawline dl = new Drawline();
dl.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class Draw extends JPanel{
private int x1,y1,x2,y2;
final Vector vx1,vy1,vx2,vy2;
public Draw()
{
vx1 = new Vector (1);
vy1 = new Vector (1);
vx2 = new Vector (1);
vy2 = new Vector (1);
addMouseListener(
new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ vector1(e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e)
{
vector2(e.getX(),e.getY());
repaint();
}
}
); }
public void vector1(int x,int y){
vx1.add(Integer.valueOf(""+x)); vy1.add(Integer.valueOf(""+y));
}
public void vector2(int x,int y){
vx2.add(Integer.valueOf(""+x)); vy2.add(Integer.valueOf(""+y));
}
public void paint(Graphics g)
{
super.paint(g);
if(vx2.size()==0)
return;
x1 = ((Integer)vx2.get(0)).intValue();
y1 = ((Integer)vy2.get(0)).intValue();
for(int i = 1;i<vx2.size();i=i+2)
{
x2 = ((Integer)vx2.get(i)).intValue();
y2 = ((Integer)vy2.get(i)).intValue();
g.drawLine(x1,y1,x2,y2);
if(i != vx2.size()-1){
x1 = ((Integer)vx2.get(i+1)).intValue();
y1 = ((Integer)vy2.get(i+1)).intValue();
} } } public Dimension getPreferredSize()
{
return new Dimension(150,100);
}}
import java.awt.*;
import java.awt.event.*;
import java.util.*;import javax.swing.*;public class DrawLine extends JFrame {
private Draw dr;
public DrawLine()
{
dr = new Draw();
dr.setBackground(Color.white);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(dr);
setSize(300,300);
show();
} public static void main(String args[])
{
DrawLine dl = new DrawLine();
dl.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}class Line {
public int x0,y0,x1,y1;
}class Draw extends JPanel{
Vector lines = new Vector();
Line cur; public Draw()
{
setPreferredSize(new Dimension(150,100));
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(cur == null)
cur = new Line();
cur.x0 = e.getX();
cur.y0 = e.getY();
}
public void mouseReleased(MouseEvent e) {
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur = null;
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
cur.x1 = e.getX();
cur.y1 = e.getY();
repaint();
}
});
} public void paint(Graphics g) {
super.paint(g); for(int i = 0;i<lines.size();i++){
Line tmp = (Line)lines.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
if(cur != null )
g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
}}