GRAPHICS! Kids, we're going to learn how to draw some stuff on the screen in a Java application. Download the following code file and get it to compile.
import java.awt.*; import javax.swing.JFrame; public class GraphicsDemo1 extends Canvas { public void paint( Graphics g ) { g.setColor(Color.green); g.drawRect(50,20,100,200); // draw a rectangle g.fillOval(160,20,100,200); // draw a filled-in oval g.setColor(Color.blue); g.fillRect(200,400,200,20); // a filled-in rectangle g.drawOval(200,430,200,100); g.setColor(Color.black); g.drawString("Graphics are pretty neat.", 500, 100); int x = getWidth() / 2; int y = getHeight() / 2; g.drawString("The first letter of this string is at (" + x + "," + y + ")", x, y); } public static void main( String[] args ) { // You can change the title or size here if you want. JFrame win = new JFrame("GraphicsDemo1"); win.setSize(800,600); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsDemo1 canvas = new GraphicsDemo1(); win.add( canvas ); win.setVisible(true); } }
Assignments turned in without these things will receive
half credit or less. Answer any questions in comments at the top of
GraphicsDemo1.java
.
g.drawRect()
, there are four numbers. What
do they mean? Try changing them to figure it out.
fillOval()
? What do the four numbers mean here?
drawString()
?
©2013 Graham Mitchell
This assignment is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.