📄 fadeanim.java
字号:
} } public static void main(String argv[]) { final DemoPanel dp = new DemoPanel(new FadeAnim()); Frame f = new Frame("Java2D Demo - FadeAnim"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { dp.surface.start(); } public void windowIconified(WindowEvent e) { dp.surface.stop(); } }); f.add("Center", dp); f.pack(); f.setSize(new Dimension(400,300)); f.show(); dp.surface.start(); } static class TextData extends Object { public String string; public Font font; public int width, height; public TextData(String str, Font font, Component cmp) { string = str; this.font = font; FontMetrics fm = cmp.getFontMetrics(font); width = fm.stringWidth(str); height = fm.getHeight(); } } static class ObjectData extends Object { final int UP = 0; final int DOWN = 1; Object object; BufferedImage bimg; Paint paint; double x, y; float alpha; int alphaDirection; int imgX; public ObjectData(Object object, Paint paint) { this.object = object; this.paint = paint; if (object instanceof BufferedImage) { bimg = (BufferedImage) object; this.object = bimg.getSubimage(0, 0, 80, 80); } getRandomXY(300, 250); alpha = (float) Math.random(); alphaDirection = Math.random() > 0.5 ? UP : DOWN; } private void getRandomXY(int w, int h) { if (object instanceof TextData) { x = Math.random() * (w - ((TextData) object).width); y = Math.random() * h; y = y < ((TextData) object).height ? ((TextData) object).height : y; } else if (object instanceof Image) { x = Math.random() * (w - ((Image) object).getWidth(null)); y = Math.random() * (h - ((Image) object).getHeight(null)); } else if (object instanceof Shape) { Rectangle bounds = ((Shape) object).getBounds(); x = Math.random() * (w - bounds.width); y = Math.random() * (h - bounds.height); } } public void reset(int w, int h) { getRandomXY(w, 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; } } public void step(int w, int h) { if (object instanceof BufferedImage) { if ((imgX += 80) == 800) { imgX = 0; } object = bimg.getSubimage(imgX, 0, 80, 80); } if (alphaDirection == UP) { if ((alpha += 0.05) > .99) { alphaDirection = DOWN; alpha = 1.0f; } } else if (alphaDirection == DOWN) { if ((alpha -= .05) < 0.01) { alphaDirection = UP; alpha = 0; getRandomXY(w, h); } } } } static class DemoControls extends JPanel implements ChangeListener, Runnable { FadeAnim demo; JSlider shapeSlider, stringSlider, imageSlider; Thread thread; Font font = new Font("serif", Font.PLAIN, 10); public DemoControls(FadeAnim demo) { this.demo = demo; setBackground(Color.gray); setLayout(new BorderLayout()); JToolBar toolbar = new JToolBar(JToolBar.VERTICAL); toolbar.setBackground(Color.gray); toolbar.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,45)); toolbar.addSeparator(); toolbar.add(shapeSlider); toolbar.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,45)); toolbar.add(stringSlider); toolbar.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,45)); toolbar.add(imageSlider); toolbar.addSeparator(); add(toolbar); toolbar.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); } 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); } slider.repaint(); if (demo.thread == null) { demo.repaint(); } } public Dimension getPreferredSize() { return new Dimension(80,0); } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Composite.FadeAnim DemoControls Thread"); thread.start(); } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null; notifyAll(); } public void run() { try { thread.sleep(999); } catch (InterruptedException e) { return; } shapeSlider.setValue((int) (Math.random() * 5)); stringSlider.setValue(10); thread = null; } } // End DemoControls} // End FadeAnim
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -