java实现类似 鼠标在桌面上点击左键并拖动,能出现一个矩形的功能,代码如下,但是不行,请指教!
不是吝啬啊,实在是只有6分了,谢谢了importjava.util.*;importjavax.swing.filechooser.*;importjava.io.*;i...
不是吝啬啊,实在是只有6分了,谢谢了
import java.util.*;
import javax.swing.filechooser.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class test
{
public static void main(String[] args)
{
new fm();
}
}
class fm extends JFrame
{
int orgx,orgy,endx,endy;
Image oimage=null;
Graphics gr=null;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
fm()
{
setVisible(true);
setSize(500,300);
oimage=createImage(d.width,d.height);
gr=oimage.getGraphics();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("...");
}
public void mouseDragged(MouseEvent e)
{
/*endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx,endy);
gr.setColor(Color.BLUE);
gr.drawRect(orgx,orgy,endx,endy);*/
}
});
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx,endy);
gr.setColor(Color.BLUE);
gr.drawRect(orgx,orgy,endx,endy);
g.clearRect(orgx-1, orgy-1, endx-1, endy-1);
}
}
);
}
public void paint(Graphics g)
{
if(gr!=null)
{
g.drawImage(oimage,0,0,this);
}
}
} 展开
import java.util.*;
import javax.swing.filechooser.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class test
{
public static void main(String[] args)
{
new fm();
}
}
class fm extends JFrame
{
int orgx,orgy,endx,endy;
Image oimage=null;
Graphics gr=null;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
fm()
{
setVisible(true);
setSize(500,300);
oimage=createImage(d.width,d.height);
gr=oimage.getGraphics();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("...");
}
public void mouseDragged(MouseEvent e)
{
/*endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx,endy);
gr.setColor(Color.BLUE);
gr.drawRect(orgx,orgy,endx,endy);*/
}
});
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx,endy);
gr.setColor(Color.BLUE);
gr.drawRect(orgx,orgy,endx,endy);
g.clearRect(orgx-1, orgy-1, endx-1, endy-1);
}
}
);
}
public void paint(Graphics g)
{
if(gr!=null)
{
g.drawImage(oimage,0,0,this);
}
}
} 展开
3个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String[] args)
{
new fm();
}
}
class fm extends JFrame
{
int orgx,orgy,endx,endy;
Image oimage=null;
Graphics gr=null;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
public fm()
{
setVisible(true);
setSize(500,300);
getGraphics().clearRect(0, 0, 500,300);
oimage=createImage(d.width,d.height);
gr=oimage.getGraphics();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("...");
}
public void mouseDragged(MouseEvent e)
{
}
});
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.clearRect(0, 0, 500,300);
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx-orgx,endy-orgy);
}
}
);
}
public void paint(Graphics g)
{
if(gr!=null)
{
g.drawImage(oimage,0,0,this);
}
}
}
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String[] args)
{
new fm();
}
}
class fm extends JFrame
{
int orgx,orgy,endx,endy;
Image oimage=null;
Graphics gr=null;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
public fm()
{
setVisible(true);
setSize(500,300);
getGraphics().clearRect(0, 0, 500,300);
oimage=createImage(d.width,d.height);
gr=oimage.getGraphics();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("...");
}
public void mouseDragged(MouseEvent e)
{
}
});
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.clearRect(0, 0, 500,300);
g.setColor(Color.BLUE);
g.drawRect(orgx,orgy,endx-orgx,endy-orgy);
}
}
);
}
public void paint(Graphics g)
{
if(gr!=null)
{
g.drawImage(oimage,0,0,this);
}
}
}
更多追问追答
追问
从左上往右下 拖的时候 是没填充的,右下往上拖的时候就是 被填充的矩形 ,这个怎么解决
?
追答
这个代码从左上往右下拖的时候是没填充的,右下往左上拖的时候就是被填充的矩形。
另外右上到左下,左下到右上你没说填不填充,我就暂时设定为不填充。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String[] args)
{
new fm();
}
}
class fm extends JFrame
{
int orgx,orgy,endx,endy;
Image oimage=null;
Graphics gr=null;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
public fm()
{
setVisible(true);
setSize(500,300);
getGraphics().clearRect(0, 0, 500,300);
oimage=createImage(d.width,d.height);
gr=oimage.getGraphics();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("...");
}
public void mouseDragged(MouseEvent e)
{
}
});
this.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endx=e.getX();
endy=e.getY();
Graphics g=getGraphics();
g.clearRect(0, 0, 500,300);
g.setColor(Color.BLUE);
int width=Math.abs(endx-orgx);
int height=Math.abs(endy-orgy);
int x=orgxendx&&orgy>endy)
g.fillRect(x, y, width, height);//这是填充的
else
g.drawRect(x,y,width,height);//这是不填充的
}
}
);
}
public void paint(Graphics g)
{
if(gr!=null)
{
g.drawImage(oimage,0,0,this);
}
}
}
展开全部
怎么画不出来?有啥提示?
追问
mouseDragged 这个事件好像一直没发生啊,拖动的过程中不会出现矩形,直到鼠标停止拖动的时候,鼠标起来, 才会出现一个矩形,而且 这矩形 感觉大很多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class test 前面没加public
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询