Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor. Made by Grzechu Rubinoff
  5. */
  6. package lab4;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.util.ArrayList;
  10.  
  11. public class AwtControlDemo implements KeyListener {
  12.  
  13. private Frame mainFrame;
  14. private Panel controlPanel;
  15. public ArrayList<Coord> coords = new ArrayList<Coord>();
  16. public double xShift, yShift;
  17. public int bufferShiftX = 500;
  18. public int bufferShiftY = 500;
  19.  
  20.  
  21.  
  22. public AwtControlDemo(ArrayList<Coord> coordList){
  23. coords = coordList;
  24. xShift = coords.get(0).getX();
  25. yShift = coords.get(0).getY();
  26.  
  27. prepareGUI();
  28. }
  29.  
  30. private void prepareGUI(){
  31. mainFrame = new Frame("Java AWT Examples");
  32. mainFrame.setSize(1000,1000);
  33. mainFrame.setLayout(new GridLayout(1, 1));
  34. mainFrame.addWindowListener(new WindowAdapter() {
  35. public void windowClosing(WindowEvent windowEvent){
  36. System.exit(0);
  37. }
  38. });
  39. controlPanel = new Panel();
  40. controlPanel.setLayout(new FlowLayout());
  41.  
  42. mainFrame.add(controlPanel);
  43. mainFrame.setVisible(true);
  44. mainFrame.addKeyListener(this);
  45. }
  46.  
  47. public void showCanvasDemo(){
  48. controlPanel.add(new MyCanvas());
  49. mainFrame.setVisible(true);
  50. }
  51.  
  52. @Override
  53. public void keyTyped(KeyEvent e) {
  54. char keyCode = e.getKeyChar();
  55. if(keyCode == 'w'){
  56. bufferShiftY -= 50;
  57. } else if(keyCode == 's'){
  58. bufferShiftY += 50;
  59. } else if(keyCode == 'a'){
  60. bufferShiftX -=50;
  61. } else if(keyCode == 'd'){
  62. bufferShiftX +=50;
  63. }
  64. controlPanel.removeAll();
  65. controlPanel.add(new MyCanvas());
  66. }
  67.  
  68. @Override
  69. public void keyPressed(KeyEvent e) {
  70. }
  71.  
  72. @Override
  73. public void keyReleased(KeyEvent e) {
  74. }
  75.  
  76. class MyCanvas extends Canvas {
  77.  
  78. public MyCanvas () {
  79. setBackground (new Color(230, 230, 220, 255));
  80. setSize(1000, 1000);
  81. }
  82.  
  83. public void paint (Graphics g) {
  84. Graphics2D g2;
  85. g2 = (Graphics2D) g;
  86. drawMap(g2);
  87. }
  88.  
  89. public void drawMap(Graphics2D g2) {
  90. double lastX = xShift;
  91. double lastY = yShift;
  92. for (Coord coord: coords) {
  93. int x1 =(int) ((lastX - xShift))*2;
  94. int y1 =(int) ((lastY - yShift))*2;
  95. int x2 =(int) ((coord.getX() - xShift))*2;
  96. int y2 =(int) ((coord.getY()- yShift))*2;
  97. g2.drawLine(x1+bufferShiftX, y1+bufferShiftY, x2+bufferShiftX, y2+bufferShiftY);
  98. lastX = coord.getX();
  99. lastY = coord.getY();
  100. }
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement