Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10.  
  11.  
  12.  
  13. public class FunnyThings {
  14. static JFrame f = new JFrame("irgendein cooler titel");
  15. static MyDrawPanel m1;
  16. //hier bestimmen was gezeichnet wird
  17. public boolean drawFigureOne = true;
  18. public boolean drawFigureTwo = true;
  19.  
  20. public static void main(String[] args) {
  21. new FunnyThings().drawGui();
  22.  
  23. }
  24.  
  25. public void drawGui() {
  26. m1 = new MyDrawPanel();
  27. JButton drawBtn = new JButton("Zeichnen");
  28. drawBtn.addActionListener(new drawBtnListener());
  29. f.setContentPane(m1);
  30. f.getContentPane().add(drawBtn);
  31. f.setBounds(30, 30, 300, 300);
  32. f.setVisible(true);
  33. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. }
  35.  
  36. public class MyDrawPanel extends JPanel{
  37. public void paintComponent(Graphics g) {
  38. Graphics2D g2 = (Graphics2D)g;
  39.  
  40.  
  41. //so bestimmn was gezeichnet wird
  42. if (drawFigureOne) {
  43. XQuadrat quad = new XQuadrat(g);
  44. }
  45.  
  46. /* if (drawFigureTwo) {
  47. int r = (int) (Math.random() * 200);
  48. g.setColor(new Color(r, 0, 0));
  49. g.fillRect(100, 100, 20, 20);
  50. } */
  51.  
  52. }
  53.  
  54. }
  55.  
  56. public class drawBtnListener implements ActionListener {
  57.  
  58. @Override
  59. public void actionPerformed(ActionEvent e) {
  60. m1.repaint();
  61.  
  62. }
  63.  
  64. }
  65. }
  66.  
  67. // ================================================
  68. // deine figur klassen:
  69. import java.awt.Color;
  70. import java.awt.Graphics;
  71. import java.awt.Graphics2D;
  72.  
  73. public class XQuadrat {
  74.  
  75. public XQuadrat(Graphics g) {
  76. int r = (int) (Math.random() * 200);
  77. g.setColor(new Color(r, 0, 0));
  78. g.fillRect(100, 100, 20, 20);
  79. }
  80.  
  81. }
Add Comment
Please, Sign In to add comment