Make first real commit: copy of CaRMetal 4.2.8

This commit is contained in:
Glen Whitney 2018-09-04 22:51:42 -04:00
parent 002acfc88e
commit c312811084
1120 changed files with 226843 additions and 1 deletions

View file

@ -0,0 +1,246 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.animations;
import rene.zirkel.ZirkelCanvas;
import rene.zirkel.objects.ConstructionObject;
import rene.zirkel.objects.ExpressionObject;
import rene.zirkel.objects.PointObject;
import rene.zirkel.objects.PrimitiveCircleObject;
import rene.zirkel.objects.SegmentObject;
/**
*
* @author erichake
*/
public class Animation {
private ZirkelCanvas ZC;
private ConstructionObject O=null;
private String ObjectName;
private AnimationPanel PANEL;
private boolean Negative=false;
private Thread TH=null;
private boolean thread_alive=true;
private double min, max, positiveincrement, parts=100;//For slider Expressions
public Animation(ZirkelCanvas zc, AnimationPanel panel, ConstructionObject o) {
ZC=zc;
O=o;
PANEL=panel;
ObjectName=O.getName();
}
public Animation(ZirkelCanvas zc, AnimationPanel panel, String objectname) {
ZC=zc;
ObjectName=objectname;
PANEL=panel;
}
public boolean getNegative() {
return Negative;
}
public void setNegative(boolean neg) {
Negative=neg;
}
public String getObjectName() {
return ObjectName;
}
public ConstructionObject getObject() {
if (O==null) {
O=ZC.getConstruction().find(ObjectName);
}
return O;
}
public void killThread() {
thread_alive=false;
}
public void run() {
if (TH!=null) {
return;
}
O=getObject();
if (O==null) {
return;
}
if (O instanceof PointObject) {
runPoint();
} else if (O instanceof ExpressionObject) {
runExpression();
}
}
public void runPoint() {
TH=new Thread() {
public void run() {
boolean back=false;
double x=0;
final ZirkelCanvas canvas=ZC;
PointObject P=(PointObject) O;
double x0=P.getX();
double y0=P.getY();
if (P.getBound() instanceof SegmentObject) {
SegmentObject s=(SegmentObject) P.getBound();
x=Math.sqrt((x0-s.getP1().getX())*(x0-s.getP1().getX())+(y0-s.getP1().getY())*(y0-s.getP1().getY()));
}else if (P.getBound() instanceof PrimitiveCircleObject) {
PrimitiveCircleObject c=(PrimitiveCircleObject) P.getBound();
x=Math.acos((x0-c.getX())/c.getR())*Math.signum(y0-c.getY());
}
if (P.getBound()==null) {
return;
}
if (!(P.getBound() instanceof SegmentObject)&&!(P.getBound() instanceof PrimitiveCircleObject)) {
return;
}
long time=System.currentTimeMillis();
while ((!PANEL.isStopped())&&(thread_alive)) {
try {
final long t=System.currentTimeMillis();
double d=PANEL.getDelay();
int h=(int) (t-time);
if (h<0) {
h=0;
}
if (h>d) {
h=(int) d;
}
Thread.sleep((int) (d-h));
time=System.currentTimeMillis();
} catch (final Exception ex) {
}
if (P.getBound() instanceof SegmentObject) {
final SegmentObject s=(SegmentObject) P.getBound();
synchronized (canvas) {
if (back) {
O.move(s.getP1().getX()+(s.getLength()-x)
*s.getDX(), s.getP1().getY()
+(s.getLength()-x)*s.getDY());
} else {
O.move(s.getP1().getX()+x*s.getDX(), s.getP1().getY()
+x*s.getDY());
}
ZC.dovalidate();
ZC.repaint();
x+=ZC.dx(2);
if (x>s.getLength()) {
back=!back;
x=0;
}
}
} else if (P.getBound() instanceof PrimitiveCircleObject) {
final PrimitiveCircleObject c=(PrimitiveCircleObject) P.getBound();
synchronized (canvas) {
O.move(c.getP1().getX()+Math.cos(x)
*c.getR(), c.getP1().getY()
+Math.sin(x)*c.getR());
ZC.dovalidate();
ZC.repaint();
if (Negative) {
x-=ZC.dx(2)/c.getR();
} else {
x+=ZC.dx(2)/c.getR();
}
}
}
}
TH=null;
}
};
TH.setPriority(Thread.MIN_PRIORITY);
TH.start();
}
public void initSlidersParameters(ExpressionObject E) {
min=E.getMinValue();
max=E.getMaxValue();
positiveincrement=(max-min)/parts;
}
public void runExpression() {
TH=new Thread() {
public void run() {
final ZirkelCanvas canvas=ZC;
ExpressionObject E=(ExpressionObject) O;
double x;
try {
x = E.getValue();
} catch (Exception ex) {
x=0;
}
double increment=1;
long time=System.currentTimeMillis();
while ((!PANEL.isStopped())&&(thread_alive)) {
try {
final long t=System.currentTimeMillis();
double d=PANEL.getDelay();
int h=(int) (t-time);
if (h<0) {
h=0;
}
if (h>d) {
h=(int) d;
}
Thread.sleep((int) (d-h));
time=System.currentTimeMillis();
} catch (final Exception ex) {
}
try {
synchronized (canvas) {
if (E.isSlider()) {
initSlidersParameters(E);
if (Negative) {
if (x>max) {
x=min;
} else {
x+=positiveincrement;
}
} else {
if (x>max) {
x=max;
increment=-positiveincrement;
} else if (x<min) {
x=min;
increment=positiveincrement;
} else {
x+=Math.signum(increment)*positiveincrement;
}
}
} else {
x+=Negative?-increment:increment;
}
E.setExpression(""+x, E.getConstruction());
// E.setSlider(false);
ZC.dovalidate();
ZC.repaint();
}
} catch (Exception ex) {
}
}
TH=null;
}
};
TH.setPriority(Thread.MIN_PRIORITY);
TH.start();
}
}

View file

@ -0,0 +1,313 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package eric.animations;
import eric.GUI.themes;
import eric.GUI.window.myJMenuItem;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import eric.JEricPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import rene.gui.Global;
import rene.util.xml.XmlWriter;
import rene.zirkel.ZirkelCanvas;
import rene.zirkel.objects.ConstructionObject;
import rene.zirkel.objects.ExpressionObject;
import rene.zirkel.objects.PointObject;
/**
*
* @author erichake
*/
public class AnimationPanel extends JEricPanel implements MouseListener {
private ZirkelCanvas ZC;
private Vector<Animation> V=new Vector<Animation>();
private static Image icon=themes.getImage("animation.png");
private int W=32, H=32, X=10, Y=47;
private int minspeed=0;
private int maxspeed=100;
private int speed=40;
private boolean stopped=false;
private double a, b, A1, A2, B2;
public void paintComponent(Graphics g) {
g.drawImage(icon, 0, 0, W, H, this);
}
public int getMaxSpeed() {
return maxspeed;
}
public AnimationPanel(ZirkelCanvas zc) {
ZC=zc;
setBounds(X, Y, W, H);
addMouseListener(this);
a=maxspeed/10;
b=maxspeed-10;
A1=b/a;
A2=(maxspeed-b)/(maxspeed-a);
B2=maxspeed*(b-a)/(maxspeed-a);
}
public double getDelay() {
int cur=speed;
double v;
if (cur>a) {
v=A2*cur+B2;
} else {
v=A1*cur;
}
double d=(maxspeed-v)*6;
return d;
}
public void setDelay(double delay) {
double cur=maxspeed-delay/6;
if (cur>b) {
speed=(int) ((cur-B2)/A2);
} else {
speed=(int) (cur/A1);
}
}
public void showPopup() {
JPopupMenu popup=new JPopupMenu();
// add Cancel Item :
myJMenuItem item=new myJMenuItem(Global.Loc("animation.run")) {
@Override
public void action() {
startAnimation();
}
};
popup.add(item);
item=new myJMenuItem(Global.Loc("animation.stop")) {
@Override
public void action() {
stopAnimation();
}
};
popup.add(item);
item=new myJMenuItem(Global.Loc("animation.reverse")) {
@Override
public void action() {
reverseAnimation();
}
};
popup.add(item);
popup.add(new JSeparator());
item=new myJMenuItem(Global.Loc("animation.removeall")) {
@Override
public void action() {
removeAllAnimations();
}
};
popup.add(item);
popup.add(new JSeparator());
popup.add(new SpeedSliderMenuItem());
popup.show(this, W-10, H);
}
public Vector<Animation> getAnimations() {
return V;
}
public void reverseAnimation() {
for (int i=0; i<V.size(); i++) {
V.get(i).setNegative(!V.get(i).getNegative());
}
}
public void setAnimationNegative(String objectname, boolean negative) {
for (int i=0; i<V.size(); i++) {
if (objectname.equals(V.get(i).getObjectName())) {
V.get(i).setNegative(negative);
return;
}
}
}
public boolean isStopped() {
return stopped;
}
public void stopAnimation() {
stopped=true;
}
public void startAnimation() {
stopped=false;
run();
}
public void run() {
if (!stopped) {
for (int i=0; i<V.size(); i++) {
V.get(i).run();
}
}
}
public boolean isAnimated(ConstructionObject o) {
for (int i=0; i<V.size(); i++) {
if (V.get(i).getObject().equalsTo(o)) {
return true;
}
}
return false;
}
public void removeAllAnimations() {
for (int i=0; i<V.size(); i++) {
V.get(i).killThread();
// V.remove(V.get(i));
}
V.removeAllElements();
ZC.remove(this);
ZC.repaint();
}
public void removeAnimation(ConstructionObject o) {
for (int i=0; i<V.size(); i++) {
if (o.equalsTo(V.get(i).getObject())) {
V.get(i).killThread();
V.remove(V.get(i));
}
}
if (V.size()==0) {
ZC.remove(this);
ZC.repaint();
}
}
// Added with the animation tool :
public void addAnimation(ConstructionObject o) {
if (o==null) {
return;
}
Animation anim=new Animation(ZC, this, o);
V.add(anim);
if (V.size()==1) {
ZC.add(this);
ZC.repaint();
}
if (!stopped) {
anim.run();
}
}
// Only for file loading :
public void addAnimation(String objectname) {
V.add(new Animation(ZC, this, objectname));
if (V.size()==1) {
ZC.add(this);
ZC.repaint();
}
}
public void setObjectSelected(boolean sel) {
for (int i=0; i<V.size(); i++) {
V.get(i).getObject().setSelected(sel);
}
}
public void printArgs(final XmlWriter xml) {
for (int i=0; i<V.size(); i++) {
ConstructionObject o=V.get(i).getObject();
if ((o!=null)&&(o instanceof PointObject)) {
PointObject p=(PointObject) o;
if ((p!=null)&&(p.isPointOn())&&(p.isInConstruction())) {
xml.startTagStart("Animate");
xml.printArg("animate", p.getName());
xml.printArg("negative", ""+V.get(i).getNegative());
xml.printArg("delay", ""+getDelay());
xml.printArg("stopped", ""+stopped);
xml.finishTagNewLine();
}
} else if ((o!=null)&&(o instanceof ExpressionObject)) {
xml.startTagStart("Animate");
xml.printArg("animate", o.getName());
xml.printArg("negative", ""+V.get(i).getNegative());
xml.printArg("delay", ""+getDelay());
xml.printArg("stopped", ""+stopped);
xml.finishTagNewLine();
}
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
showPopup();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
class SpeedSliderMenuItem extends JSlider implements ChangeListener {
TitledBorder ttle;
public SpeedSliderMenuItem() {
ttle=new TitledBorder(" "+Global.Loc("animation.speed")+"="+speed+"% ");
ttle.setTitleFont(themes.TabMenusFont);
setBorder(ttle);
setMinimum(minspeed);
setMaximum(maxspeed);
setValue(speed);
setMajorTickSpacing(20);
setMinorTickSpacing(10);
setFocusable(false);
addChangeListener(this);
putClientProperty("JComponent.sizeVariant", "small");
// http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/size.html
}
public void stateChanged(ChangeEvent e) {
// System.out.println("vitesse="+getValue());
speed=getValue();
ttle.setTitle(" "+Global.Loc("animation.speed")+"="+speed+"% ");
repaint();
}
}
}