Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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.
  5. */
  6. package lab6;
  7.  
  8. import javax.microedition.lcdui.*;
  9.  
  10. public class MyCanvas extends Canvas implements CommandListener {
  11.  
  12. Midlet midlet;
  13. int xPos;
  14. int yPos;
  15. int rColor;
  16. int gColor;
  17. int bColor;
  18. boolean key_command[];
  19.  
  20. public MyCanvas(Midlet _midlet) {
  21. midlet = _midlet;
  22. key_command = new boolean[4];
  23. for (int i = 0; i < 4; i++) {
  24. key_command[i] = false;
  25. }
  26. addCommand(new Command("Koniec", Command.EXIT, 0));
  27. setCommandListener(this);
  28.  
  29. xPos = getWidth() / 2;
  30. yPos = getHeight() / 2;
  31.  
  32. rColor = 10;
  33. gColor = 10;
  34. bColor = 255;
  35.  
  36. }
  37.  
  38. protected void paint(Graphics g) {
  39.  
  40. g.setColor(0xffffff);
  41. int screenWidth = getWidth();
  42. int screenHeight = getHeight();
  43.  
  44. g.fillRect(0, 0, screenWidth, screenHeight);
  45.  
  46. g.setColor(rColor, gColor, bColor);
  47. g.fillRect(xPos, yPos, 32, 32);
  48. System.out.println("repaint");
  49.  
  50. }
  51.  
  52. protected void keyPressed(int keyCode) {
  53. moveRectangle(keyCode);
  54. }
  55.  
  56. protected void keyRepeated(int keyCode) {
  57. moveRectangle(keyCode);
  58. }
  59.  
  60. public void commandAction(Command c, Displayable d) {
  61. switch (c.getCommandType()) {
  62. case Command.EXIT:
  63. midlet.destroyApp(false);
  64. midlet.notifyDestroyed();
  65. break;
  66. }
  67. }
  68.  
  69. private void moveRectangle(int keyCode) {
  70. switch (keyCode) {
  71. case KEY_NUM2:
  72. --yPos;
  73. rColor = 255;
  74. gColor = 10;
  75. bColor = 10;
  76. break;
  77. case KEY_NUM8:
  78. ++yPos;
  79. rColor = 10;
  80. gColor = 255;
  81. bColor = 10;
  82. break;
  83. case KEY_NUM4:
  84. --xPos;
  85. rColor = 255;
  86. gColor = 255;
  87. bColor = 10;
  88. break;
  89. case KEY_NUM6:
  90. ++xPos;
  91. rColor = 10;
  92. gColor = 255;
  93. bColor = 255;
  94. break;
  95. }
  96. repaint();
  97. serviceRepaints();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement