University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TegnUt.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import java.util.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. /**
  5. * Klasse for aa tegne et punktsett med n punkter (Helst n < 200) og
  6. * den konvekse innhyllinga i IntList CoHull, koordinatene i d.x[] og d.y[]
  7. * ver 7.mai 2015, 2016,2017
  8. ******************************************************************************/
  9. class TegnUt extends JFrame{
  10. Oblig4 d;
  11. IntList theCoHull;
  12. int n, MAX_Y;
  13. int [] x,y;
  14. TegnUt(Oblig4 d, IntList CoHull, String s ){
  15. theCoHull = CoHull;
  16. this.d =d;
  17. x = d.x;
  18. y = d.y;
  19. n = d.n;
  20. MAX_Y = d.MAX_Y;
  21. size = 500;
  22. margin = 50;
  23. scale =size/d.MAX_X +0.8;
  24. setTitle("Oblig4, num points:"+n+" "+s);
  25. grafen = new Graph();
  26. getContentPane().add(grafen, BorderLayout.CENTER);
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. pack();
  29. setVisible(true);
  30. // angir foretrukket storrelse paa dette lerretet.
  31. setPreferredSize(new Dimension(d.MAX_X+2*margin,d.MAX_Y+2*margin));
  32. }
  33. Graph grafen;
  34. int size , margin;
  35. double scale ;
  36. class Graph extends JPanel {
  37. void drawPoint(int p, Graphics g) {
  38. int SIZE =7;
  39. if (n <= 50) g.drawString(p+"("+x[p]+","+y[p]+")",xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2);
  40. else if (n <= 200)g.drawString(p+"",xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2);
  41. g.drawOval (xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2,SIZE,SIZE);
  42. g.fillOval (xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2,SIZE,SIZE);
  43. }
  44. Graph() {
  45. setPreferredSize(new Dimension(size+2*margin+10,size+2*margin+10));
  46. }
  47. int xDraw(int x){return (int)(x*scale)+ margin ;}
  48. int yDraw(int y){return (int)((MAX_Y-y)*scale+margin);}
  49. public void paintComponent(Graphics g) {
  50. super.paintComponent(g);
  51. g.setColor(Color.black);
  52. for (int i = 0; i < n; i++){
  53. drawPoint(i,g);
  54. }
  55. g.setColor(Color.red);
  56. // draw cohull
  57. int x2 = x[theCoHull.get(0)], y2 = y[theCoHull.get(0)],x1,y1;
  58. for (int i = 1; i < theCoHull.size(); i++){
  59. y1 = y2; x1=x2;
  60. x2 = x[theCoHull.get(i)];
  61. y2 = y[theCoHull.get(i)];
  62. g.drawLine (xDraw(x1),yDraw(y1), xDraw(x2),yDraw(y2));
  63. }
  64. g.drawLine (xDraw(x[theCoHull.get(theCoHull.size()-1)]),
  65. yDraw(y[theCoHull.get(theCoHull.size()-1)]),
  66. xDraw(x[theCoHull.get(0)]),yDraw(y[theCoHull.get(0)]));
  67. } // end paintComponent
  68. } // end class Graph
  69. }// end class DT