Advertisement
Guest User

PLACE

a guest
May 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public abstract class Plats extends JComponent{
  9.  
  10.  
  11. private Coordinates position;
  12. private Color color;
  13. private String kategori;
  14. private String name;
  15.  
  16.  
  17.  
  18. public Plats(int xCord, int yCord, String kategori, String name) {
  19. this.name = name;
  20. this.position = new Coordinates(xCord, yCord);
  21. this.kategori = kategori;
  22. if (kategori.equals("Bus"))
  23. this.color = Color.RED;
  24. else if (kategori.equals("Underground"))
  25. this.color = Color.BLUE;
  26. else if (kategori.equals("Train"))
  27. this.color = Color.GREEN;
  28. else
  29. this.color = Color.BLACK;
  30.  
  31. setBounds(xCord-15, yCord-30, 30, 30);
  32.  
  33.  
  34.  
  35.  
  36. FlyttLyss flyttLyss = new FlyttLyss();
  37. addMouseMotionListener(flyttLyss);
  38. addMouseListener(flyttLyss);
  39. addKeyListener(new PilLyss());
  40.  
  41.  
  42.  
  43. }
  44.  
  45.  
  46. @Override
  47. protected void paintComponent(Graphics g) {
  48. super.paintComponent(g);
  49. int xpoints[] = {0, 15, 30};
  50. int ypoints[] = {0, 30, 0};
  51. int npoints = 3;
  52. g.setColor(color);
  53. g.fillPolygon(xpoints, ypoints, npoints);
  54.  
  55.  
  56. //repaint(); ha dessa i plats kanske?? //Peter
  57. //validate();
  58.  
  59.  
  60. }
  61. public void showInfo(Plats p) {
  62. String name = p.getName();
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69. class FlyttLyss extends MouseAdapter {
  70. private int startX, startY;
  71.  
  72. @Override
  73. public void mousePressed(MouseEvent mev) {
  74. startX = mev.getX();
  75. startY = mev.getY();
  76. requestFocusInWindow();
  77.  
  78.  
  79. }
  80.  
  81. public void mouseDragged(MouseEvent mev) {
  82. int x = mev.getX();
  83. int y = mev.getY();
  84. setLocation(getX() + x - startX, getY() + y - startY);
  85. }
  86.  
  87.  
  88. }
  89.  
  90.  
  91. class PilLyss extends KeyAdapter {
  92. @Override
  93. public void keyPressed(KeyEvent kev) {
  94. int x = getX();
  95. int y = getY();
  96. switch (kev.getKeyCode()) {
  97. case KeyEvent.VK_LEFT:
  98. x--;
  99. break;
  100. case KeyEvent.VK_RIGHT:
  101. x++;
  102. break;
  103. case KeyEvent.VK_UP:
  104. y--;
  105. break;
  106. case KeyEvent.VK_DOWN:
  107. y++;
  108. break;
  109.  
  110. }
  111. setLocation(x, y);
  112.  
  113.  
  114. }
  115.  
  116.  
  117. }
  118.  
  119.  
  120. public Color getColor() {
  121. return color;
  122. }
  123. public void setColorMarked(Plats p) {
  124. p.color = Color.orange;
  125.  
  126. }
  127. public void setOriginalColor(Plats p){
  128. if (p.getKategori().equals("Bus"))
  129. p.color = Color.RED;
  130. else if (p.getKategori().equals("Underground"))
  131. p.color = Color.BLUE;
  132. else if (p.getKategori().equals("Train"))
  133. p.color = Color.GREEN;
  134. else
  135. p.color = Color.BLACK;
  136. }
  137.  
  138. public String getKategori() {
  139. return kategori;
  140. }
  141.  
  142.  
  143. @Override
  144. public String getName() {
  145. return name;
  146. }
  147.  
  148. public int getX_cord() {
  149. return position.getX_cord();
  150.  
  151. }
  152. public int getY_cord() {
  153. return position.getY_cord();
  154.  
  155. }
  156. public String getDescription() {
  157. return DescribedPlace.getDesc();
  158. }
  159.  
  160.  
  161. public Coordinates getPosition() {
  162. return position;
  163.  
  164. }
  165. @Override
  166. public String toString() {
  167. return name + color.toString() + kategori+ position.getX_cord()+ position.getY_cord();
  168.  
  169.  
  170. }
  171.  
  172.  
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement