Lesson9

Post Reply
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Lesson9

Post by admin »

Create the project DrawingApp.
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

  1. package drawingapp;
  2. import org.opensourcephysics.controls.*;
  3. import org.opensourcephysics.display.*;
  4. import org.opensourcephysics.frames.*;
  5. public class DrawingApp extends AbstractCalculation {
  6. DisplayFrame frame = new DisplayFrame("x", "y", "Graphics");
  7. public DrawingApp() {
  8. frame.setPreferredMinMax(0, 10, 0, 10);
  9. }
  10. public void calculate() {
  11. // gets rectangle location
  12. int left = control.getInt("xleft");
  13. int top = control.getInt("ytop");
  14. // gets rectangle dimensions
  15. int width = control.getInt("width");
  16. int height = control.getInt("height");
  17. Drawable rectangle = new PixelRectangle(left, top, width, height);
  18. frame.addDrawable(rectangle);
  19. // frame is automatically rendered after Calculate button is pressed
  20. }
  21. public void reset() {
  22. frame.clearDrawables(); // removes drawables added by the user
  23. control.setValue("xleft", 60); // sets default input values
  24. control.setValue("ytop", 70);
  25. control.setValue("width", 100);
  26. control.setValue("height", 150);
  27. }
  28. public static void main(String[] args) { // creates a calculation control structure using this class
  29. CalculationControl.createApp(new DrawingApp());
  30. }
  31. }
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

Code: Select all

package drawingapp;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.display.*;
import org.opensourcephysics.frames.*;
public class DrawingApp extends AbstractCalculation {
  DisplayFrame frame = new DisplayFrame("x", "y", "Graphics");
  public DrawingApp() {
    frame.setPreferredMinMax(0, 10, 0, 10);
  }
  public void calculate() {
    // gets rectangle location
    int left = control.getInt("xleft");
    int top = control.getInt("ytop");
    // gets rectangle dimensions
    int width = control.getInt("width");
    int height = control.getInt("height");
    Drawable rectangle = new PixelRectangle(left, top, width, height);
    frame.addDrawable(rectangle);
    // frame is automatically rendered after Calculate button is pressed
  }
  public void reset() {
    frame.clearDrawables();        // removes drawables added by the user
    control.setValue("xleft", 60); // sets default input values
    control.setValue("ytop", 70);
    control.setValue("width", 100);
    control.setValue("height", 150);
  }
  public static void main(String[] args) { // creates a calculation control structure using this class
    CalculationControl.createApp(new DrawingApp());
  }
}
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

Add the file PixelRectangle (java application) to the project.
Add osp.jar to the project.
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

  1. package drawingapp;
  2. import java.awt.*; // uses Abstract Window Toolkit (awt)
  3. import org.opensourcephysics.display.*;
  4. public class PixelRectangle implements Drawable {
  5. int left, top; // position of rectangle in pixels
  6. int width, height; // size of rectangle in pixels
  7. PixelRectangle(int left, int top, int width, int height) {
  8. this.left = left; // location of left edge
  9. this.top = top; // location of top edge
  10. this.width = width;
  11. this.height = height;
  12. }
  13. public void draw(DrawingPanel panel, Graphics g) {
  14. // this method implements the Drawable interface
  15. g.setColor(Color.RED); // set drawing color to red
  16. g.fillRect(left, top, width, height); // draws rectangle
  17. }
  18. }
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

Code: Select all

package drawingapp;
import java.awt.*; // uses Abstract Window Toolkit (awt)
import org.opensourcephysics.display.*;
public class PixelRectangle implements Drawable {
  int left, top;     // position of rectangle in pixels
  int width, height; // size of rectangle in pixels
  PixelRectangle(int left, int top, int width, int height) {
    this.left = left; // location of left edge
    this.top = top;   // location of top edge
    this.width = width;
    this.height = height;
  }
  public void draw(DrawingPanel panel, Graphics g) {
    // this method implements the Drawable interface
    g.setColor(Color.RED);                // set drawing color to red
    g.fillRect(left, top, width, height); // draws rectangle
  }
}
admin
Site Admin
Posts: 3136
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson9

Post by admin »

The screenshot of DrawingApp program.
Attachments
DrawingApp.png
DrawingApp.png (19.33 KiB) Viewed 1001 times
Post Reply