CIS-257 Home: http://www.c-jump.com/bcc/c257c/c257syllabus.html
The Java 2D API facilitates color management and provides capabilities to draw many geometric primitives, such as curves, rectangles, ellipses, and methods for rendering any geometric shape.
The API is offered by
java.awt.Graphics class
java.awt.Graphics2D class
These classes allow to draw onto Swing components, using them as rendering targets.
See also:
Oracle tutorial: docs.oracle.com/javase/tutorial/2d
Java 2D tutorial from http://zetcode.com/gfx/java2d/
A program can render 2D graphics directly on a JFrame.
At the same time, a program may need UI components to enter parameters for the drawing (radio buttons, check boxes, text fileds, and so on.)
To manage UI, it is desirable to use NetBeans drag-and-drop for components and other visual design capabilities.
A better place to render 2D graphics is on a JPanel.
The goal is to override JPanel's method
public void paintComponent( Graphics graph )
The Graphics object is passed as method's parameter.
Create new NetBeans Java Application project.
Do not create main class: uncheck "Create Main Class" option. We will specify the main class later, when JFrame form is added to the project.
In the project tree view, right-click "Source Packages" and select New -> JPanel form...
Class Name: DrawingBox2D
Package: drawingDemo
The file DrawingBox2D.java is added to the project and opens in the design view. Switch to the source view of DrawingBox2D.java and paste paintComponent method after the class constructor:
public class DrawingBox2D extends javax.swing.JPanel {
/**
* Creates new form DrawingBox2D
*/
public DrawingBox2D() {
initComponents();
}
@Override
public void paintComponent( Graphics graph ) {
super.paintComponent(graph);
// Our own 2D rendering starts here:
graph.setColor( Color.RED );
graph.fillRect( 10, 20, 20, 40 );
graph.setColor( Color.BLUE );
graph.drawString( "Hello!", 35, 45 );
graph.drawLine( 100, 100, 80, 80 );
int oldx = 0;
int oldy = 0;
for ( int x = 0; x < 360; ++x ) {
int y = (int)( -sin( Math.toRadians(x) ) * 50 ) + 100;
graph.drawLine( oldx, oldy, x, y );
oldx = x;
oldy = y;
}
}//paintComponent
//...
}//class DrawingBox2D
For the above code to compile, we also need a few imports:
import java.awt.Color; import java.awt.Graphics; import static java.lang.Math.*;
In the project tree view, right-click on the drawingDemo and select New -> JFrame form... and name it FrameDemo2D
The file FrameDemo2D.java is added to the project and opens in the design view.
Select a Panel component from the palette Swing Containers category.
Place the component on the JFrame form and resize it to fit the form.
In the Properties sheet, switch to Code tab and rename the panel component as "pnlDrawingBoard".
Right-click on the panel component in FrameDemo2D.java Design view and select the Customize code... option.
Select custom creation in the drop-down list for the Initialization code
Type in
pnlDrawingBoard = new DrawingBox2D();
and click OK. This custom code is placed within the GUI Builder's guarded blocks of code, which remain read-only in the regular NetBeans source editor.
Alternatively, you can enter new DrawingBox2D() in the Properties sheet for your panel component by clicking the ellipsis (...) button next to "custom creation code" property.
Set window title for FrameDemo2D as "2D Graphics Demo" in the title field of the Properties sheet. Alternatively, this can be added to the FrameDemo2D constructor:
/**
* Creates new form FrameDemo2D
*/
public FrameDemo2D() {
initComponents();
setTitle( "2D Graphics Demo" );
}
Compile and run the application. First time NetBeans will ask for the main method, you need to confirm its location in drawingDemo.FrameDemo2D class. You should get a result similar to this:
Now you have a window form with container that hosts your drawings. Use online tutorials to learn more about 2D graphics rendering.