📄 main.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package graphic;/** * * @author spider */import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;import javax.swing.event.*; public class Main extends JComponent { public static void main(String[] args) { JFrame f = new JFrame("QuadVadis"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Main app = (Main) f.getContentPane().add(new Main()); JPanel ui = new JPanel(); f.getContentPane().add(ui, BorderLayout.SOUTH); final JLabel label = (JLabel) ui.add(new JLabel("N="+app.N+":")); final JSlider size = (JSlider) ui.add(new JSlider(8,100,app.N)); size.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) { if (!size.getValueIsAdjusting()) { app.N = size.getValue(); app.repaint(); } label.setText("N="+size.getValue()+":"); } }); final JCheckBox samples = (JCheckBox) ui.add(new JCheckBox("samples", true)); final JCheckBox tangents = (JCheckBox) ui.add(new JCheckBox("tangents", true)); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent evt) { app.showTangents = tangents.isSelected(); app.showSamples = samples.isSelected(); app.repaint(); } }; samples.addActionListener(al); tangents.addActionListener(al); f.setSize(450,450); f.setLocationRelativeTo(null); f.setVisible(true); } int N = 32; GeneralPath path=new GeneralPath(), tangents=new GeneralPath(), samples=new GeneralPath(); boolean showSamples=true, showTangents=true; float x(float t) {return (float) Math.cos(3.0*t);} float y(float t) {return (float) Math.sin(5.0*t);} float dx(float t) {return (float) (-3.0 * Math.sin(3.0*t));} float dy(float t) {return (float) (5.0* Math.cos(5.0*t));} void calculate() { float x1=x(0), y1=y(0), dx1=dx(0), dy1=dy(0), side=0.05f, t, x2, y2, dx2, dy2; path.reset(); tangents.reset(); samples.reset(); path.moveTo(x1, y1); tangents.moveTo(x1, y1); for(int i=0; i<N; ++i, x1=x2, y1=y2, dx1=dx2, dy1=dy2) { t=(i+1)*2*(float)Math.PI/N; x2=x(t); y2=y(t); dx2=dx(t); dy2=dy(t); samples.moveTo(x2-side/2, y2); samples.lineTo(x2+side/2,y2); samples.moveTo(x2, y2-side/2); samples.lineTo(x2,y2+side/2); float desc = dx1*dy2 - dx2*dy1; if (Math.abs(desc) < 0.1f) { path.lineTo(x2, y2); tangents.moveTo(x2, y2); } else { float ctrlx = (x2*dx1*dy2 - x1*dx2*dy1 + dx1*dx2*y1 - dx1*dx2*y2)/desc; float ctrly = (x2*dy1*dy2 - x1*dy1*dy2 + dx1*y1*dy2 - dx2*y2*dy1)/desc; path.quadTo(ctrlx, ctrly, x2, y2); tangents.lineTo(ctrlx, ctrly); tangents.lineTo(x2, y2); } } } protected void paintComponent(Graphics g) { calculate(); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); float scale = 0.45f * Math.min(getWidth(),getHeight()); AffineTransform xform = new AffineTransform(scale,0,0,scale,getWidth()/2,getHeight()/2); g2.setPaint(Color.RED); if (showTangents) g2.draw(tangents.createTransformedShape(xform)); g2.setPaint(Color.BLUE); g2.setStroke(new BasicStroke(2)); g2.draw(path.createTransformedShape(xform)); g2.setPaint(Color.BLACK); if (showSamples) g2.draw(samples.createTransformedShape(xform)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -