Java – internal fill of jtextarea with background image
My ultimate goal is to have a jtextarea with a background image I found that the online code showed me how to do this, but now the problem I encounter is that the text is on top of the image
That's what I mean:
Is there any way to add an inward indentation so that the text does not overlap the edge of the image?
This is the original comment bubble image
This is the code:
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Graphics; import java.awt.Image; import javax.swing.GrayFilter; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class myBackgroundSample { String file; public myBackgroundSample(String i) { file = i; setItUp(); } public void setItUp() { final ImageIcon imageIcon = new ImageIcon(file); JTextArea textArea = new JTextArea() { Image image = imageIcon.getImage(); public void paint(Graphics g) { setOpaque(false); g.drawImage(image,this); super.paint(g); } }; JFrame frame = new JFrame("Background Example"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); JScrollPane scrollPane = new JScrollPane(textArea); Container content = frame.getContentPane(); content.add(scrollPane,BorderLayout.CENTER); frame.setSize(400,400); frame.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub String right = "chat1.jpg"; myBackgroundSample temp = new myBackgroundSample(right); } }
Solution
Use a custom border that extends abstractborder Something like this:
Get the exact shape and color for the reader to practice
The above is all the contents of the internal filling of Java jtextarea with background image collected and sorted by programming house for you. I hope this article can help you solve the program development problems encountered in the internal filling of Java jtextarea with background image.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.