diff options
author | Phil Burk <[email protected]> | 2014-12-30 16:53:03 -0800 |
---|---|---|
committer | Phil Burk <[email protected]> | 2014-12-30 16:53:03 -0800 |
commit | 534969d42ca5168d645678345cd21242fe41f389 (patch) | |
tree | e8f5d1cba1ec57685e76ceb923d8da25a7846cfb /src/com/jsyn/swing/XYController.java | |
parent | a4d8ca95178d2e3acfc3299a4b73e84c2646d24e (diff) |
Initial commit of code.
Diffstat (limited to 'src/com/jsyn/swing/XYController.java')
-rw-r--r-- | src/com/jsyn/swing/XYController.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/com/jsyn/swing/XYController.java b/src/com/jsyn/swing/XYController.java new file mode 100644 index 0000000..49cb9fd --- /dev/null +++ b/src/com/jsyn/swing/XYController.java @@ -0,0 +1,132 @@ +/* + * Copyright 1997 Phil Burk, Mobileer Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jsyn.swing; + +import javax.swing.JPanel; + +/** + * Root class for 2 dimensional X,Y controller for wave editors, Theremins, etc. Maps pixel + * coordinates into "world" coordinates. + * + * @author (C) 1997 Phil Burk, SoftSynth.com + */ + +public class XYController extends JPanel { + double minWorldX = 0.0; + double maxWorldX = 1.0; + double minWorldY = 0.0; + double maxWorldY = 1.0; + + public XYController() { + } + + public XYController(double minWX, double minWY, double maxWX, double maxWY) { + setMinWorldX(minWX); + setMaxWorldX(maxWX); + setMinWorldY(minWY); + setMaxWorldY(maxWY); + } + + /** + * Set minimum World coordinate value for the horizontal X dimension. The minimum value + * corresponds to the left of the component. + */ + public void setMinWorldX(double minWX) { + minWorldX = minWX; + } + + public double getMinWorldX() { + return minWorldX; + } + + /** + * Set maximum World coordinate value for the horizontal X dimension. The minimum value + * corresponds to the right of the component. + */ + public void setMaxWorldX(double maxWX) { + maxWorldX = maxWX; + } + + public double getMaxWorldX() { + return maxWorldX; + } + + /** + * Set minimum World coordinate value for the vertical Y dimension. The minimum value + * corresponds to the bottom of the component. + */ + public void setMinWorldY(double minWY) { + minWorldY = minWY; + } + + public double getMinWorldY() { + return minWorldY; + } + + /** + * Set maximum World coordinate value for the vertical Y dimension. The maximum value + * corresponds to the top of the component. + */ + public void setMaxWorldY(double maxWY) { + maxWorldY = maxWY; + } + + public double getMaxWorldY() { + return maxWorldY; + } + + /** Convert from graphics coordinates (pixels) to world coordinates. */ + public double convertGXtoWX(int gx) { + int width = bounds().width; + return minWorldX + ((maxWorldX - minWorldX) * gx) / width; + } + + public double convertGYtoWY(int gy) { + int height = bounds().height; + return minWorldY + ((maxWorldY - minWorldY) * (height - gy)) / height; + } + + /** Convert from world coordinates to graphics coordinates (pixels). */ + public int convertWXtoGX(double wx) { + int width = bounds().width; + return (int) (((wx - minWorldX) * width) / (maxWorldX - minWorldX)); + } + + public int convertWYtoGY(double wy) { + int height = bounds().height; + return height - (int) (((wy - minWorldY) * height) / (maxWorldY - minWorldY)); + } + + /** Clip wx to the min & max World X values. */ + public double clipWorldX(double wx) { + if (wx < minWorldX) + wx = minWorldX; + else if (wx > maxWorldX) + wx = maxWorldX; + return wx; + } + + /** Clip wy to the min & max World Y values. */ + public double clipWorldY(double wy) { + if (wy < minWorldY) + wy = minWorldY; + else if (wy > maxWorldY) + wy = maxWorldY; + return wy; + } + +} |