Posted July 20, 2003
The goal of the Java question on the Midterm will be for you to write out two complete class definitions that will result in displaying what you are asked to display. I will give you the names and other information about the methods the graphics I specify. You will be given a specific task, such as "draw a rectangle" or "draw a Bézier curve", etc. You will then write the source code to solve the problem. Your code will be very similar to the code below. The only real difference will be the exact path methods called and their numerical parameters.
I would urge you to type the code below into your computer and run it, to be sure you understand it. Also, do some variations in the drawing commands and see how your results change.
Note in particular that you'll need to understand Bézier curves in order to get the full and correct answer to this problem. Partial credit of course if the rest of your code is correct. I will give you a picture of the curve you're supposed to draw, such as the one below.
Here is a list of all the methods used in the code below, the ones you'll need to be able to use on the Midterm. This list will appear in the Midterm. I'll also give you the imports needed, so you don't need to remember them.
Here is code for a class that sets up the JFrame, its panel and causes the frame to be displayed.
1:import java.awt.*; 2:import java.awt.geom.*; 3:import javax.swing.*; 4: 5:/** 6: * Simplest main() that sets up a JFrame and adds a JPanel. 7: * For COM1370 Summer 2003, Northeastern University 8: * @author R. P. Futrelle 9: * @version 0.1 of 19 July 2003 10: */ 11: 12: public class DemoA { 13: 14: public static void main(String[] args) { 15: 16: JFrame frame = new JFrame("COM1370sm2003 DemoA"); 17: JPanel panel = new PanelA(); 18: panel.setPreferredSize(new Dimension(400,300)); 19: frame.getContentPane().add(panel); 20: frame.pack(); 21: frame.setVisible(true); 22: } 23: 24: } 25:
Here is code for a class that extends JPanel and overrides
paintComponent()
. It uses GeneralPath to draw, fill
and stroke closed regions.
1:import java.awt.*; 2:import java.awt.geom.*; 3:import javax.swing.*; 4: 5:/** 6: * Simple JPanel that defines (overrides) a paintComponent() method. 7: * For COM1370 Summer 2003, Northeastern University 8: * @author R. P. Futrelle 9: * @version 0.1 of 19 July 2003 10: */ 11: 12: public class PanelA extends JPanel { 13: 14: public void paintComponent(Graphics g) { 15: Graphics2D g2 = (Graphics2D)g; // cast 16: GeneralPath pth = new GeneralPath(); // add to and fill and stroke 17: pth.moveTo(200,200); // "top" peak of triangle 18: pth.lineTo(100,150); // "lower" left corner 19: pth.lineTo(300,150); // "lower" right corner 20: pth.closePath(); // back to "top" 21: 22: g2.setPaint(Color.red); 23: g2.fill(pth); // fill with red 24: g2.setPaint(Color.green); // outline with green 25: g2.draw(pth); 26: 27: pth = new GeneralPath(); 28: pth.moveTo(0,10); 29: pth.curveTo(0,100,100,100,100,10); 30: pth.closePath(); 31: g2.fill(pth); // fill with green 32: } 33: }