Lesson9
Re: Lesson9
- 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());
- }
- }
Re: Lesson9
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());
}
}
Re: Lesson9
Add the file PixelRectangle (java application) to the project.
Add osp.jar to the project.
Add osp.jar to the project.
Re: Lesson9
- 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
- }
- }
Re: Lesson9
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
}
}
Re: Lesson9
The screenshot of DrawingApp program.
- Attachments
-
- DrawingApp.png (19.33 KiB) Viewed 1888 times