Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Liang listing 14.3
- import javax.swing.*;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.geom.Line2D;
- public class TestPanelDrawing extends JFrame {
- public TestPanelDrawing() {
- add(new NewPanel());
- }
- public static void main(final String[] args) {
- final TestPanelDrawing frame = new TestPanelDrawing();
- frame.setTitle("TestPanelDrawing");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(800, 800);
- frame.setLocationRelativeTo(null); // Center the frame
- frame.setVisible(true);
- }
- }
- class NewPanel extends JPanel {
- int w = 750;
- int h = 750;
- int scale = 200;
- int unitscale = 1;
- int x,y,x2,y2 = 0;
- int arrowsize = 50;
- int res = 1000;
- double[] xPoints = new double[res];
- double[] yPoints = new double[res];
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.translate(400, 400);
- Graphics2D g2 = (Graphics2D) g;
- //draw function
- for(int n = -1000; n < 1000; n++){
- x2 = x;
- y2 = y;
- x = ((n)+w/2);
- y = (h/2-((n*n)/scale));
- g.drawLine(x2, y2, x, y);
- }
- // FILL ARRAY WITH VALUES
- for(int n = 0; n < res; ++n){
- xPoints[n] = n*unitscale;
- yPoints[n] = (Math.sin(((2*Math.PI)/360)*(n*unitscale)));
- }
- // SCALE VALUES ACCORDING TO SCALE INTEGER
- for(int n = 0; n < xPoints.length; n++) {
- xPoints[n] = xPoints[n];
- yPoints[n] = yPoints[n]*scale;
- }
- for(int n = 0; n < xPoints.length-1; n++) {
- g.drawLine((int) xPoints[n+1],(int) yPoints[n+1],(int) xPoints[n],(int) yPoints[n]);
- }
- // for(int n = -360; n < 360; n++){
- // x2 = x;
- // y2 = y;
- // x = n-360+h/2;
- // y = (int) (h/2-Math.sin(((2*Math.PI)/360)*(n*unitscale))*scale);
- // g.drawLine(x2, y2, x, y);
- // }
- g.drawLine(-w/2, 0, w/2, 0); //x akse
- g.drawString("x", 0, w); //x mærkat
- // g.drawLine(w, 0, w-arrowsize, arrowsize); //del af pil til x akse
- // g.drawLine(w, 0, w-arrowsize, arrowsize); //del af pil til x akse
- g.drawLine(0, -h/2, 0, h/2); //y akse
- g.drawString("y", w/2, h); //y mærkat
- // g.drawLine(w/2, h, w/2-arrowsize, h-arrowsize); //del af pil til y akse
- // g.drawLine(200, 10, 210, 25); //del af pil til y akse
- g.drawString("-2pi", -360/unitscale, 0);
- g.drawString("2pi", 360/unitscale, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement