Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.



JFrame class:
class SimpleFrame extends JFrame
{
public SimpleFrame() { setSize(300, 200); }
}JFrame frame = new SimpleFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
EventQueue.invokeLater(() ->
{
frame initialization
});
setLocation setBounds setIconImage setTitle setResizable
Toolkit object:
Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize();
frame.pack();

frame.add(comp);
JComponent class:
class MyComponent extends JComponent
{
public void paintComponent(Graphics g)
{
code for drawing
}
}Graphics object for drawing:
g.drawString("Not a Hello World program", 75, 100);
public Dimension getPreferredSize() { return new Dimension(300, 200); }
Graphics object to draw shapes:
g.drawRect(75, 100, 200, 150);
float and double coordinates:
Rectangle2D rect1 = new Rectangle2D.Double(7.5, 10.0, 200, 150); Rectangle2D rect2 = new Rectangle2D.Float(7.5F, 10.0F, 200, 150);
Point2D is a point, Line2D a line:
Point2D start = new Point2D.Double(10, 20); Point2D end = new Point2D.Double(100, 110); Line2D line = new Line2D.Double(start, end);
draw method of Graphics2D class to draw any shape:
Graphics2D g2 = (Graphics2D) g; g2.draw(line);

Rectangle2D and Ellipse2D are “rectangular” shapes.
RectangularShape class has methods:
getX/getY getWidth/getHeight getCenterX/getCenterY getMaxX/getMaxY setFrame
g2.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);fill instead of draw to fill a shape:
g2.fill(rect); // fills rect with current paint
Color constants for these colors:
BLACK BLUE CYAN DARK_GRAY GRAY GREEN LIGHT_GRAY MAGENTA ORANGE PINK RED WHITE YELLOW
g2.setPaint(new Color(0, 128, 128)); // a dull blue-green
comp.setBackground(Color.PINK);
GraphicsEnvironment.getAvailableFonts method gives an array of all fonts.Font font1 = new Font("SansSerif", Font.BOLD, 14);SansSerif, Serif, Monospaced, Dialog, DialogInput
Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC
deriveFont to change properties:
Font font2 = font1.deriveFont(18F); // Change point size Font font3 = font1.deriveFont(Font.ITALIC); // Change style
FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = sansbold14.getStringBounds(message, context);
double stringWidth = bounds.getWidth(); double stringHeight = bounds.getHeight(); double ascent = -bounds.getY();


Image image = new ImageIcon(filename).getImage();
g.drawImage(image, x, y, null);
int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null);
null arguments are archaic “image observers” that were used for monitoring incremental loading over a dialup internet connection.for (int i = 0; i * imageWidth <= getWidth(); i++)
for (int j = 0; j * imageHeight <= getHeight(); j++)
if (i + j > 0)
g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight);