java画三角函数

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Paint {

	public static void main(String[] args) {

		JFrame f = new PaintFrame();
		f.setVisible(true);
	}
}

class PaintovalPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9125164341994818026L;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.green);
		g.fillOval(150, 150, 200, 200);

	}
}

class GJpanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int w;
	private int h;

	public GJpanel() {
	}

	public void paintComponent(final Graphics g) {

		w = getWidth();
		h = getHeight();

		g.drawLine(0, 0, 0, getHeight());
		g.setColor(Color.black);
		g.drawLine(0, h / 2, w, h / 2); // x
		g.drawLine(w, h / 2, w - 10, h / 2 - 10);
		g.drawLine(w, h / 2, w - 10, h / 2 + 10);

		g.drawLine(w / 2, 0, w / 2, h); // y
		g.drawLine(w / 2, 0, w / 2 - 10, 10);
		g.drawLine(w / 2, 0, w / 2 + 10, 10);

		g.drawString("Y", w / 2 - 20, 20);
		g.drawString("X", w - 20, h / 2 + 20);

		g.setColor(Color.black);
		for (int x = 0; x < w; x++) {
			int y = (int) (Math.cos(x * Math.PI / 180) * h / 3);
			g.drawString("·", x, h / 2 - y);
		}
	}
}

class PaintFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6729853332061293627L;

	Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
	int screen_width = (int) screensize.getWidth();
	int screen_height = (int) screensize.getHeight();
	int width = 800;
	int height = 600;

	public PaintFrame() {
		setTitle("Java画板");
		setSize(width, height);
		setLocation((screen_width - width) / 2, (screen_height - height) / 2);
		addWindowListener(new WindowAdapter() {
			public void WindowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		Container contentPane = getContentPane();
		// contentPane.add(new PaintovalPane());
		contentPane.add(new GJpanel());
	}
}

猜你喜欢

转载自xiongjiajia.iteye.com/blog/2320373