当前位置: 代码迷 >> Java相关 >> [求助]请教关于AWT绘图问题!!
  详细解决方案

[求助]请教关于AWT绘图问题!!

热度:223   发布时间:2007-11-07 00:11:35.0
[求助]请教关于AWT绘图问题!!

为什么这个程序画了直线后,那条直线不能马上显示,而要对窗口进行放大或缩小时才能看到?
还有如果要想多画几条直线,怎样才能保留原先画的直线?
请指教!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class man extends JFrame
{
int x1,y1,x2,y2;
public man(String str)
{
super(str);
}
public void put(man JF)
{
Container contentPane=JF.getContentPane();
contentPane.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
}
});
women JP=new women();
contentPane.add(JP);
JF.setSize(600,400);
JF.setLocation(180,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public static void main(String[] args)
{
man JF=new man("菜单窗口");
JF.put(JF);
}
class women extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
}
}
}

搜索更多相关的解决方案: AWT  import  awt  直线  绘图  

----------------解决方案--------------------------------------------------------
在改了xy座标之后,再调用一下repaint()方法

----------------解决方案--------------------------------------------------------
回复:(千里冰封)在改了xy座标之后,再调用一下repa...
你好,我还不懂你的意思,能不能再详细一点,xy的坐标要怎样改,谢谢了!!
----------------解决方案--------------------------------------------------------

帮你改了,不知道你是不是想实现这样的效果?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class man extends JFrame
{
int x1,y1,x2,y2;
public man(String str)
{
super(str);
}
public void put(man JF)
{
women JP=new women();
this.add(JP);
JF.setSize(600,400);
JF.setLocation(180,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
public static void main(String[] args)
{
man JF=new man("菜单窗口");
JF.put(JF);
}
class women extends JPanel
{
public women()
{
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
repaint();
}

});
}
public void paintComponent(Graphics g)
{
// super.paintComponent(g);
g.drawLine(x1,y1,x2,y2);
}
}
}


----------------解决方案--------------------------------------------------------
回复:(netstriker) 帮你改了,不知道你是不是想实...

如果在每次画直线的时候,都能保留先前画的直线,那该怎么实现啊?请赐教!!


----------------解决方案--------------------------------------------------------
  相关解决方案