Guest User

Untitled

a guest
Jun 30th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. JPanel: automatically paint at creation?
  2. private class RadarPanel extends JPanel {
  3. public RadarPanel() {
  4. super();
  5. this.repaint();
  6. }
  7.  
  8. @Override
  9. public void paintComponent(Graphics g) {
  10. super.paintComponent(g);
  11.  
  12. //painting logic here
  13.  
  14. //repaint in 500 ms
  15. this.repaint(500);
  16. }
  17. }
  18.  
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.util.ArrayList;
  25.  
  26. import javax.swing.JFrame;
  27. import javax.swing.JPanel;
  28.  
  29. public class PlayerRadar extends JFrame {
  30. private static final long serialVersionUID = 230324190;
  31.  
  32. //settings
  33. private static final int windowWidth = 300;
  34. private static final int windowHeight = 300;
  35. private static final int maxDistance = 250;
  36.  
  37. //components
  38. private PlayerRadar radarWindow;
  39. private JPanel radarPanel;
  40.  
  41. public PlayerRadar(String title) {
  42. super(title);
  43.  
  44. //set reference
  45. radarWindow = this;
  46.  
  47. //create radar window
  48. Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  49. this.setAlwaysOnTop(true);
  50. this.setBackground(new Color(0xFFFFFF));
  51. this.setBounds(screenSize.width - windowWidth, 0, windowWidth, windowHeight);
  52. this.addWindowListener(new WindowAdapter() {
  53. public void windowClosing(WindowEvent e) {
  54. radarWindow.setVisible(false);
  55. }
  56. });
  57. this.setVisible(true);
  58.  
  59. //create a JPanel for drawing
  60. radarPanel = new RadarPanel();
  61. radarPanel.setBounds(0, 0, windowWidth, windowHeight);
  62. radarPanel.setBackground(new Color(0xFFFFFF));
  63.  
  64. //add to frame
  65. this.getContentPane().add(radarPanel);
  66. }
  67.  
  68. private class RadarPanel extends JPanel {
  69. private static final long serialVersionUID = 230324191;
  70. private static final int repaintInterval = 500;
  71.  
  72. public RadarPanel() {
  73. super();
  74. }
  75.  
  76. @Override
  77. public void paint(Graphics g) {
  78. super.paint(g);
  79.  
  80. //draw player oval (center of the frame)
  81. g.setColor(Color.BLUE); //blue
  82. int ovalWidth = (int) Math.round(this.getWidth() / 30);
  83. int ovalHeight = (int) Math.round(this.getHeight() / 30);
  84. int playerLocalX = (int) Math.round(this.getWidth() / 2);
  85. int playerLocalY = (int) Math.round(this.getHeight() / 2);
  86. int ovalX = playerLocalX - ovalWidth / 2;
  87. int ovalY = playerLocalY - ovalHeight / 2;
  88. g.fillOval(ovalX, ovalY, ovalWidth, ovalHeight);
  89. g.setColor(Color.BLACK); //black
  90. g.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
  91.  
  92. //get info of the player itself
  93. PlayerInfo thisPlayer = GameUtil.getPlayerInfo();
  94. float playerPosZ = thisPlayer.position[0];
  95. float playerPosX = thisPlayer.position[2];
  96. //float playerRotRad = thisPlayer.rotation;
  97.  
  98. //set rectangle specs
  99. int rectWidth = this.getWidth() / 40;
  100. int rectHeight = this.getWidth() / 40;
  101.  
  102. //only continue if we have information about our player
  103. if (thisPlayer != null) {
  104. //get nearby players
  105. ArrayList<PlayerInfo> playersInfo = GameUtil.getNearbyPlayers();
  106.  
  107. //for each other player, draw a rectangle
  108. for (PlayerInfo playerInfo : playersInfo) {
  109. //get data
  110. float posZ = playerInfo.position[0];
  111. float posX = playerInfo.position[2];
  112. //float rotRad = playerInfo.rotation;
  113.  
  114. //calculate relative x and y
  115. int rectX = playerLocalX + Math.round((posX - playerPosX) / maxDistance * this.getWidth() / 2) - rectWidth / 2;
  116. int rectY = playerLocalY + ovalHeight / 2 + Math.round((playerPosZ - posZ) / maxDistance * this.getHeight() / 2) - rectHeight / 2;
  117.  
  118. //draw rectangle
  119. g.setColor(Color.RED);
  120. g.fillRect(rectX, rectY, rectWidth, rectHeight);
  121. g.setColor(Color.BLACK);
  122. g.drawRect(rectX, rectY, rectWidth, rectHeight);
  123. }
  124. }
  125.  
  126. //repaint soon
  127. this.repaint(repaintInterval);
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment