|
| 1 | +package com.android; |
| 2 | + |
| 3 | +import com.android.CycleView.Model; |
| 4 | +import com.android.CycleView.Prop; |
| 5 | +import java.awt.BasicStroke; |
| 6 | +import java.awt.Color; |
| 7 | +import java.awt.FlowLayout; |
| 8 | +import java.awt.Graphics; |
| 9 | +import java.awt.Graphics2D; |
| 10 | +import java.awt.Stroke; |
| 11 | +import java.awt.geom.AffineTransform; |
| 12 | +import javax.swing.JButton; |
| 13 | +import javax.swing.JPanel; |
| 14 | +import javax.swing.Timer; |
| 15 | + |
| 16 | +class AnimationPanel extends JPanel { |
| 17 | + |
| 18 | + private float mDuration = 20000; // duration in milliseconds |
| 19 | + private float myAnimationPercent; |
| 20 | + private long myLastTime; |
| 21 | + private static final int BUTTON_WIDTH = 123; |
| 22 | + private static final int BUTTON_HEIGHT = 40; |
| 23 | + private String myTitle = "button"; |
| 24 | + private JButton myPlayButton; |
| 25 | + private boolean myIsPlaying = false; |
| 26 | + private Timer myPlayTimer = new Timer(14, e -> step()); |
| 27 | + private Model myModel; |
| 28 | + private int myAttributeType; |
| 29 | + private Prop myAttribute; |
| 30 | + private float myCurrentValue; |
| 31 | + static final Stroke DASH_STROKE = new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,1,new float[]{10,10},0); |
| 32 | + AnimationPanel(Model model, JButton play) { |
| 33 | + super(new FlowLayout(FlowLayout.LEFT)); |
| 34 | + myModel = model; |
| 35 | + myPlayButton = play; |
| 36 | + myPlayButton.addActionListener(e -> play()); |
| 37 | + } |
| 38 | + |
| 39 | + public void setModel(Model model) { |
| 40 | + myModel = model; |
| 41 | + } |
| 42 | + |
| 43 | + public void setMode() { |
| 44 | + System.out.println("update attribute_type "); |
| 45 | + myAttribute = Prop.values()[myAttributeType = myModel.mAttrIndex]; |
| 46 | + } |
| 47 | + |
| 48 | + public void play() { |
| 49 | + if (myIsPlaying) { |
| 50 | + myModel.setDot(Float.NaN, 0); |
| 51 | + pause(); |
| 52 | + myIsPlaying = false; |
| 53 | + return; |
| 54 | + } |
| 55 | + myPlayButton.setText("pause"); |
| 56 | + myIsPlaying = true; |
| 57 | + myPlayTimer.start(); |
| 58 | + myAttributeType = myModel.mAttrIndex; |
| 59 | + myAttribute = Prop.values()[myAttributeType]; |
| 60 | + System.out.println("prop = " + myAttribute.name()); |
| 61 | + } |
| 62 | + |
| 63 | + int call_count = 0; |
| 64 | + int paint_count = 0; |
| 65 | + long last_update; |
| 66 | + |
| 67 | + public void step() { |
| 68 | + long time = System.currentTimeMillis(); |
| 69 | + myAnimationPercent += (time - myLastTime) / mDuration; |
| 70 | + if (myAnimationPercent > 1.0f) { |
| 71 | + myAnimationPercent = 0; |
| 72 | + } |
| 73 | + |
| 74 | + myCurrentValue = myModel.getComputedValue(myAnimationPercent); |
| 75 | + myModel.setDot(myAnimationPercent, myCurrentValue); |
| 76 | + call_count++; |
| 77 | + repaint(); |
| 78 | + myLastTime = time; |
| 79 | + } |
| 80 | + |
| 81 | + public void pause() { |
| 82 | + myPlayButton.setText("play"); |
| 83 | + myPlayTimer.stop(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void paint(Graphics g) { |
| 88 | + super.paint(g); |
| 89 | + paint_count++; |
| 90 | + long time = System.currentTimeMillis(); |
| 91 | + int w = getWidth(); |
| 92 | + int h = getHeight(); |
| 93 | + float buttonCX = w / 2.0f; |
| 94 | + float buttonCY = h / 2.0f; |
| 95 | + float startX = buttonCX; |
| 96 | + float startY = buttonCY; |
| 97 | + float endX = buttonCX; |
| 98 | + float endY = buttonCY; |
| 99 | + if (myMoveObject != 0) { |
| 100 | + float dx = movement[myMoveObject][0]; |
| 101 | + float dy = movement[myMoveObject][1]; |
| 102 | + startX = buttonCX - (dx * w)/3.0f; |
| 103 | + startY = buttonCY - (dy * h)/3.0f; |
| 104 | + endX = buttonCX + (dx * w)/3.0f; |
| 105 | + endY = buttonCY + (dy * h)/3.0f; |
| 106 | + buttonCX = startX+ myAnimationPercent *(endX-startX); |
| 107 | + buttonCY = startY+ myAnimationPercent *(endY-startY); |
| 108 | + |
| 109 | + } |
| 110 | + Graphics2D g2d = (Graphics2D) g.create(); |
| 111 | + AffineTransform at; |
| 112 | + Stroke old = g2d.getStroke(); |
| 113 | + g2d.setColor(Color.RED); |
| 114 | + g2d.setStroke(DASH_STROKE); |
| 115 | + g2d.drawLine((int)startX,(int)startY,(int)endX,(int)endY); |
| 116 | + g2d.setStroke(old); |
| 117 | + Color background = Color.LIGHT_GRAY; |
| 118 | + Color border = Color.DARK_GRAY; |
| 119 | + Color text = Color.BLACK; |
| 120 | + if (myAttribute != null) { |
| 121 | + switch (myAttribute) { |
| 122 | + |
| 123 | + case PATH_ROTATE: |
| 124 | + at = new AffineTransform(); |
| 125 | + at.rotate(Math.toRadians(myCurrentValue), buttonCX, buttonCY); |
| 126 | + g2d.transform(at); |
| 127 | + break; |
| 128 | + case ALPHA: |
| 129 | + int alpha = Math.max(0, Math.min(255, (int) (myCurrentValue * 255))); |
| 130 | + background = new Color(background.getRed(), background.getGreen(), background.getBlue(), |
| 131 | + alpha); |
| 132 | + border = new Color(border.getRed(), border.getGreen(), border.getBlue(), alpha); |
| 133 | + text = new Color(text.getRed(), text.getGreen(), text.getBlue(), alpha); |
| 134 | + break; |
| 135 | + case ELEVATION: |
| 136 | + break; |
| 137 | + case ROTATION: |
| 138 | + at = new AffineTransform(); |
| 139 | + at.rotate(Math.toRadians(myCurrentValue), buttonCX, buttonCY); |
| 140 | + g2d.transform(at); |
| 141 | + break; |
| 142 | + case ROTATION_X: |
| 143 | + break; |
| 144 | + case ROTATION_Y: |
| 145 | + break; |
| 146 | + case SCALE_X: |
| 147 | + at = new AffineTransform(); |
| 148 | + at.translate(w / 2.0, buttonCY); |
| 149 | + at.scale(myCurrentValue, 1); |
| 150 | + at.translate(-w / 2.0, -buttonCY); |
| 151 | + |
| 152 | + g2d.transform(at); |
| 153 | + break; |
| 154 | + case SCALE_Y: |
| 155 | + at = new AffineTransform(); |
| 156 | + at.translate(buttonCX, buttonCY); |
| 157 | + at.scale(1, myCurrentValue); |
| 158 | + at.translate(-buttonCX, -buttonCY); |
| 159 | + g2d.transform(at); |
| 160 | + break; |
| 161 | + case TRANSLATION_X: |
| 162 | + at = new AffineTransform(); |
| 163 | + at.translate(myCurrentValue, 0); |
| 164 | + g2d.transform(at); |
| 165 | + break; |
| 166 | + case TRANSLATION_Y: |
| 167 | + at = new AffineTransform(); |
| 168 | + at.translate(0, myCurrentValue); |
| 169 | + g2d.transform(at); |
| 170 | + break; |
| 171 | + case TRANSLATION_Z: |
| 172 | + break; |
| 173 | + case PROGRESS: |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + int px = (int)(0.5+buttonCX - BUTTON_WIDTH / 2.0f); |
| 179 | + int py = (int)(0.5+buttonCY - BUTTON_HEIGHT / 2.0f); |
| 180 | + |
| 181 | + g2d.setColor(background); |
| 182 | + g2d.fillRoundRect(px, py, BUTTON_WIDTH, BUTTON_HEIGHT, 5, 5); |
| 183 | + g2d.setColor(border); |
| 184 | + g2d.drawRoundRect(px, py, BUTTON_WIDTH, BUTTON_HEIGHT, 5, 5); |
| 185 | + int sw = g.getFontMetrics().stringWidth(myTitle); |
| 186 | + int fh = g.getFontMetrics().getHeight(); |
| 187 | + int fa = g.getFontMetrics().getAscent(); |
| 188 | + g2d.setColor(text); |
| 189 | + g2d.drawString(myTitle, px + BUTTON_WIDTH / 2 - sw / 2, py + BUTTON_HEIGHT / 2 + fa - fh / 2); |
| 190 | + if (time - last_update > 1000) { |
| 191 | + System.out.println("1 > " + 1000 * call_count / (float) (time - last_update) + " paint " |
| 192 | + + 1000 * paint_count / (float) (time - last_update)); |
| 193 | + last_update = time; |
| 194 | + paint_count = 0; |
| 195 | + call_count = 0; |
| 196 | + } |
| 197 | + } |
| 198 | + static String []MOVE_NAMES = {"Stationary","South to North","West to East","SW to NE","NW to SE", "SE to NW", "NE to SW"}; |
| 199 | + static String []DURATION = {"0.1s","0.5s","1.0s","2.0s", "5s", "10s","20s"}; |
| 200 | + static int []DURATION_VAL = {100,500,1000,2000, 5000, 10000,20000}; |
| 201 | + |
| 202 | + static int [][] movement = { |
| 203 | + {0,0}, |
| 204 | + {0,-1}, |
| 205 | + {1,0}, |
| 206 | + {1,-1}, |
| 207 | + {1,1}, |
| 208 | + {-1,-1}, |
| 209 | + {-1,1}, |
| 210 | + }; |
| 211 | + int myMoveObject = 0; |
| 212 | + public void setMovement(int move) { |
| 213 | + myMoveObject = move; |
| 214 | + repaint(); |
| 215 | + } |
| 216 | + |
| 217 | + public void setDurationIndex(int selectedIndex) { |
| 218 | + mDuration = DURATION_VAL[selectedIndex]; |
| 219 | + } |
| 220 | +} |
0 commit comments