Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class FunnyThings {
- static JFrame f = new JFrame("irgendein cooler titel");
- static MyDrawPanel m1;
- //hier bestimmen was gezeichnet wird
- public boolean drawFigureOne = true;
- public boolean drawFigureTwo = true;
- public static void main(String[] args) {
- new FunnyThings().drawGui();
- }
- public void drawGui() {
- m1 = new MyDrawPanel();
- JButton drawBtn = new JButton("Zeichnen");
- drawBtn.addActionListener(new drawBtnListener());
- f.setContentPane(m1);
- f.getContentPane().add(drawBtn);
- f.setBounds(30, 30, 300, 300);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public class MyDrawPanel extends JPanel{
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D)g;
- //so bestimmn was gezeichnet wird
- if (drawFigureOne) {
- XQuadrat quad = new XQuadrat(g);
- }
- /* if (drawFigureTwo) {
- int r = (int) (Math.random() * 200);
- g.setColor(new Color(r, 0, 0));
- g.fillRect(100, 100, 20, 20);
- } */
- }
- }
- public class drawBtnListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- m1.repaint();
- }
- }
- }
- // ================================================
- // deine figur klassen:
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- public class XQuadrat {
- public XQuadrat(Graphics g) {
- int r = (int) (Math.random() * 200);
- g.setColor(new Color(r, 0, 0));
- g.fillRect(100, 100, 20, 20);
- }
- }
Add Comment
Please, Sign In to add comment