当前位置: 技术问答>java相关
帮我查一下问题出在哪里,高分重谢!!
来源: 互联网 发布时间:2015-07-02
本文导语: /** * 重新绘制panel * @param g */ public void paintComponent(Graphics g) { int x,y; super.paintComponent(g); currentGraphics=g; Dimension d = getSize(); int clientWidth = d.width; int clientHe...
/**
* 重新绘制panel
* @param g
*/
public void paintComponent(Graphics g)
{ int x,y;
super.paintComponent(g);
currentGraphics=g;
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
if (image!=null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
if (clientWidth > imageWidth){
if (clientHeight>imageHeight){
x=(clientWidth-imageWidth)/2;
y=(clientHeight-imageHeight)/2;}
else
{x=0;y=0;}
}
else
{x=0;y=0;}
g.drawImage(image,x, y, this);
}
}
public void drawLines(Component c1,Component c2)
{
Graphics g = this.getGraphics();
paintComponent(g);
int c1x=c1.getX()+c1.getWidth()/2;
int c1y=c1.getY()+c1.getHeight()/2;
int c2x=c2.getX()+c2.getWidth()/2;
int c2y=c2.getY()+c2.getHeight()/2;
g.setColor(Color.red);
g.drawLine(c1x,c1y,c2x,c2y);]
repaint();
}
运行没有出错,但是在调用drawLines的时候,在两个控件间画的线只是闪了一下就不见了
各位老大可以帮我找出原因吗?如何把画出来的线同原来的画面合并在一起??
* 重新绘制panel
* @param g
*/
public void paintComponent(Graphics g)
{ int x,y;
super.paintComponent(g);
currentGraphics=g;
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
if (image!=null)
{
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
if (clientWidth > imageWidth){
if (clientHeight>imageHeight){
x=(clientWidth-imageWidth)/2;
y=(clientHeight-imageHeight)/2;}
else
{x=0;y=0;}
}
else
{x=0;y=0;}
g.drawImage(image,x, y, this);
}
}
public void drawLines(Component c1,Component c2)
{
Graphics g = this.getGraphics();
paintComponent(g);
int c1x=c1.getX()+c1.getWidth()/2;
int c1y=c1.getY()+c1.getHeight()/2;
int c2x=c2.getX()+c2.getWidth()/2;
int c2y=c2.getY()+c2.getHeight()/2;
g.setColor(Color.red);
g.drawLine(c1x,c1y,c2x,c2y);]
repaint();
}
运行没有出错,但是在调用drawLines的时候,在两个控件间画的线只是闪了一下就不见了
各位老大可以帮我找出原因吗?如何把画出来的线同原来的画面合并在一起??
|
补充一下:用鼠标在窗口中任意拖动即可画线,我写好后已经测试过了。