Getgraphics returns null




















This graphics context can then be used as the parameter to methods like paint , print , update , or printAll to print a single page. All of these methods result in calls to paint ; in paint , you draw whatever you want to print on the Graphics object.

The getPageDimension method returns the dimensions of the page in pixels, as a Dimension object. Since getGraphics returns a graphics context only for a single page, it is the programmer's responsibility to decide when the current page is full, print the current page, and start a new page with a new Graphics object.

The page size is chosen to roughly represent a screen but has no relationship to the page size or orientation. The getPageResolution method returns the number of pixels per inch for drawing on the page.

It is completely unclear what this means, since the number returned has no relationship to the printer resolution.

It appears to be similar to the screen resolution. The lastPageFirst method lets you know if the user configured the printer to print pages in reverse order.

If this returns true , you need to generate the last page first. If false , you should print the first page first. This is relevant only if you are trying to print a multipage document.

The end method terminates the print job. This is the last method you should call when printing; it does any cleaning up that's necessary. The finalize method is called by the garbage collector. In the event you forget to call end , finalize calls it for you. However, it is best to call end as soon as you know you have finished printing; don't rely on finalize.

The methods that start the printing process come from either the Component or Container class and are inherited by all their children. All components inherit the printAll and print methods. Containers also inherit the printComponents method, in addition to printAll and print. A container should call printComponents to print itself if it contains any components. Otherwise, it is sufficient to call printAll. These methods end up calling paint , which does the actual drawing.

Now that you know about the different classes necessary to print, let's put it all together. Printing takes four steps:. This code prints the current component: what you get from the printer should be a reasonable rendition of what you see on the screen.

Note that we didn't need to modify paint at all. That should always be the case if you want your printer output to look like your onscreen component. Of course, in many situations, you want to do more than print the appearance of a component. You often want to print the contents of some component, rather than the component itself.

For example, you may want to print the text the user has typed into a text area, rather than the text area itself. Or you may want to print the contents of a spreadsheet, rather than the collection of components that compose the spreadsheet.

You aren't restricted to printing your components' appearance. In many ways, the steps required to print arbitrary content are similar to those we outlined previously. This class is extending a JPanel and I thought calling super would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics method is returning null, so the next line that calls the graphics object is actually the one that produces the error.

Can someone please help me figure out why this is returning null? Attitude is everything. I like The Graphics object will be null until something is visible. This is demonstrated below. DanW Could you explain how to do that quickly please? I'm not that experienced in Java. Kierrow, I really don't know, I was rushing whilst creating and setting up the Class, I usually to serialize it. Show 1 more comment. Active Oldest Votes. From the Component documentation for createImage : returns an off-screen drawable image, which can be used for double buffering.

Chris Chris 3, 1 1 gold badge 22 22 silver badges 33 33 bronze badges. You'll have to remove the createImage from init or call init only once the JPanel is added to the JFrame and not in his constructor. Show 4 more comments. JPanel; import java. BufferedImage; import tm. T8y Kusum Adhikari Kusum Adhikari 3 3 silver badges 13 13 bronze badges.

Add a comment. There are several ways you can deal with it. Community Bot 1 1 1 silver badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science.



0コメント

  • 1000 / 1000