2024年10月fillrectjava(Java编写一个Applent程序,使用fillRect方法输出不同的颜色)

发布时间:

  ⑴fillrectjava(Java编写一个Applent程序,使用fillRect方法输出不同的颜色

  ⑵Java编写一个Applent程序,使用fillRect方法输出不同的颜色

  ⑶你刚才那题不用关闭的.追问就可以了.我是刚才那个回答的.importjava.applet.Applet;importjava.awt.Button;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.Random;publilassColorAppletextendsApplet{Buttonbt;Colorcolor=newColor(,,);Randomran=newRandom();publicvoidinit(){bt=newButton(“随机改变颜色“);bt.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(e.getSource()==bt){color=newColor((int)(ran.nextFloat()*),(int)(ran.nextFloat()*),(int)(ran.nextFloat()*));repaint();}}});add(bt);}publicvoidpaint(Graphicsg){g.setColor(color);g.fillRect(,,,);}}

  ⑷java中的fillRect问题

  ⑸从起始位置开始覆盖的,即如果x为,这一行也包括在其中。例如,左边缘为,右边缘为,它所占的像素是至,也就是说宽度为。

  ⑹java中的fillRect问题

  ⑺Android的Canvas本身没有提供fillRect函数但是它提供了个功能相近的函数。publicvoiddrawColor(intcolor)Filltheentirecanvas’bitmap(restrictedtothecurrentclip)withthespecifiedcolor,usingsrcoverporterduffmode.可以把它封装到一个fillRect函数,以便调用。publicvoidfillRect(intx,inty,intw,inth){g.clipRect(x,y,w,h);g.drawColor(p.getColor());g.clipRect(rect);}

  ⑻javag.fillRect(x,y,,)

  ⑼fillRect(x,y,w,h函数的作用是:填充一个矩形区域,x、y为起始坐标(即左上角坐标,后面两个参数分别为:w、h,是矩形区域的宽和高,这里的表示填充宽度像素,表示填充高度像素。如有其它不懂的地方可以在线问我。

  ⑽直接说event是简单,不过总要试一试才敢拿上来讲,所以就全写上来了。。。importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Point;importjava.awt.Rectangle;importjava.awt.event.MouseEvent;importjava.util.ArrayList;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.event.MouseInputAdapter;importjavax.swing.event.MouseInputListener;publilassTest{publicstaticvoidmain(Stringargs){JFrameframe=newJFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setContentPane(newPaintingPanel());frame.setBounds(,,,);frame.setVisible(true);}}classPaintingPanelextendsJPanel{ArrayList《Rectangle》list;Rectanglecurrent;publicPaintingPanel(){list=newArrayList《Rectangle》();addMouseMotionListener(mouseHandler);addMouseListener(mouseHandler);}MouseInputListenermouseHandler=newMouseInputAdapter(){PointstartPoint;publicvoidmousePressed(MouseEvente){startPoint=e.getPoint();current=newRectangle();}publicvoidmouseReleased(MouseEvente){makeRectangle(startPoint,e.getPoint());if(current.width》&¤t.height》){list.add(current);current=null;repaint();}}publicvoidmouseDragged(MouseEvente){if(current!=null){makeRectangle(startPoint,e.getPoint());repaint();}}privatevoidmakeRectangle(Pointp,Pointp){intx=Math.min(p.x,p.x);inty=Math.min(p.y,p.y);intw=Math.abs(p.x-p.x);inth=Math.abs(p.y-p.y);current.setBounds(x,y,w,h);}};publicvoidpaint(Graphicsg){super.paint(g);g.setColor(Color.BLACK);for(Rectanglerect:list){g.fillRect(rect.x,rect.y,rect.width,rect.height);}if(current!=null){g.drawRect(current.x,current.y,current.width,current.height);}}}

  ⑾java中clearRect()和fillRect()有什么区别吗,在用法上是不是可以通用的啊,谢谢大家了

  ⑿不知道你的代码是什么,从名字上来看这两个方法明显不同,一个是清除,一个是填满。另外,这两个方法不是Java标准的东西,看看源代码吧,很容易就能学会的

  ⒀g.fillRect(,,size.width,size.border=““Height);什么意思,java新手,求详细解释

  ⒁填充矩形的原方法应该是:publicvoidfillRect(int,intint,int);个参数的意思分别为,起始X坐标,起始Y坐标,宽度,高度。而你这一句g.fillRect(,,size.width,size.border=““Height);传的参数明显是有错误的.size.border=““是这字符串变量你的正确写法应该是g.fillRect(,,size.width,size.height);

  ⒂java怎么绘制一个正方形

  ⒃可使用Graphics的fillRect绘制正方形,代码如下:

  ⒄import?java.awt.Color;import?java.awt.Graphics;import?javax.swing.JFrame;public?class?Rect?extends?JFrame{Rect(){setSize(,?);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);}public?void?paint(Graphics?g){g.setColor(Color.blue);g.drawRect(,?,?,?);g.fillRect(,?,?,?);}public?static?void?main(String?args)?{new?Rect();}}

  ⒅如何在Java中进行图片剪裁疯狂JAVA

  ⒆package?test;import?java.awt.Color;import?java.awt.GraphicsD;import?java.awt.Image;import?java.awt.geom.Affiransform;import?java.awt.image.AffiransformOp;import?java.awt.image.BufferedImage;import?java.io.File;import?java.io.IOException;import?java.nio.Buffer;import?javax.imageio.ImageIO;import?javax.imageio.stream.ImageOutputStream;/**?*?裁剪、缩放图片工具类?*??*?author?CSDN?没有梦想-何必远方?*/public?class?ImgUtils?{/**?*?缩放图片方法?*??*?param?srcImageFile?*????????????要缩放的图片路径?*?param?result?*????????????缩放后的图片路径?*?param?height?*????????????目标高度像素?*?param?width?*????????????目标宽度像素?*?param?bb?*????????????是否补白?*/public?final?static?void?scale(String?srcImageFile,?String?result,int?height,?int?width,?boolean?bb)?{try?{double?ratio?=?.;?//?缩放比例File?f?=?new?File(srcImageFile);BufferedImage?bi?=?ImageIO.read(f);Image?itemp?=?bi.getScaledInstance(width,?height,?bi.SCALE_SMOOTH);//?bi.SCALE_SMOOTH//?选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。//?计算比例if?((bi.getHeight()?》?height)?||?(bi.getWidth()?》?width))?{double?ratioHeight?=?(new?Integer(height)).doubleValue()/?bi.getHeight();double?ratioWhidth?=?(new?Integer(width)).doubleValue()/?bi.getWidth();if?(ratioHeight?》?ratioWhidth)?{ratio?=?ratioHeight;}?else?{ratio?=?ratioWhidth;}AffiransformOp?op?=?new?AffiransformOp(Affiransform//?仿射转换.getScaleInstance(ratio,?ratio),?null);//?返回表示剪切变换的变换itemp?=?op.filter(bi,?null);//?转换源?BufferedImage?并将结果存储在目标//?BufferedImage?中。}if?(bb)?{//?补白BufferedImage?image?=?new?BufferedImage(width,?height,BufferedImage.TYPE_INT_RGB);//?构造一个类型为预定义图像类型之一的//?BufferedImage。GraphicsD?g?=?image.createGraphics();//?创建一个//?GraphicsD,可以将它绘制到此//?BufferedImage?中。g.setColor(Color.white);//?控制颜色g.fillRect(,?,?width,?height);//?使用?GraphicsD?上下文的设置,填充?Shape//?的内部区域。if?(width?==?itemp.getWidth(null))g.drawImage(itemp,?,?(height?-?itemp.getHeight(null))?/?,itemp.getWidth(null),?itemp.getHeight(null),Color.white,?null);elseg.drawImage(itemp,?(width?-?itemp.getWidth(null))?/?,?,itemp.getWidth(null),?itemp.getHeight(null),Color.white,?null);g.dispose();itemp?=?image;}ImageIO.write((BufferedImage)?itemp,?“JPEG“,?new?File(result));?//?输出压缩图片}?catch?(IOException?e)?{e.printStackTrace();}}/**?*?裁剪图片方法?*??*?param?bufferedImage?*????????????图像源?*?param?startX?*????????????裁剪开始x坐标?*?param?startY?*????????????裁剪开始y坐标?*?param?endX?*????????????裁剪结束x坐标?*?param?endY?*????????????裁剪结束y坐标?*?return?*/public?static?BufferedImage?cropImage(BufferedImage?bufferedImage,int?startX,?int?startY,?int?endX,?int?endY)?{int?width?=?bufferedImage.getWidth();int?height?=?bufferedImage.getHeight();if?(startX?==?-)?{startX?=?;}if?(startY?==?-)?{startY?=?;}if?(endX?==?-)?{endX?=?width?-?;}if?(endY?==?-)?{endY?=?height?-?;}BufferedImage?result?=?new?BufferedImage(endX?-?startX,?endY?-?startY,);for?(int?x?=?startX;?x?《?endX;?++x)?{for?(int?y?=?startY;?y?《?endY;?++y)?{int?rgb?=?bufferedImage.getRGB(x,?y);result.setRGB(x?-?startX,?y?-?startY,?rgb);}}return?result;}public?static?void?main(String?args)?throws?IOException?{File?input?=?new?File(“input.jpg“);BufferedImage?img?=?ImageIO.read(input);cropImage(img,?,?,?,?);File?output?=?new?File(“output.jpg“);ImageIO.write(img,?“jpg“,?output);}}

  ⒇如何用Java实现图形的放大和缩小

  ⒈要用Java实现图形的放大和缩小,可以使用以下代码:

  ⒉importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;//实现矩形在规定时间间隔里循环放大缩小;

  ⒊publilassTestextendsJFrameimplementsRunnable{?staticintw=,h=;?//w:矩形宽度,h:矩形高度;

  ⒋booleanflag=false;//false:放大,true:缩小?publicTest(){????this.setSize(,);??this.setVisible(true);??this.setTitle(w+“,“+h);??this.setLocationRelativeTo(this);??this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?}??publicvoidpaint(Graphicsg){??intwidth=getWidth();

  ⒌//窗口宽度??intheight=getWidth();??//窗口高度??g.setColor(Color.WHITE);?//设置画笔颜色??g.fillRect(,,width,height);?//填充窗口??intx=(width-w)/;?//x:矩形左上角横坐标??inty=(height-h)/;?//y:矩形左上角纵坐标??g.setColor(Color.BLUE);?//同上...??g.drawRect(x,y,w,h);

  ⒍//画矩形,实心矩形为fillRect(....)??g.setColor(Color.RED);?//同上...??g.drawOval(x,y,w,h);????//画椭圆??setTitle(w+“,“+h);????//以矩形宽和高来设置窗口标题?}?publicvoidsetSize(){??floatn=getWidth()/getWidth();??if(w==||h==)???flag=false;

  ⒎if(w==getWidth()||h==getWidth())???flag=true;??if(!flag){???w+=;???h+=n;??}??if(flag){???w-=;???h-=n;??}???}?publicvoidrun(){??while(true){???try{????Thread.sleep();//间隔???}catch(InterruptedExceptionie){}??????setSize();???repaint();??}?}?publicstaticvoidmain(Stringargs){??Testt=newTest();??Threadth=newThread(t);??th.start();?}}

  ⒏Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

  ⒐Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。