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 solve the problem that java Graphics drawImage cannot display pictures

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

Share

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

This article shows you how to solve the problem that java Graphics drawImage can not display pictures, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Java Graphics drawImage cannot display the picture package com.sitech.test; import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image; import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.JFrame; public class ImageTest {public static void main (String [] args) {EventQueue.invokeLater (new Runnable () {public void run () {JFrame frame = new ImageFrame (); frame.setTitle ("sasdas"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)) Frame.setVisible (true);}});}} class ImageFrame extends JFrame {public ImageFrame () {add (new ImageComponent ()); pack ();}} class ImageComponent extends JComponent {private static final int DEFAULT_WIDTH = 800 Image image; public ImageComponent () {String path = "com/sitech/test/111.jpg"; image= new ImageIcon (ClassLoader.getSystemResource (path)). GetImage () / / load pictures} public void paintComponent (Graphics g) {Graphics2D G2 = (Graphics2D) g; if (image==null) return; int imageWidth= image.getWidth (this); int imageHeight = image.getHeight (this); g2.drawImage (image,0,0,this); / / for in this way (int I = 0; iSimpleWidth = 42) {this.life = false Solution: load all the pictures once public void paint (Graphics g) {if (! g.drawImage (imagesBomb [count], x, y, null)) {for (int I = 0; I)

< 43; i++) { g.drawImage(imagesBomb[i], -1000, -1000, null); } g.drawImage(imagesBomb[count], x, y, null); } count++; if (count >

= 42) {this.life = false;}}

Data

I keep calling a function in Applet.

Use Graphics::drawImage to display the picture in this function, as shown below

In the initial period of time, the return value b is often false, resulting in abnormal display, which is why? Is there any way to ensure that it is displayed properly for a period of time in the beginning?

However, as long as it is displayed for a period of time, that is, after the function has been called several times, the return value b will always be true and will never return false again.

When the image in the drawImage method is not fully loaded, this method returns false.

The drawImage method loads the required image only when it is called, so when it is called for the first time, the image is not fully loaded, resulting in abnormal display or even no display of the image. After that, the image is loaded due to the method call, and then when the image is used as a parameter, it can be displayed normally.

Solution: use the MediaTracker class Image img = Toolkit.getDefaultToolkit (). GetImage (imgPath); MediaTracker t = new MediaTracker (this); t.addImage (img, 0); t.waitForAll ()

Call this code before drawImage (), and MediaTracker ensures that your image is loaded for use before DRAW.

Add an Image with an ID number through the addImage method, and waitForAll () waits for all the added images to be loaded.

The above content is how to solve the problem that java Graphics drawImage can not display pictures. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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