Lesson10

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

Lesson10

Post by admin »

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

Re: Lesson10

Post by admin »

  1. package drawingapp1;
  2. import org.opensourcephysics.controls.*;
  3. import org.opensourcephysics.display.*;
  4. import org.opensourcephysics.frames.*;
  5. public class DrawingApp1 extends AbstractCalculation {
  6. DisplayFrame frame = new DisplayFrame("x", "y", "Graphics");
  7. public DrawingApp1() {
  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 WorldRectangle(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", 4); // sets default input values
  24. control.setValue("ytop", 3);
  25. control.setValue("width", 5);
  26. control.setValue("height", 6);
  27. }
  28. public static void main(String[] args) { // creates a calculation control structure using this class
  29. CalculationControl.createApp(new DrawingApp1());
  30. }
  31. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson10

Post by admin »

Code: Select all

package drawingapp1;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.display.*;
import org.opensourcephysics.frames.*;
public class DrawingApp1 extends AbstractCalculation {
  DisplayFrame frame = new DisplayFrame("x", "y", "Graphics");
  public DrawingApp1() {
    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 WorldRectangle(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", 4); // sets default input values
    control.setValue("ytop", 3);
    control.setValue("width", 5);
    control.setValue("height", 6);
  }
  public static void main(String[] args) { // creates a calculation control structure using this class
    CalculationControl.createApp(new DrawingApp1());
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson10

Post by admin »

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

Re: Lesson10

Post by admin »

  1. package drawingapp1;
  2. import java.awt.*;
  3. import org.opensourcephysics.display.*;
  4. public class WorldRectangle implements Drawable {
  5. double left, top; // position of rectangle in world coordinates
  6. double width, height; // size of rectangle in world units
  7. public WorldRectangle(double left, double top, double width, double 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. // convert from world to pixel coordinates
  17. int leftPixels = panel.xToPix(left);
  18. int topPixels = panel.yToPix(top);
  19. int widthPixels = (int) (panel.getXPixPerUnit()*width);
  20. int heightPixels = (int) (panel.getYPixPerUnit()*height);
  21. g.fillRect(leftPixels, topPixels, widthPixels, heightPixels); // draws rectangle
  22. }
  23. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson10

Post by admin »

Code: Select all

package drawingapp1;
import java.awt.*;
import org.opensourcephysics.display.*;
public class WorldRectangle implements Drawable {
  double left, top;     // position of rectangle in world coordinates
  double width, height; // size of rectangle in world units
  public WorldRectangle(double left, double top, double width, double 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
    // convert from world to pixel coordinates
    int leftPixels = panel.xToPix(left);
    int topPixels = panel.yToPix(top);
    int widthPixels = (int) (panel.getXPixPerUnit()*width);
    int heightPixels = (int) (panel.getYPixPerUnit()*height);
    g.fillRect(leftPixels, topPixels, widthPixels, heightPixels); // draws rectangle
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson10

Post by admin »

The screenshot of DrawingApp1 program.
Attachments
DrawingApp1.png
DrawingApp1.png (20.69 KiB) Viewed 531 times
Post Reply