27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Graphics, AWT, Swing I part<br />

offscreen and then displaying the entire screen at once. It is called double buffering<br />

because there are two drawing buffers and you switch between them. Use double<br />

buffering only if the above solution alone does not work. The following code snippet<br />

describes how to do it.<br />

Image offscreenImage;<br />

Graphics offscreenGraphics;<br />

offscreenImage = createImage(size().width, size().height);<br />

offscreenGraphics = offscreenImage.getGraphics();<br />

offscreenGraphics.drawImage(img, 10, 10, this);<br />

g.drawImage(offscreenImage, 0, 0, this);<br />

The following app works fine, but when I start it, I cannot close it using the X at<br />

the right top of the form...<br />

Please help me on the following. I'm just starting to use java (JDK1.3). The<br />

following app works fine, but when I start it, I cannot close it using the X<br />

at the right top of the form. What should I add to fix this problem? The<br />

source is shown below.<br />

import java.awt.*;<br />

import java.awt.event.*;<br />

public class MyApplication extends Frame{<br />

public static void main(String args[]){<br />

Frame f = new Frame("MyApp");<br />

f.setLayout(new FlowLayout());<br />

f.add(new Button("A"));<br />

f.setVisible(true);<br />

}<br />

}<br />

Answer: You should add a listener to handle the closing of the window when the X<br />

box gets clicked on.<br />

f.addWindowListener(new WindowAdapter(){<br />

public void windowClosing(WindowEvent evt){<br />

System.exit(0);<br />

}<br />

});<br />

Read on this, and other ways to do it, in the sections dealing with<br />

event handling, in whichever <strong>Java</strong> text you are using.<br />

How can I set a JFrame to be full screen at the start of a program? I want no<br />

borders, no titles and I just want to use the entire screen.<br />

Answer: Try using JWindow instead, that one can be customized to have no borders<br />

or titles... as for size, I think you can use<br />

setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().<br />

getDefaultScreenDevice().getDefaultConfiguration().getBounds());<br />

to fill out the entire screen.<br />

Why can not I mix AWT and Swing?<br />

file:///F|/350_t/350_tips/graphics-I.htm (2 of 6) [2002-02-27 21:18:07]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!