Lesson10
Re: Lesson10
- 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());
- }
- }
Re: Lesson10
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());
}
}
Re: Lesson10
Add the file WorldRectangle (java application) to the project.
Add osp.jar to the project.
Add osp.jar to the project.
Re: Lesson10
- 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
- }
- }
Re: Lesson10
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
}
}
Re: Lesson10
The screenshot of DrawingApp1 program.
- Attachments
-
- DrawingApp1.png (20.69 KiB) Viewed 2727 times