Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to place a picture on a GUI control and output translucent text on a picture

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to place pictures on GUI controls and output translucent text on pictures. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

The java.awt.image.* package is mainly used, which inherits the RGBImageFilter class and modifies the pixels of the image by alpha (transparency). Take applet as an example:

Import java.applet.*

Import java.awt.*

Import java.awt.event.*

Import java.awt.font.*

Import java.awt.image.*

Public class applet6 extends Applet {

MediaTracker mt

Image img=null

Image im=null

ImageFilter imgf=null

FilteredImageSource fis=null

Public void init () {

Img=this.getImage (this.getCodeBase (), "d.jpg")

Mt=new MediaTracker (this)

Mt.addImage (img,0)

Try {

Mt.waitForAll (0)

Catch (Exception ex) {System.err.println (ex.toString ());}

Im=this.createImage (100100); / / create a new picture for entering text for subsequent transparent processing

Graphics g2=im.getGraphics ()

G2.setFont (new Font ("Song style", Font.BOLD,15))

G2.drawString ("translucent text", 10pm 50)

Imgf=new myImage (100100100); / / call custom classes for object construction

Fis=new FilteredImageSource (im.getSource (), imgf); / / A pair of image sources (image producers) are filtered to construct FilteredImageSource object instances.

Im=this.createImage (fis); / / generate Image through FilteredImageSource instance

}

Public void paint (Graphics g) {

G.drawImage (img,0,0,this); / / draw a picture

G.drawImage (im,100,100,this); / / add translucent text

}

}

Class myImage extends RGBImageFilter {/ / Abstract class RGBImageFilter is a subclass of ImageFilter and inherits it to implement image ARGB processing.

Int width=0

Int height=0

Int alpha=0

Public myImage (int width,int height,int alpha) {/ / constructor to receive the size of the image to be filtered, as well as transparency

This.canFilterIndexColorModel=true

The / / TransparentImageFilter class inherits from RGBImageFilter, and its constructor requires the width and height of the original image to be passed in. This class implements the filterRGB abstract function. By default, the function passes in the ARGB value of the pixel identified by XMagi y, and the programmer returns the new ARGB value of the pixel after processing according to certain program logic.

This.width=width

This.height=height

This.alpha=alpha

}

Public int filterRGB (int xreint yjinint rgb) {

DirectColorModel dcm= (DirectColorModel) ColorModel.getRGBdefault ()

/ / the DirectColorModel class is used to decompose the ARGB value independently.

Int red=dcm.getRed (rgb)

Int green=dcm.getGreen (rgb)

Int blue=dcm.getBlue (rgb)

If (red==255&&green==255&&blue==255) / / if the pixel is white, make it transparent

Alpha=0

Return alpha

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report