我在作画线的时候,每次最小化后,重画的线与原来的位置都不同了,请大家帮忙改一下,怎样才能与原来的位置一样呀?
程序如下:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;class MyLine
{
private int x1,x2,y1,y2;
public MyLine(int x1,int x2,int y1,int y2)
{
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawString("起始坐标:"+new String(x1+","+y1),x1,y1);
}
}public class DrawLine extends Frame
{
Vector v=new Vector();
public static void main(String[]args)
{
DrawLine dl=new DrawLine();
dl.init();
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
Enumeration en=v.elements();
while(en.hasMoreElements())
{
MyLine m=(MyLine)en.nextElement();
m.drawMe(g);
}
}
public void init()
{
setSize(300,300);
setVisible(true);
addMouseListener(new MouseAdapter()
{
int x1,x2,y1,y2;
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=e.getComponent().getGraphics();
g.setColor(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.drawString(new String("起始点坐标:"+x1+","+y1),x1,y1);
v.add(new MyLine(x1,y1,x2,y2));
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}}
);
}
}
程序如下:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;class MyLine
{
private int x1,x2,y1,y2;
public MyLine(int x1,int x2,int y1,int y2)
{
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawString("起始坐标:"+new String(x1+","+y1),x1,y1);
}
}public class DrawLine extends Frame
{
Vector v=new Vector();
public static void main(String[]args)
{
DrawLine dl=new DrawLine();
dl.init();
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
Enumeration en=v.elements();
while(en.hasMoreElements())
{
MyLine m=(MyLine)en.nextElement();
m.drawMe(g);
}
}
public void init()
{
setSize(300,300);
setVisible(true);
addMouseListener(new MouseAdapter()
{
int x1,x2,y1,y2;
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Graphics g=e.getComponent().getGraphics();
g.setColor(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.drawString(new String("起始点坐标:"+x1+","+y1),x1,y1);
v.add(new MyLine(x1,y1,x2,y2));
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}}
);
}
}
解决方案 »
- 线程共享数据的问题,求赐教
- .ini读到jTextField
- JAVA中空字符的疑问?
- 菜鸟问题……CMD JAVAC
- 在JAVA APPLET中可以便用JDBC或别的方法访问服务器端的数据库吗?
- 问一道笔试题
- jtable 一次只选中一行而不是一格,还有怎么响应编辑的事件,谢谢了
- 求救,通过RMI如何把远程的文件copy到本地,应该靠传递什么呢???
- 如何将中文文件存入数据库?是文件,不是表单,谢谢!!
- 我为什么运行不了sun j2sdkee1.3 doc中的sample :Converter?
- jPasswordField.getPassword()问题
- 如何实现自由拖动JButton,谢谢!
import java.awt.event.*;
import java.util.*;
import javax.swing.*;class MyLine
{
private int x1,x2,y1,y2;
public MyLine(int x1,int x2,int y1,int y2)
{
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void drawMe(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
g.drawString("起始坐标:"+new String(x1+","+y1),x1,y1);
}
}public class DrawLine extends Frame
{
Vector v=new Vector();
public static void main(String[]args)
{
DrawLine dl=new DrawLine();
dl.init();
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
Enumeration en=v.elements();
while(en.hasMoreElements())
{
MyLine m=(MyLine)en.nextElement();
m.drawMe(g);
}}
public void init()
{
setSize(300,300);
setVisible(true);
addMouseListener(new MouseAdapter()
{int x1,x2,y1,y2;
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();Graphics g=e.getComponent().getGraphics();
g.setColor(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.drawString(new String("起始点坐标:"+x1+","+y1),x1,y1);
v.add(new MyLine(x1,x2,y1,y2)); // here pay attention
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
((Window)e.getSource()).dispose();
System.exit(0);
}}
);
}
}
用lineto()