Dépôt du be graphe
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.

Drawing.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package org.insa.drawing;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.image.*;
  8. import javax.swing.JPanel;
  9. /**
  10. * Cette implementation de la classe Dessin produit vraiment un affichage
  11. * (au contraire de la classe DessinInvisible).
  12. */
  13. public class Drawing extends JPanel {
  14. /**
  15. *
  16. */
  17. private static final long serialVersionUID = 96779785877771827L;
  18. private final Graphics2D gr;
  19. private float long1;
  20. private float long2;
  21. private float lat1;
  22. private float lat2;
  23. // Width and height of the image
  24. private final int width, height;
  25. private boolean bb_is_set ;
  26. private Image image;
  27. private ZoomAndPanListener zoomAndPanListener;
  28. public boolean autoRepaint = true;
  29. /**
  30. * Cree et affiche une nouvelle fenetre de dessin.
  31. */
  32. public Drawing() {
  33. this.zoomAndPanListener = new ZoomAndPanListener(this, ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
  34. this.addMouseListener(zoomAndPanListener);
  35. this.addMouseMotionListener(zoomAndPanListener);
  36. this.addMouseWheelListener(zoomAndPanListener);
  37. this.width = 2000;
  38. this.height = 1600;
  39. BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
  40. this.image = img;
  41. this.gr = img.createGraphics();
  42. this.zoomAndPanListener.setCoordTransform(this.gr.getTransform());
  43. this.bb_is_set = false;
  44. this.long1 = 0.0f;
  45. this.long2 = this.width;
  46. this.lat1 = 0.0f;
  47. this.lat2 = this.height;
  48. this.clear();
  49. this.repaint();
  50. }
  51. @Override
  52. public void paintComponent(Graphics g1) {
  53. Graphics2D g = (Graphics2D)g1;
  54. g.clearRect(0, 0, getWidth(), getHeight());
  55. g.setTransform(zoomAndPanListener.getCoordTransform());
  56. g.drawImage(image, 0, 0, this);
  57. }
  58. public void setAutoRepaint(boolean autoRepaint) {
  59. this.autoRepaint = autoRepaint;
  60. }
  61. protected void doAutoPaint() {
  62. if (autoRepaint) {
  63. this.repaint();
  64. }
  65. }
  66. public void setWidth(int width) {
  67. this.gr.setStroke(new BasicStroke(width));
  68. }
  69. public void setColor(Color col) {
  70. this.gr.setColor(col);
  71. }
  72. public void clear() {
  73. this.gr.setColor(Color.WHITE);
  74. this.gr.fillRect(0, 0, this.width, this.height);
  75. }
  76. public void setBB(double long1, double long2, double lat1, double lat2) {
  77. if (long1 > long2 || lat1 > lat2) {
  78. throw new Error("DessinVisible.setBB : mauvaises coordonnees.");
  79. }
  80. this.long1 = (float)long1;
  81. this.long2 = (float)long2;
  82. this.lat1= (float)lat1;
  83. this.lat2 = (float)lat2;
  84. this.bb_is_set = true;
  85. double scale = 1 / Math.max(this.width / (double)this.getWidth(), this.height / (double)this.getHeight());
  86. this.zoomAndPanListener.getCoordTransform().setToIdentity();
  87. this.zoomAndPanListener.getCoordTransform().translate((this.getWidth() - this.width * scale) / 2,
  88. (this.getHeight() - this.height * scale) / 2);
  89. this.zoomAndPanListener.getCoordTransform().scale(scale, scale);
  90. this.zoomAndPanListener.setZoomLevel(0);
  91. this.repaint();
  92. }
  93. private int projx(float lon) {
  94. return (int)(width * (lon - this.long1) / (this.long2 - this.long1)) ;
  95. }
  96. private int projy(float lat) {
  97. return (int)(height * (1 - (lat - this.lat1) / (this.lat2 - this.lat1))) ;
  98. }
  99. private void checkBB() {
  100. if (!this.bb_is_set) {
  101. throw new Error("Classe DessinVisible : vous devez invoquer la methode setBB avant de dessiner.") ;
  102. }
  103. }
  104. public void drawLine(float long1, float lat1, float long2, float lat2) {
  105. this.checkBB() ;
  106. int x1 = this.projx(long1) ;
  107. int x2 = this.projx(long2) ;
  108. int y1 = this.projy(lat1) ;
  109. int y2 = this.projy(lat2) ;
  110. gr.drawLine(x1, y1, x2, y2) ;
  111. this.doAutoPaint();
  112. }
  113. public void drawPoint(float lon, float lat, int width) {
  114. this.checkBB() ;
  115. int x = this.projx(lon) - width / 2 ;
  116. int y = this.projy(lat) - width / 2 ;
  117. gr.fillOval (x, y, width, width) ;
  118. this.doAutoPaint();
  119. }
  120. public void putText(float lon, float lat, String txt) {
  121. this.checkBB() ;
  122. int x = this.projx(lon) ;
  123. int y = this.projy(lat) ;
  124. gr.drawString (txt, x, y) ;
  125. this.doAutoPaint();
  126. }
  127. }