1 / 31

Drawing the Hilbert Curve with Applets

Learn how to draw the Hilbert curve using applets in Java. This tutorial covers topics such as using drawLineList, color, instance variables, GUI components, and reading input in applets. Examples and code snippets are provided.

vickiet
Télécharger la présentation

Drawing the Hilbert Curve with Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 10 - Applets • Using drawLineList • Color • Instance variables • GUI components • Input in applets

  2. Reading assignment • Savitch Ch. 7 & 13, or Horstmann Ch. 4 & 10.

  3. Example: drawing the Hilbert curve • We can use drawLineList to draw the Hilbert curve. No change in the HC function is needed (well, almost none....). • To keep our files small, we will place the HC function in a separate class called HilbertCurve. Thus, to create the Hilbert curve line list, we will call HilbertCurve.HC(order, segment-length).

  4. Example: drawing the Hilbert curve (cont.) The HilbertCurve class looks like our previous version, but it doesn’t have the main method: class HilbertCurve { LineList HC (int order, int sl) { .... } ... definitions of translate and other auxiliary methods ... }

  5. Example: drawing the Hilbert curve (cont.) • The Hilbert curve applet, HCApplet, has only a paint method: public class HCApplet extends Applet { public void paint (Graphics g) { drawLineList(HilbertCurve.HC(4, 10), g); } static void drawLineList (LineList L, Graphics g) { ... as shown in Class 17 ... } }

  6. Scaling the curve • The way we have drawn Hilbert curves, each curve grows larger and larger as the order increases. • Would instead like to scale the picture to fit into the window • Scaling is a simple matter of multiplying each point by a scaling factor.

  7. Scaling the curve (cont.) static LineList scale (LineList L, double sc) { if (L.empty()) return LL.nil; else { Line ln = L.hd(); ln = new Line(scale(sc, ln.x0()), scale(sc, ln.y0()), scale(sc, ln.x1()), scale(sc, ln.y1())); return LL.cons(ln, scale(L.tl(), sc)); } } static int scale (double sc, int x) { return (int)(Math.round(x*sc)); }

  8. Scaling the curve (cont.) Add a call to scale in the paint method: public void paint (Graphics g) { LineList hc = scale(HilbertCurve.HC(4, 10), 0.5); drawLineList(hc, g); }

  9. Color • The Color class (in the java.awt package) has values that denote different colors. • Symbolic constants Color.black, Color.red, etc., are predefined. • Arbitrary colors can be created by giving RGB values in the range 0-255 when constructing a Color value: • Color col1 = new Color(100,220,155); • Color darkred = new Color(100,50,50);

  10. Drawing in color • A Graphics objects has a current color, in which all lines and fills are drawn. • Change the current color of Graphics object g by the call: g.setColor(Color object) • E.g. draw a red circle: g.setColor(Color.red); g.drawOval(10,10,50,50);

  11. Example: drawing the Hilbert curve in color Suppose you want to draw the Hilbert curve with each segment in a color that depends upon the location of the segment:

  12. Example: drawing the Hilbert curve in color (cont.) static void drawLineList (LineList L, Graphics g) { if (!L.empty()) { Line ln = L.hd(); g.setColor(makeColor(ln.x0(), ln.y0())); g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1()); drawLineList(L.tl(), g); } } static Color makeColor (int x, int y) { return new Color(255,Math.min(255,x), Math.min(255,y)); }

  13. Instance variables • Variables declared inside a method (the kind we’ve used so far) are either parameters or local variables. • Variables declared outside of any method are either class variables or instance variables. (They are class variables if they are declared with the static keyword; we will consider only instance variables for now.)

  14. Instance variables (cont.) Public class MyApplet extends Applet { int x = initial-value; double y; public void init () { y = initial value; } public void paint (Graphics g) { … x … y … } } Declare here - may initialize here, or here Refer to in this or any other instance method

  15. Instance variables (cont.) • Class and instance variables can be referred to by any method in the class. (Exception: instance variables cannot be used by class methods.) • The value of the variable is the last value assigned by the last method that made an assignment to that variable.

  16. Instance variable example • In our HCApplet, the curve is calculated each time the paint method is called. To avoid this, declare an instance variable and assign to it in the init method; then refer to it in the paint method.

  17. Instance variable example (cont.) public class HCApplet extends Applet { LineList theCurve; public void init () { theCurve = LL.scale(HilbertCurve.HC(5, 10), 0.8); } public void paint (Graphics g) { drawLineList(theCurve, g); } static void drawLineList (LineList L, Graphics g) { . . . } }

  18. GUI Components • Aside from drawing, one can place Graphical User Interface (GUI) components, such as buttons and text boxes (for input). • java.awt contains many classes corresponding to components (including one called Component). See: http://java.sun.com/products/jdk/1.1/docs/api/packages.html

  19. Placing GUI components in an applet • Normally, declare an instance variable of the component type (Button, Checkbox, TextField, etc.) • In init • Initialize variable. • Use add method to place it in applet. • paint does not have to draw components - they are automatically drawn

  20. Components Some sample components: • Label: Place text in applet (alternative to drawString): new Label(“A label”); • Button: Place button, possibly with label: new Button(); new Button(“Press me”); • TextField: Create field of given width to input text: new TextField(10);

  21. Example: A component-filled applet • public class BusyApplet extends Applet { • Label L = new Label(“A label”); • Button b = new Button(); • Button bl = new Button(“Press me”); • TextField t = new TextField(10); • CheckBox cb = new Checkbox(); • public void init () { • add(L); add(b); add(bl); • add(t); add(cb); • } • }

  22. Example: A component-filled applet (cont.) • An applet can have components and also graphics. • Add paint method to previous applet: public void paint (Graphics g) { g.drawLine(0, 100, 150, 50); g.setColor(Color.red); g.fillRect(50, 50, 30, 60); }

  23. Input in applets • Users enter text in TextField components • Reading the value in a TextField involves four changes to what we’ve seen: import java.awt.*; import java.applet.*; import java.awt.event.*; public class appletname extends Applet implements ActionListener { 1 2

  24. Input in applets (cont.) TextField t = new TextField(4); public void init () { ... add(t); t.addActionListener(this); } public void actionPerformed (ActionEvent e) { ... t.getText() ... repaint(); } 3 4

  25. Input in applets (cont.) “import java.awt.event.*” must appear verbatim “implements ActionListener” must appear verbatim TextField variable, say t, is declared as instance variable In init, in addition to calling add, call t.addActionListener(this); 1 2 3

  26. Input in applets (cont.) 4 Define actionPerformed method. • Called when user types Enter key in the text field. • Use getText method of TextField class to get the String contained in t. • If there is a paint method, it needs to be called again. Call repaint().

  27. Interactive Hilbert applet • This version of the Hilbert applet allows the user to input the order of the curve to be drawn. (Needed enhancements: • Scale to window • Separate text field from drawing. )

  28. Interactive Hilbert applet source import java.awt.*; import java.applet.*; import java.awt.event.*; public class HCApplet5 extends Applet implements ActionListener { TextField order = new TextField(2); LineList theCurve; public void init () { theCurve = LLops.scale( HilbertCurve.HC(5, 10), 0.8); add(new Label("Order: ")); add(order); order.addActionListener(this); }

  29. Interactive Hilbert applet source public void paint (Graphics g) { drawLineList(theCurve, g); } static void drawLineList (LineList L, Graphics g) { if (!LL.empty(L)) { Line ln = LL.hd(L); g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1()); drawLineList(LL.tl(L), g); } } public void actionPerformed (ActionEvent e) { int ord = Integer.parseInt(order.getText()); theCurve = LLops.scale(HilbertCurve.HC(ord, 10), 0.8); repaint(); } }

  30. Interactive Hilbert screen dump

  31. The event model • Code above is one instance of the event model. • With the event model, can respond to button clicks, mouse movements, etc. • Can also use multiple text fields and distinguish which one was modified. • More next week...

More Related