Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. // Liang listing 14.3
  2.  
  3. import javax.swing.*;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.geom.Line2D;
  7.  
  8. public class TestPanelDrawing extends JFrame {
  9.  
  10.   public TestPanelDrawing() {
  11.     add(new NewPanel());
  12.   }
  13.  
  14.   public static void main(final String[] args) {
  15.     final TestPanelDrawing frame = new TestPanelDrawing();
  16.     frame.setTitle("TestPanelDrawing");
  17.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.     frame.setSize(800, 800);
  19.     frame.setLocationRelativeTo(null); // Center the frame
  20.     frame.setVisible(true);
  21.   }
  22. }
  23.  
  24. class NewPanel extends JPanel {
  25.   int w = 750;
  26.   int h = 750;
  27.   int scale = 200;
  28.   int unitscale = 1;
  29.   int x,y,x2,y2 = 0;
  30.   int arrowsize = 50;
  31.   int res = 1000;
  32.   double[] xPoints = new double[res];
  33.   double[] yPoints = new double[res];
  34.  
  35.  
  36.   protected void paintComponent(Graphics g) {
  37.     super.paintComponent(g);
  38.    
  39.     g.translate(400, 400);
  40.     Graphics2D g2 = (Graphics2D) g;
  41.    
  42.    
  43.     //draw function
  44.    
  45.     for(int n = -1000; n < 1000; n++){
  46.         x2 = x;
  47.         y2 = y;
  48.         x = ((n)+w/2);
  49.         y = (h/2-((n*n)/scale));
  50.         g.drawLine(x2, y2, x, y);
  51.     }
  52.    
  53.     // FILL ARRAY WITH VALUES
  54.     for(int n = 0; n < res; ++n){
  55.         xPoints[n] = n*unitscale;
  56.         yPoints[n] = (Math.sin(((2*Math.PI)/360)*(n*unitscale)));
  57.     }
  58.    
  59.     // SCALE VALUES ACCORDING TO SCALE INTEGER
  60.    
  61.     for(int n = 0; n < xPoints.length; n++) {
  62.         xPoints[n] = xPoints[n];
  63.         yPoints[n] = yPoints[n]*scale;
  64.     }
  65.    
  66.     for(int n = 0; n < xPoints.length-1; n++) {
  67.         g.drawLine((int) xPoints[n+1],(int) yPoints[n+1],(int) xPoints[n],(int) yPoints[n]);
  68.     }
  69. //    for(int n = -360; n < 360; n++){
  70. //      x2 = x;
  71. //      y2 = y;
  72. //      x = n-360+h/2;
  73. //      y = (int) (h/2-Math.sin(((2*Math.PI)/360)*(n*unitscale))*scale);
  74. //      g.drawLine(x2, y2, x, y);
  75. //    }
  76.    
  77.    
  78.     g.drawLine(-w/2, 0, w/2, 0);    //x akse
  79.     g.drawString("x", 0, w);        //x mærkat
  80. //    g.drawLine(w, 0, w-arrowsize, arrowsize); //del af pil til x akse
  81. //    g.drawLine(w, 0, w-arrowsize, arrowsize); //del af pil til x akse
  82.     g.drawLine(0, -h/2, 0, h/2);    //y akse
  83.     g.drawString("y", w/2, h);  //y mærkat
  84. //    g.drawLine(w/2, h, w/2-arrowsize, h-arrowsize);   //del af pil til y akse
  85. //    g.drawLine(200, 10, 210, 25);     //del af pil til y akse
  86.     g.drawString("-2pi", -360/unitscale, 0);
  87.     g.drawString("2pi", 360/unitscale, 0);
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement