java图形问题~~~郁闷!!!:( 最原始最简单的方法就是获得(mousedown)坐标(startx,starty),获得(mouseup)坐标(endx,endy),这就是矩形的对角坐标,然后根据公式算出其他两个点的坐标,再把他们连线画出来就行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你想要那种应用的,applet或是application 里面有线,谢先,圆,把圆该一下draw(Rectangle2D.DOUBLE);applet 你继承字japplet酒兴import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Test extends JFrame{ private JButton b1,b2,b3; private Draw dr; public Test () { dr =new Draw(); b1 = new JButton("line"); b1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { dr.setDraw(e.getActionCommand()); dr.repaint(); } } ); b2 = new JButton("oval"); b2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { dr.setDraw(e.getActionCommand()); dr.repaint(); } } ); b3 = new JButton("xie"); b3.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { dr.setDraw(e.getActionCommand()); dr.repaint(); } } ); Container c = getContentPane(); c.add(b1,BorderLayout.SOUTH); c.add(b2,BorderLayout.NORTH); c.add(b3,BorderLayout.WEST); c.add(dr,BorderLayout.CENTER); validate(); show(); setSize(200,200); repaint(); } public static void main(String args[]) { Test t =new Test(); t.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; Vector ovals ; Vector write; private String ss; protected int x1,x2,y1,y2; Line cur,curo; public Draw() { lines = new Vector(); ovals = new Vector(); write = new Vector(); setPreferredSize(new Dimension(150,100)); setBackground(Color.white); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if((cur == null)||(curo==null)) { cur = new Line(); curo = new Line();// text = new Line(); } if(ss=="line") { cur.x0 = e.getX(); cur.y0 = e.getY(); } else if(ss=="oval") { curo.x0 = e.getX(); curo.y0 = e.getY(); } else if(ss=="xie")//跟下一个程序对比这里 { x1=e.getX(); y1=e.getY(); }//。 } public void mouseReleased(MouseEvent e) { if(ss=="line") { cur.x1 = e.getX(); cur.y1 = e.getY(); lines.add(cur); cur =null; } else if (ss=="oval") { curo.x1 = e.getX(); curo.y1 = e.getY(); ovals.add(curo); curo = null; } } }); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ if(ss =="line") { cur.x1 = e.getX(); cur.y1 = e.getY(); } else if(ss=="oval") { curo.x1 = e.getX(); curo.y1 = e.getY(); } else if(ss == "xie")//跟下一个程序对比这里 { Line text = new Line(); text.x0 = x1; text.y0 = y1; text.x1 = e.getX(); text.y1 = e.getY(); x1=e.getX(); y1=e.getY(); write.add(text); }//。。// else if(ss=="xie")//跟下一个程序对比这里// {// x2=e.getX();// y2=e.getY();// }// repaint(); } }); } public void setDraw(String s) { ss=s; } public void paintComponent(Graphics g) { super.paintComponent(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); for(int i = 0;i<ovals.size();i++){ Line tmpo = (Line)ovals.elementAt(i); g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1), Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1)); } if(curo != null ) g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1), Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1)); for(int i = 0;i<write.size();i++){ Line tmp = (Line)write.elementAt(i); g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1); }// if(x1==-1 || y1==-1)////跟下一个程序对比这里。。// {//// x1=x2;// y1=y2;// }// g.drawLine(x1,y1,x2,y2);//跟下一个程序对比这里//// x1=x2;// y1=y2;//。 }} 把g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1), Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));改成g.drawRect(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1), Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));就是矩形 JavaBoyCaoJi(老曹) 怎么到处都有你!还总是UP 以前做的作业, 你参考一下http://my.nbip.net/homepage/zhouB403/MyCode/2dDraw.exe java新手的疑惑。。。 java非阻塞IO通信问题。 请问Java中JButton可不可以实现文字在图片的下方显示,而不是默认的右边 java编程中,如何使主JFrame窗体弹出子对话框,点击其他区域无效?有无现成函数?如何使用? 急急急急急急!!! 请问,哪里有SWT/JFace 的API帮助下载? 求助,关于日期时间的正则表达式 只能得到地址值!姓名、性别的值得不到! 函数传参中的 final 超級菜的問題 请大家帮忙 请教:不明原因的错误,高手请进! 请问如何去掉JInternalFrame的标题栏?
draw(Rectangle2D.DOUBLE);applet 你继承字japplet酒兴
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame{
private JButton b1,b2,b3;
private Draw dr;
public Test ()
{
dr =new Draw();
b1 = new JButton("line");
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
b2 = new JButton("oval");
b2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
b3 = new JButton("xie");
b3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
Container c = getContentPane();
c.add(b1,BorderLayout.SOUTH);
c.add(b2,BorderLayout.NORTH);
c.add(b3,BorderLayout.WEST);
c.add(dr,BorderLayout.CENTER);
validate();
show();
setSize(200,200);
repaint();
}
public static void main(String args[])
{
Test t =new Test();
t.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;
Vector ovals ;
Vector write;
private String ss;
protected int x1,x2,y1,y2;
Line cur,curo; public Draw()
{ lines = new Vector();
ovals = new Vector();
write = new Vector();
setPreferredSize(new Dimension(150,100));
setBackground(Color.white);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if((cur == null)||(curo==null))
{
cur = new Line();
curo = new Line();
// text = new Line();
}
if(ss=="line")
{
cur.x0 = e.getX();
cur.y0 = e.getY();
}
else if(ss=="oval")
{
curo.x0 = e.getX();
curo.y0 = e.getY();
}
else if(ss=="xie")//跟下一个程序对比这里
{
x1=e.getX();
y1=e.getY();
}//。
}
public void mouseReleased(MouseEvent e) { if(ss=="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur =null;
}
else if (ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
ovals.add(curo);
curo = null;
}
}
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(ss =="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
}
else if(ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
} else if(ss == "xie")//跟下一个程序对比这里
{
Line text = new Line();
text.x0 = x1;
text.y0 = y1;
text.x1 = e.getX();
text.y1 = e.getY();
x1=e.getX();
y1=e.getY();
write.add(text);
}//。。// else if(ss=="xie")//跟下一个程序对比这里
// {
// x2=e.getX();
// y2=e.getY();
// }//
repaint();
}
});
}
public void setDraw(String s)
{
ss=s;
}
public void paintComponent(Graphics g) {
super.paintComponent(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);
for(int i = 0;i<ovals.size();i++){
Line tmpo = (Line)ovals.elementAt(i);
g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1),
Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1));
}
if(curo != null )
g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));
for(int i = 0;i<write.size();i++){
Line tmp = (Line)write.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
// if(x1==-1 || y1==-1)////跟下一个程序对比这里。。
// {
//
// x1=x2;
// y1=y2;
// }
// g.drawLine(x1,y1,x2,y2);//跟下一个程序对比这里
//
// x1=x2;
// y1=y2;//。
}
}
Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));
改成g.drawRect(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));就是矩形
怎么到处都有你!
还总是UP