Advertisement
Guest User

Untitled

a guest
May 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package mloeschl8_2;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.*;
  7. import java.awt.Color;
  8.  
  9. public class RechteckPanel extends JPanel{
  10. private JButton löschen, einRechteck, zehnRechtecke;
  11. private RechteckZeichnung grafik;
  12.  
  13. /**
  14. *
  15. */
  16. public RechteckPanel(){
  17. this.setLayout(new BorderLayout());
  18.  
  19. grafik = new RechteckZeichnung(0);
  20. this.add(grafik);
  21.  
  22. JPanel oben = new JPanel(new GridLayout(1, 3));
  23. löschen = new JButton("Löschen");
  24. oben.add(löschen);
  25. einRechteck = new JButton("1 Rechteck");
  26. oben.add(einRechteck);
  27. zehnRechtecke = new JButton("10 Rechtecke");
  28. oben.add(zehnRechtecke);
  29.  
  30. this.add(oben, BorderLayout.PAGE_START);
  31.  
  32. ActionHandler ah = new ActionHandler();
  33. löschen.addActionListener(ah);
  34. einRechteck.addActionListener(ah);
  35. zehnRechtecke.addActionListener(ah);
  36.  
  37. }
  38. private class ActionHandler implements ActionListener{
  39. @Override
  40. public void actionPerformed(ActionEvent e){
  41. if(e.getSource() == löschen){
  42. grafik.hide();
  43. } else if(e.getSource() == einRechteck){
  44. grafik.show();
  45. //X
  46. grafik.setBereichA(getWidth());
  47. grafik.setX();
  48. //Y
  49. grafik.setBereichB(getHeight());
  50. grafik.setY();
  51. grafik.setFarbe();
  52.  
  53. } else if(e.getSource() == zehnRechtecke){
  54. show();
  55. grafik.setAnzahl(10);
  56. }
  57.  
  58.  
  59. }
  60. }
  61. }
  62.  
  63.  
  64. package mloeschl8_2;
  65.  
  66. import javax.swing.*;
  67. import java.awt.*;
  68.  
  69. public class RechteckZeichnung extends JPanel{
  70. private Color farbe ;
  71. private int x, y;
  72. private int a = 20;
  73. private int bereich;
  74. private int anzahl;
  75.  
  76.  
  77. public void setAnzahl(int anzahl) {
  78. this.anzahl = anzahl;
  79. super.repaint();
  80. }
  81.  
  82. public RechteckZeichnung(int anzahl){
  83. this.anzahl = anzahl;
  84. }
  85. public Color generateColor(){
  86. int r, g, b;
  87. r = (int)(Math.random() * 256);
  88. g = (int)(Math.random() * 256);
  89. b = (int)(Math.random() * 256);
  90. Color back = new Color(r, g, b);
  91. return back;
  92. }
  93. public int generatePositionX(){
  94. int back = (int)(Math.random() * this.bereich);
  95. return back;
  96. }
  97. public void setX(){
  98. this.x = generatePositionX();
  99. }
  100. public void setFarbe(){
  101. this.farbe = generateColor();
  102. this.repaint();
  103. }
  104. public void setY(){
  105. this.y = generatePositionX();
  106. }
  107. public void setBereichA(int a){
  108. this.bereich = a;
  109. }
  110. public void setBereichB(int a){
  111. this.bereich = a;
  112. }
  113. @Override
  114. public void paintComponent(Graphics g){
  115. super.paintComponent(g);
  116. if(this.anzahl != 0) {
  117. for (int i = 0; i < this.anzahl; i++) {
  118. setBereichA(getWidth());
  119. setX();
  120. //Y
  121. setBereichB(getHeight());
  122. setY();
  123. setFarbe();
  124. g.setColor(this.farbe);
  125. g.fillRect(x, y, a, a);
  126. }
  127. }else{
  128. g.setColor(this.farbe);
  129. g.fillRect(x, y, a, a);
  130. }
  131.  
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement