📄 textureanim.java
字号:
f.pack(); f.setSize(new Dimension(400,300)); f.show(); dp.surface.start(); } static class AnimVal { float curval; float lowval; float highval; float currate; float lowrate; float highrate; public AnimVal(int lowval, int highval, int lowrate, int highrate) { this.lowval = lowval; this.highval = highval; this.lowrate = lowrate; this.highrate = highrate; this.curval = randval(lowval, highval); this.currate = randval(lowrate, highrate); } public AnimVal(int lowval, int highval, int lowrate, int highrate, int pos) { this(lowval, highval, lowrate, highrate); set(pos); } public float randval(float low, float high) { return (float) (low + Math.random() * (high - low)); } public float getFlt() { return curval; } public int getInt() { return (int) curval; } public void anim() { curval += currate; clip(); } public void set(float val) { curval = val; clip(); } public void clip() { if (curval > highval) { curval = highval - (curval - highval); if (curval < lowval) { curval = highval; } currate = - randval(lowrate, highrate); } else if (curval < lowval) { curval = lowval + (lowval - curval); if (curval > highval) { curval = lowval; } currate = randval(lowrate, highrate); } } public void newlimits(int lowval, int highval) { this.lowval = lowval; this.highval = highval; clip(); } } // End AnimVal class static class DemoControls extends JPanel implements ActionListener, Runnable { TextureAnim demo; JToolBar toolbar; JComboBox combo; Thread thread; JMenu menu; JMenuItem menuitems[]; int iconSize = 20; public DemoControls(TextureAnim demo) { this.demo = demo; menuitems = new JMenuItem[2]; setBackground(Color.gray); add(toolbar = new JToolBar()); toolbar.setFloatable(false); addTool("BO", "bounce", true); addTool("SA", "show anchor", true); addTool("RS", "resize", false); addTool("RO", "rotate", false); addTool("SX", "shear x", false); addTool("SY", "shear y", false); add(combo = new JComboBox()); combo.addActionListener(this); combo.addItem("8"); combo.addItem("16"); combo.addItem("32"); combo.addItem("64"); combo.addItem("80"); combo.setSelectedIndex(2); JMenuBar menuBar = new JMenuBar(); menu = (JMenu) menuBar.add(new JMenu()); for (int i = 0; i < menuitems.length; i++) { BufferedImage bimg = demo.makeImage(iconSize, i); TexturedIcon icon = new TexturedIcon(bimg); menuitems[i] = menu.add(new JMenuItem(icon)); menuitems[i].addActionListener(this); } menu.setIcon(menuitems[0].getIcon()); add(menuBar); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); demo.bNum = 0; } public void addTool(String str, String toolTip, boolean state) { JButton b = (JButton) toolbar.add(new JButton(str)); b.setBackground(state ? Color.green : Color.lightGray); b.setSelected(state); b.setToolTipText(toolTip); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj instanceof JComboBox) { int size = Integer.parseInt((String) combo.getSelectedItem()); demo.textureImg = demo.makeImage(size, demo.bNum); } else if (obj instanceof JMenuItem) { for (int i = 0; i < menuitems.length; i++) { if (obj.equals(menuitems[i])) { demo.textureImg = demo.makeImage(demo.tilesize, i); menu.setIcon(menuitems[i].getIcon()); break; } } } else { JButton b = (JButton) obj; b.setSelected(!b.isSelected()); b.setBackground(b.isSelected() ? Color.green : Color.lightGray); if (b.getText().equals("BO")) { demo.bouncerect = b.isSelected(); } else if (b.getText().equals("SA")) { demo.showanchor = b.isSelected(); } else if (b.getText().equals("RS")) { demo.bouncesize = b.isSelected(); } else if (b.getText().equals("RO")) { demo.rotate = b.isSelected(); } else if (b.getText().equals("SX")) { demo.shearx = b.isSelected(); } else if (b.getText().equals("SY")) { demo.sheary = b.isSelected(); } } if (demo.thread == null) { demo.repaint(); } } public Dimension getPreferredSize() { return new Dimension(200,37); } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Paint.TextureAnim DemoControls Thread"); thread.start(); } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null; notifyAll(); } public void run() { Thread me = Thread.currentThread(); while (thread == me) { for (int i = 2; i < toolbar.getComponentCount(); i++) { try { thread.sleep(4444); } catch (InterruptedException e) { return; } ((JButton) toolbar.getComponentAtIndex(i)).doClick(); } } thread = null; } class TexturedIcon implements Icon { BufferedImage bi; public TexturedIcon(BufferedImage bi) { this.bi = bi; } public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; Rectangle r = new Rectangle(x, y, iconSize, iconSize); g2.setPaint(new TexturePaint(bi, r)); g2.fillRect(x, y, iconSize, iconSize); g2.setColor(Color.gray); g2.draw3DRect(x, y, iconSize-1, iconSize-1, true); } public int getIconWidth() { return iconSize; } public int getIconHeight() { return iconSize; } } // End TexturedIcon class } // End DemoControls class} // End TextureAnim class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -