Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // die Figur
- import java.awt.Color;
- import java.awt.Graphics;
- public abstract class Figure {
- public int xValue;
- public int yValue;
- public Graphics g;
- //usw
- public Figure(int xValue, int yValue, Graphics g) {
- this.xValue = xValue;
- this.yValue = yValue;
- this.g = g;
- }
- // falls man keine werte eingeben will das standard polygon/quadrat ..
- public Figure(Graphics g) {
- this.xValue = 10;
- this.yValue = 10;
- this.g = g;
- }
- abstract void draw(); // muss bei jeder vererbung überschrieben werden
- }
- // das quadrat
- public class Quadrat extends Figure {
- //hier vielleicht noch den konstruktor überschreiben falls man mehr oder
- // weniger daten braucht um die figur zu beschreiben
- public Quadrat(Graphics g) {
- super(g);
- }
- @Override
- void draw() {
- int r = (int) (Math.random() * 200);
- g.setColor(new Color(r, 0, 0));
- g.fillRect(this.xValue, this.yValue, 20, 20); // die werte der superklasse
- }
- }
- // das gui
- public class FunnyThings {
- private JFrame f = new JFrame("irgendein cooler titel");
- private 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) {
- if (drawFigureOne) {
- Quadrat quad = new Quadrat(g);
- quad.draw();
- }
- }
- }
- public class drawBtnListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- m1.repaint();
- }
- }
- }
Add Comment
Please, Sign In to add comment