📄 transformanim.java
字号:
Thread me = Thread.currentThread(); while (thread == me) { repaint(); try { thread.sleep(10); } catch (InterruptedException e) { break; } } thread = null; } /** * The TextData class creates an Object to store the * specified String and Font. */ static class TextData extends Object { public String string; public Font font; public TextData(String str, Font font) { string = str; this.font = font; } } /** * The ObjectData class calculates the transformations and generates * random coordinates for all objects and generates random sizes * for shapes. */ static class ObjectData extends Object { Object object; Paint paint; static final int UP = 0; static final int DOWN = 1; double x, y; double ix=5, iy=3; int rotate; double scale, shear; int scaleDirection, shearDirection; AffineTransform at = new AffineTransform(); // generates random transformation direction and factor public ObjectData(Object object, Paint paint) { this.object = object; this.paint = paint; rotate = (int)(Math.random() * 360); scale = Math.random() * 1.5; scaleDirection = Math.random() > 0.5 ? UP : DOWN; shear = Math.random() * 0.5; shearDirection = Math.random() > 0.5 ? UP : DOWN; } /* * generates random coordinate values for all objects * and generates random size values for shapes */ public void reset(int w, int h) { x = Math.random()*w; y = Math.random()*h; double ww = 20 + Math.random()*((w == 0 ? 400 : w)/4); double hh = 20 + Math.random()*((h == 0 ? 300 : h)/4); if (object instanceof Ellipse2D) { ((Ellipse2D) object).setFrame(0, 0, ww, hh); } else if (object instanceof Rectangle2D) { ((Rectangle2D) object).setRect(0, 0, ww, ww); } else if (object instanceof RoundRectangle2D) { ((RoundRectangle2D) object).setRoundRect(0, 0, hh, hh, 20, 20); } else if (object instanceof Arc2D) { ((Arc2D) object).setArc(0, 0, hh, hh, 45, 270, Arc2D.PIE); } else if (object instanceof QuadCurve2D) { ((QuadCurve2D) object).setCurve(0, 0, w*.2, h*.4, w*.4, 0); } else if (object instanceof CubicCurve2D) { ((CubicCurve2D) object).setCurve(0,0,30,-60,60,60,90,0); } else if (object instanceof GeneralPath) { GeneralPath p = new GeneralPath(); float size = (float) ww; p.moveTo(- size / 2.0f, - size / 8.0f); p.lineTo(+ size / 2.0f, - size / 8.0f); p.lineTo(- size / 4.0f, + size / 2.0f); p.lineTo(+ 0.0f, - size / 2.0f); p.lineTo(+ size / 4.0f, + size / 2.0f); p.closePath(); object = p; } } /* * calculates new transformation factors for the * chosen transformation and the current direction */ public void step(int w, int h, Demo demo) { at.setToIdentity(); if (demo.doRotate) { if ((rotate+=5) == 360) { rotate = 0; } at.rotate(Math.toRadians(rotate), x, y); } at.translate(x, y); if (demo.doTranslate) { x += ix; y += iy; if (x > w) { x = w - 1; ix = Math.random() * -w/32 - 1; } if (x < 0) { x = 2; ix = Math.random() * w/32 + 1; } if (y > h ) { y = h - 2; iy = Math.random() * -h/32 - 1; } if (y < 0) { y = 2; iy = Math.random() * h/32 + 1; } } if (demo.doScale && scaleDirection == UP) { if ((scale += 0.05) > 1.5) { scaleDirection = DOWN; } } else if (demo.doScale && scaleDirection == DOWN) { if ((scale -= .05) < 0.5) { scaleDirection = UP; } } if (demo.doScale) { at.scale(scale, scale); } if (demo.doShear && shearDirection == UP) { if ((shear += 0.05) > 0.5) { shearDirection = DOWN; } } else if (demo.doShear && shearDirection == DOWN) { if ((shear -= .05) < -0.5) { shearDirection = UP; } } if (demo.doShear) { at.shear(shear, shear); } } } // End ObjectData class } // End Demo class /** * The DemoControls class provides controls for selecting the amount * of Shapes, Strings or Images to display and selecting the * transformation to perform. */ static class DemoControls extends JPanel implements ActionListener, ChangeListener { Demo demo; JSlider shapeSlider, stringSlider, imageSlider; Font font = new Font("serif", Font.PLAIN, 10); JToolBar toolbar; public DemoControls(Demo demo) { this.demo = demo; setBackground(Color.gray); setLayout(new BorderLayout()); JToolBar bar = new JToolBar(JToolBar.VERTICAL); bar.setBackground(Color.gray); bar.setFloatable(false); shapeSlider = new JSlider(JSlider.HORIZONTAL,0,20,demo.numShapes); shapeSlider.addChangeListener(this); TitledBorder tb = new TitledBorder(new EtchedBorder()); tb.setTitleFont(font); tb.setTitle(String.valueOf(demo.numShapes) + " Shapes"); shapeSlider.setBorder(tb); shapeSlider.setPreferredSize(new Dimension(80,44)); bar.add(shapeSlider); bar.addSeparator(); stringSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numStrings); stringSlider.addChangeListener(this); tb = new TitledBorder(new EtchedBorder()); tb.setTitleFont(font); tb.setTitle(String.valueOf(demo.numStrings) + " Strings"); stringSlider.setBorder(tb); stringSlider.setPreferredSize(new Dimension(80,44)); bar.add(stringSlider); bar.addSeparator(); imageSlider = new JSlider(JSlider.HORIZONTAL,0,10,demo.numImages); imageSlider.addChangeListener(this); tb = new TitledBorder(new EtchedBorder()); tb.setTitleFont(font); tb.setTitle(String.valueOf(demo.numImages) + " Images"); imageSlider.setBorder(tb); imageSlider.setPreferredSize(new Dimension(80,44)); bar.add(imageSlider); bar.addSeparator(); toolbar = new JToolBar(); toolbar.setFloatable(false); addButton("T", "translate", demo.doTranslate); addButton("R", "rotate", demo.doRotate); addButton("SC", "scale", demo.doScale); addButton("SH", "shear", demo.doShear); bar.add(toolbar); add(bar); } public void addButton(String s, String tt, boolean state) { JButton b = (JButton) toolbar.add(new JButton(s)); b.setFont(font); b.setSelected(state); b.setToolTipText(tt); b.setBackground(state ? Color.green : Color.lightGray); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { JButton b = (JButton) e.getSource(); b.setSelected(!b.isSelected()); b.setBackground(b.isSelected() ? Color.green : Color.lightGray); if (b.getText().equals("T")) { demo.doTranslate = b.isSelected(); } else if (b.getText().equals("R")) { demo.doRotate = b.isSelected(); } else if (b.getText().equals("SC")) { demo.doScale = b.isSelected(); } else if (b.getText().equals("SH")) { demo.doShear = b.isSelected(); } } public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider) e.getSource(); int value = slider.getValue(); TitledBorder tb = (TitledBorder) slider.getBorder(); if (slider.equals(shapeSlider)) { tb.setTitle(String.valueOf(value) + " Shapes"); demo.setShapes(value); } else if (slider.equals(stringSlider)) { tb.setTitle(String.valueOf(value) + " Strings"); demo.setStrings(value); } else if (slider.equals(imageSlider)) { tb.setTitle(String.valueOf(value) + " Images"); demo.setImages(value); } } } // End DemoControls class public static void main(String argv[]) { final TransformAnim demo = new TransformAnim(); demo.init(); JFrame f = new JFrame("Java 2D(TM) Demo - TransformAnim"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }); f.getContentPane().add("Center", demo); f.pack(); f.setSize(new Dimension(550,360)); f.show(); demo.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -