0% found this document useful (0 votes)
59 views

Exercises Java2d

This document provides Java coding exercises related to 2D drawing and graphics. It includes instructions to: 1. Modify a tic-tac-toe applet to draw thicker lines using Graphics2D. 2. Change the applet to use dashed lines. 3. Create a simple application with JLabels of different fonts, sizes on a Frame using FlowLayout. The document provides additional exercises to draw shapes with random transparency, text at angles, and toggle antialiasing. It also includes import statements needed and describes how to view available fonts.

Uploaded by

Szabó Attila
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Exercises Java2d

This document provides Java coding exercises related to 2D drawing and graphics. It includes instructions to: 1. Modify a tic-tac-toe applet to draw thicker lines using Graphics2D. 2. Change the applet to use dashed lines. 3. Create a simple application with JLabels of different fonts, sizes on a Frame using FlowLayout. The document provides additional exercises to draw shapes with random transparency, text at angles, and toggle antialiasing. It also includes import statements needed and describes how to view available fonts.

Uploaded by

Szabó Attila
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Customized Onsite Training: Java 7, JSF 2, PrimeFaces, Servlets/JSP, Ajax/jQuery, Android, Spring, Hibernate, Hadoop, GWT, REST, etc:

http://courses.coreservlets.com/

Java 2D Drawing
Note: to use any of the shape classes (e.g., Rectangle2D.Double, as in problem 4), you have to add import java.awt.geom.*; to whatever import statements you would otherwise be using.

1.

Change your first tic-tac-toe applet (from the first applet exercises) to draw 10-pixel-thick lines instead of 1-pixel-thick lines. Or, grab my version from the applet-exercises project. Since we are not (yet) using Swing, you will not change paint to paintComponent, but you will still need to cast Graphics to Graphics2D. So, your paint method will look like this:
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.doSomethingCool(...); ... }

2. 3.

Change the applet to use dashed lines. Create a JLabel object (new JLabel("some text")) and drop it into a Frame that is using FlowLayout. Use several different labels with different font sizes and system-specific fonts. Note that to use JLabel, you need import javax.swing.* at the top. The Font constructor is used in the notes, but here is a summary:
Font font = new Font("some font name", Font.PLAIN, someSize); someJLabel.setFont(font);

You can figure out the names and appearance of the fonts installed on your PC by bringing up PowerPoint, selecting some text, then looking at the font dropdown box at the top.

4. 5. 6.

Make an application that has a red background color. Have your paint method draw 20 blue rectangles, each of which have a random transparency. Make a small application that has text in various fonts drawn at various angles. Modify your application from the previous problem so that you can select whether or not antialiasing is used. Try it both ways and see if the difference is noticeable (it should be!).

You might also like