java—每次使用循环存储矩形大小

h5qlskok  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(417)

创建了一个类似于paint的程序,其中有一个矩形,其在屏幕内的移动可以通过“w,a,s,d”键控制,其大小可以使用鼠标上的滚动条增减。还有几个不同颜色的按钮,当按下时,这些按钮会用各自的颜色填充矩形形状。我也试着用这个矩形来画画,当我按下空格键时,我想填充指定区域所选的颜色。即使我能做到。我做的下一个动作是:要么按“w,a,s,d”键,要么按滚动条,颜色消失。现在我知道必须以某种方式保存colour或fillrect(),以便下一个操作不会影响它,但我尝试了几种方法,但都没有实现。我尝试使用循环来存储矩形的值,这样每次重新绘制时,它都会重新绘制循环中包含的所有值。但这不管用。如果它不工作,由于一些误判做说,如果不是和这个方法使用循环是错误的,请不要建议其他简单的方法来做到这一点。
所以基本上我要做的是,当按下空格键时,我想要一个那个颜色的矩形的印记,当我移开这个矩形的时候。我要留下印记。
注:我知道我的代码是穷人,并不是所有可行的,但我做这个项目与我们所教的,我希望你会提供沿着这条线的解决方案。

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Animation extends Frame implements KeyListener, MouseWheelListener, ActionListener {
  5. int x,y,a,b;
  6. char choice1;
  7. int draw=1;
  8. int defaultValue=0;
  9. int n=0;
  10. int color1,color2,color3;
  11. Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
  12. Animation() {
  13. setSize(1000, 1000);
  14. setVisible(true);
  15. x = 500;
  16. y = 500;
  17. a = 20;
  18. b = 50;
  19. JPanel panel = new JPanel();
  20. button1 = new Button("Black");
  21. button2 = new Button("Blue");
  22. button3 = new Button("Green");
  23. button4 = new Button("Orange");
  24. button5 = new Button("Red");
  25. button6 = new Button("Yellow");
  26. button7 = new Button("Gray");
  27. button8 = new Button("Cyan");
  28. button9 = new Button("Magenta");
  29. button10 = new Button("Pink");
  30. button11 = new Button("Default");
  31. panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);
  32. panel.add(button6);panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button10);
  33. panel.add(button11);
  34. add(panel);
  35. // button1.setBounds(50,680,50,20); button2.setBounds(120,680,50,20);
  36. // button3.setBounds(190,680,50,20); button4.setBounds(260,680,50,20);
  37. // button5.setBounds(330,680,50,20); button6.setBounds(400,680,50,20);
  38. // button7.setBounds(470,680,50,20); button8.setBounds(540,680,50,20);
  39. // button9.setBounds(610,680,50,20); button10.setBounds(680,680,50,20);
  40. button1.setFocusable(false);
  41. button2.setFocusable(false);
  42. button3.setFocusable(false);
  43. button4.setFocusable(false);
  44. button5.setFocusable(false);
  45. button6.setFocusable(false);
  46. button7.setFocusable(false);
  47. button8.setFocusable(false);
  48. button9.setFocusable(false);
  49. button10.setFocusable(false);
  50. button11.setFocusable(false);
  51. button1.addActionListener(this);button2.addActionListener(this);
  52. button3.addActionListener(this);button4.addActionListener(this);
  53. button5.addActionListener(this);button6.addActionListener(this);
  54. button7.addActionListener(this);button8.addActionListener(this);
  55. button9.addActionListener(this);button10.addActionListener(this);
  56. button11.addActionListener(this);
  57. addKeyListener(this);
  58. addMouseWheelListener(this);
  59. addWindowListener(new WindowListener() {
  60. @Override
  61. public void windowOpened(WindowEvent e) {
  62. }
  63. @Override
  64. public void windowClosing(WindowEvent e) {
  65. System.exit(0);
  66. }
  67. @Override
  68. public void windowClosed(WindowEvent e) {
  69. }
  70. @Override
  71. public void windowIconified(WindowEvent e) {
  72. }
  73. @Override
  74. public void windowDeiconified(WindowEvent e) {
  75. }
  76. @Override
  77. public void windowActivated(WindowEvent e) {
  78. }
  79. @Override
  80. public void windowDeactivated(WindowEvent e) {
  81. }
  82. });
  83. }
  84. @Override
  85. public void keyTyped(KeyEvent e) {
  86. }
  87. @Override
  88. public void keyPressed(KeyEvent e) {
  89. }
  90. @Override
  91. public void keyReleased(KeyEvent e) {
  92. choice1 = e.getKeyChar();
  93. if (choice1 == 'w') {
  94. y = y - 10;
  95. }
  96. if (choice1 == 's') {
  97. y = y + 10;
  98. }
  99. if (choice1 == 'a') {
  100. x = x - 10;
  101. }
  102. if (choice1 == 'd') {
  103. x = x + 10;
  104. }
  105. if(choice1 == ' '){
  106. draw=2;
  107. }
  108. repaint();
  109. }
  110. @Override
  111. public void mouseWheelMoved(MouseWheelEvent e) {
  112. double p = e.getPreciseWheelRotation();
  113. if(p>0){
  114. a=a+5;
  115. b=b+5;
  116. } else{
  117. a=a-5;
  118. b=b-5;
  119. }
  120. repaint();
  121. }
  122. @Override
  123. public void actionPerformed(ActionEvent e) {
  124. defaultValue = 0;
  125. if(e.getActionCommand().equals("Black")){
  126. color1 = 0;
  127. color2 = 0;
  128. color3 = 0;
  129. }
  130. if(e.getActionCommand().equals("Blue")){
  131. color1 = 0;
  132. color2 = 0;
  133. color3 = 255;
  134. }
  135. if(e.getActionCommand().equals("Green")){
  136. color1 = 0;
  137. color2 = 255;
  138. color3 = 0;
  139. }
  140. if(e.getActionCommand().equals("Orange")){
  141. color1 = 255;
  142. color2 = 165;
  143. color3 = 0;
  144. }
  145. if(e.getActionCommand().equals("Red")){
  146. color1 = 255;
  147. color2 = 0;
  148. color3 = 0;
  149. }
  150. if(e.getActionCommand().equals("Yellow")){
  151. color1 = 255;
  152. color2 = 255;
  153. color3 = 0;
  154. }
  155. if(e.getActionCommand().equals("Gray")){
  156. color1 = 169;
  157. color2 = 169;
  158. color3 = 169;
  159. }
  160. if(e.getActionCommand().equals("Cyan")){
  161. color1 = 0;
  162. color2 = 255;
  163. color3 = 255;
  164. }
  165. if(e.getActionCommand().equals("Magenta")){
  166. color1 = 255;
  167. color2 = 0;
  168. color3 = 255;
  169. }
  170. if(e.getActionCommand().equals("Pink")){
  171. color1 = 255;
  172. color2 = 192;
  173. color3 = 203;
  174. }
  175. if(e.getActionCommand().equals("Default")){
  176. defaultValue = 1;
  177. }
  178. repaint();
  179. }
  180. public void paint(Graphics g) {
  181. super.paint(g);
  182. if(draw==1) {
  183. if(defaultValue == 1){
  184. g.setColor(new Color(color1,color2,color3));
  185. g.drawRect(x, y, a, b);
  186. }else{
  187. g.drawRect(x, y, a, b);
  188. g.setColor(new Color(color1,color2,color3));
  189. g.fillRect(x,y,a,b);
  190. }
  191. }
  192. if(draw==2){
  193. g.setColor(new Color(color1,color2,color3));
  194. int[] temp1 = new int[50];
  195. temp1[n] = x;
  196. int[] temp2 = new int[50];
  197. temp2[n] = y;
  198. int[] temp3 = new int[50];
  199. temp3[n] = a;
  200. int[] temp4 = new int[50];
  201. temp4[n] = b;
  202. n++;
  203. for (int i=0;i<n;i++){
  204. g.drawRect(x, y, a, b);
  205. g.fillRect(temp1[i],temp2[i],temp3[i],temp4[i]);
  206. }
  207. draw=1;
  208. }
  209. }
  210. public static void main(String[] args) {
  211. Animation animation = new Animation();
  212. }
  213. }
6rvt4ljy

6rvt4ljy1#

我不是一个swingMaven,但我试图修复你的代码,我认为它的工作。
进行了以下更改:
存储所有压印图像的列表(通过输入“空格键”),这将删除代码中的整数数组。
java,值对象(添加在此代码底部)
为moue-pressed动作添加了mouselistener。
对颜色组件使用颜色常量而不是int值。
基本上,点击空格键会将当前矩形添加到列表中,稍后可以重新绘制。

  1. import java.awt.Button;
  2. import java.awt.Color;
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.MouseWheelEvent;
  13. import java.awt.event.MouseWheelListener;
  14. import java.awt.event.WindowEvent;
  15. import java.awt.event.WindowListener;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import javax.swing.JPanel;
  19. public class Animation extends Frame implements KeyListener, MouseWheelListener,MouseListener, ActionListener {
  20. /**
  21. *
  22. */
  23. private static final long serialVersionUID = 1L;
  24. int x,y,a,b;
  25. char choice1;
  26. int defaultValue=0;
  27. int number=0;
  28. Color cursorColor = Color.BLACK;
  29. boolean isImprint = false;
  30. private Rectangle baseRect = null;
  31. private List<Rectangle> lstImprints = new ArrayList<Rectangle>();
  32. Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
  33. Animation() {
  34. System.out.println("animation ");
  35. setSize(1000, 1000);
  36. setVisible(true);
  37. x = 500;
  38. y = 500;
  39. a = 20;
  40. b = 50;
  41. baseRect = new Rectangle(x, y, a, b, cursorColor);
  42. JPanel panel = new JPanel();
  43. button1 = new Button("Black");
  44. button2 = new Button("Blue");
  45. button3 = new Button("Green");
  46. button4 = new Button("Orange");
  47. button5 = new Button("Red");
  48. button6 = new Button("Yellow");
  49. button7 = new Button("Gray");
  50. button8 = new Button("Cyan");
  51. button9 = new Button("Magenta");
  52. button10 = new Button("Pink");
  53. button11 = new Button("Default");
  54. panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);
  55. panel.add(button6);panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button10);
  56. panel.add(button11);
  57. add(panel);
  58. // button1.setBounds(50,680,50,20); button2.setBounds(120,680,50,20);
  59. // button3.setBounds(190,680,50,20); button4.setBounds(260,680,50,20);
  60. // button5.setBounds(330,680,50,20); button6.setBounds(400,680,50,20);
  61. // button7.setBounds(470,680,50,20); button8.setBounds(540,680,50,20);
  62. // button9.setBounds(610,680,50,20); button10.setBounds(680,680,50,20);
  63. button1.setFocusable(false);
  64. button2.setFocusable(false);
  65. button3.setFocusable(false);
  66. button4.setFocusable(false);
  67. button5.setFocusable(false);
  68. button6.setFocusable(false);
  69. button7.setFocusable(false);
  70. button8.setFocusable(false);
  71. button9.setFocusable(false);
  72. button10.setFocusable(false);
  73. button11.setFocusable(false);
  74. button1.addActionListener(this);button2.addActionListener(this);
  75. button3.addActionListener(this);button4.addActionListener(this);
  76. button5.addActionListener(this);button6.addActionListener(this);
  77. button7.addActionListener(this);button8.addActionListener(this);
  78. button9.addActionListener(this);button10.addActionListener(this);
  79. button11.addActionListener(this);
  80. this.addMouseListener(this);
  81. addKeyListener(this);
  82. addMouseWheelListener(this);
  83. addWindowListener(new WindowListener() {
  84. @Override
  85. public void windowOpened(WindowEvent e) {
  86. }
  87. @Override
  88. public void windowClosing(WindowEvent e) {
  89. System.exit(0);
  90. }
  91. @Override
  92. public void windowClosed(WindowEvent e) {
  93. }
  94. @Override
  95. public void windowIconified(WindowEvent e) {
  96. }
  97. @Override
  98. public void windowDeiconified(WindowEvent e) {
  99. }
  100. @Override
  101. public void windowActivated(WindowEvent e) {
  102. }
  103. @Override
  104. public void windowDeactivated(WindowEvent e) {
  105. }
  106. });
  107. }
  108. @Override
  109. public void keyTyped(KeyEvent e) {
  110. }
  111. @Override
  112. public void keyPressed(KeyEvent e) {
  113. }
  114. @Override
  115. public void keyReleased(KeyEvent e) {
  116. choice1 = e.getKeyChar();
  117. if (choice1 == 'w') {
  118. //y = y - 10;
  119. baseRect.setY(baseRect.getY()-10);
  120. }
  121. if (choice1 == 's') {
  122. //y = y + 10;
  123. baseRect.setY(baseRect.getY()+10);
  124. }
  125. if (choice1 == 'a') {
  126. //x = x - 10;
  127. baseRect.setX(baseRect.getX()-10);
  128. }
  129. if (choice1 == 'd') {
  130. //x = x + 10;
  131. baseRect.setX(baseRect.getX()-10);
  132. }
  133. if(choice1 == ' '){
  134. isImprint = true;
  135. }
  136. System.out.println("choice 1"+choice1);
  137. repaint();
  138. }
  139. @Override
  140. public void mouseWheelMoved(MouseWheelEvent e) {
  141. double p = e.getPreciseWheelRotation();
  142. if(p>0){
  143. baseRect.setHeight(baseRect.getHeight()+5);
  144. //b=b+5;
  145. baseRect.setWidth(baseRect.getWidth()+5);
  146. } else{
  147. baseRect.setHeight(baseRect.getHeight()-5);
  148. baseRect.setWidth(baseRect.getWidth()-5);
  149. //b=b-5;
  150. }
  151. repaint();
  152. }
  153. @Override
  154. public void actionPerformed(ActionEvent e) {
  155. System.out.println("action performed");
  156. defaultValue = 0;
  157. if(e.getActionCommand().equals("Black")){
  158. cursorColor = Color.BLACK;
  159. }
  160. if(e.getActionCommand().equals("Blue")){
  161. cursorColor = Color.BLUE;
  162. }
  163. if(e.getActionCommand().equals("Green")){
  164. cursorColor = Color.GREEN;
  165. }
  166. if(e.getActionCommand().equals("Orange")){
  167. cursorColor = Color.ORANGE;
  168. }
  169. if(e.getActionCommand().equals("Red")){
  170. cursorColor = Color.RED;
  171. }
  172. if(e.getActionCommand().equals("Yellow")){
  173. cursorColor = Color.YELLOW;
  174. }
  175. if(e.getActionCommand().equals("Gray")){
  176. cursorColor = Color.GRAY;
  177. }
  178. if(e.getActionCommand().equals("Cyan")){
  179. cursorColor = Color.CYAN;
  180. }
  181. if(e.getActionCommand().equals("Magenta")){
  182. cursorColor = Color.MAGENTA;
  183. }
  184. if(e.getActionCommand().equals("Pink")){
  185. cursorColor = Color.PINK;
  186. }
  187. if(e.getActionCommand().equals("Default")){
  188. defaultValue = 1;
  189. }
  190. repaint();
  191. }
  192. public void paint(Graphics g) {
  193. System.out.println("paint ");
  194. super.paint(g);
  195. g.setColor(cursorColor);
  196. g.drawRect(baseRect.getX(),baseRect.getY(),baseRect.getHeight(),baseRect.getWidth());
  197. g.fillRect(baseRect.getX(),baseRect.getY(),baseRect.getHeight(),baseRect.getWidth());
  198. System.out.println(lstImprints.size());
  199. for(Rectangle rect : lstImprints){
  200. g.setColor(rect.getColor());
  201. g.drawRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());
  202. g.fillRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());
  203. }
  204. /* if(defaultValue == 1){
  205. g.setColor(cursorColor);
  206. g.drawRect(rect.getX(),rect.getY(),rect.getHeight(),rect.getWidth());
  207. }else{
  208. g.drawRect(x, y, a, b);
  209. g.setColor(cursorColor);
  210. g.fillRect(x,y,a,b);
  211. }*/
  212. }
  213. public static void main(String[] args) {
  214. Animation animation = new Animation();
  215. }
  216. @Override
  217. public void mouseClicked(MouseEvent e) {
  218. // TODO Auto-generated method stub
  219. }
  220. @Override
  221. public void mouseEntered(MouseEvent e) {
  222. // TODO Auto-generated method stub
  223. }
  224. @Override
  225. public void mouseExited(MouseEvent e) {
  226. // TODO Auto-generated method stub
  227. }
  228. @Override
  229. public void mousePressed(MouseEvent e) {
  230. // TODO Auto-generated method stub
  231. }
  232. @Override
  233. public void mouseReleased(MouseEvent e) {
  234. // TODO Auto-generated method stub
  235. if(isImprint){
  236. Point point = e.getLocationOnScreen();
  237. Rectangle rectangle = new Rectangle(Double.valueOf(point.getX()).intValue(), Double.valueOf(point.getY()).intValue(), baseRect.getHeight(), baseRect.getWidth(),cursorColor);
  238. lstImprints.add(rectangle);
  239. isImprint = false;
  240. repaint();
  241. }
  242. }
  243. }
  1. import java.awt.Color;
  2. public class Rectangle {
  3. private int x;
  4. private int y;
  5. private int height;
  6. private int width;
  7. private Color color = null;
  8. public Rectangle(int x, int y, int height, int width, Color color) {
  9. super();
  10. this.x = x;
  11. this.y = y;
  12. this.height = height;
  13. this.width = width;
  14. this.color = color;
  15. }
  16. public int getX() {
  17. return x;
  18. }
  19. public void setX(int x) {
  20. this.x = x;
  21. }
  22. public int getY() {
  23. return y;
  24. }
  25. public void setY(int y) {
  26. this.y = y;
  27. }
  28. public int getHeight() {
  29. return height;
  30. }
  31. public void setHeight(int height) {
  32. this.height = height;
  33. }
  34. public int getWidth() {
  35. return width;
  36. }
  37. public void setWidth(int width) {
  38. this.width = width;
  39. }
  40. public Color getColor() {
  41. return color;
  42. }
  43. public void setColor(Color color) {
  44. this.color = color;
  45. }
  46. }
展开查看全部

相关问题