Guest User

Untitled

a guest
Jul 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // die Figur
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4.  
  5. public abstract class Figure {
  6. public int xValue;
  7. public int yValue;
  8. public Graphics g;
  9. //usw
  10.  
  11. public Figure(int xValue, int yValue, Graphics g) {
  12. this.xValue = xValue;
  13. this.yValue = yValue;
  14. this.g = g;
  15. }
  16.  
  17. // falls man keine werte eingeben will das standard polygon/quadrat ..
  18. public Figure(Graphics g) {
  19. this.xValue = 10;
  20. this.yValue = 10;
  21. this.g = g;
  22. }
  23.  
  24. abstract void draw(); // muss bei jeder vererbung überschrieben werden
  25. }
  26.  
  27. // das quadrat
  28.  
  29. public class Quadrat extends Figure {
  30. //hier vielleicht noch den konstruktor überschreiben falls man mehr oder
  31. // weniger daten braucht um die figur zu beschreiben
  32. public Quadrat(Graphics g) {
  33. super(g);
  34. }
  35.  
  36.  
  37. @Override
  38. void draw() {
  39. int r = (int) (Math.random() * 200);
  40. g.setColor(new Color(r, 0, 0));
  41. g.fillRect(this.xValue, this.yValue, 20, 20); // die werte der superklasse
  42.  
  43. }
  44.  
  45. }
  46.  
  47. // das gui
  48. public class FunnyThings {
  49. private JFrame f = new JFrame("irgendein cooler titel");
  50. private MyDrawPanel m1;
  51. //hier bestimmen was gezeichnet wird
  52. public boolean drawFigureOne = true;
  53. public boolean drawFigureTwo = true;
  54.  
  55. public static void main(String[] args) {
  56. new FunnyThings().drawGui();
  57.  
  58. }
  59.  
  60. public void drawGui() {
  61. m1 = new MyDrawPanel();
  62. JButton drawBtn = new JButton("Zeichnen");
  63. drawBtn.addActionListener(new drawBtnListener());
  64. f.setContentPane(m1);
  65. f.getContentPane().add(drawBtn);
  66. f.setBounds(30, 30, 300, 300);
  67. f.setVisible(true);
  68. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69. }
  70.  
  71. public class MyDrawPanel extends JPanel{
  72. public void paintComponent(Graphics g) {
  73.  
  74. if (drawFigureOne) {
  75. Quadrat quad = new Quadrat(g);
  76. quad.draw();
  77. }
  78. }
  79.  
  80. }
  81.  
  82. public class drawBtnListener implements ActionListener {
  83.  
  84. @Override
  85. public void actionPerformed(ActionEvent e) {
  86. m1.repaint();
  87.  
  88. }
  89.  
  90. }
  91. }
Add Comment
Please, Sign In to add comment