Quadrat, Rechteck zeichnen

Detlef7able

Aktives Mitglied
Guten Tag,

ich soll 2 Flächen zeichnen, ein Quadrat und einen Kreis. Das Quadrat wird gezeichnet aber nicht so wie es sein soll. Nun möchte ich noch einen Kreis dazu zeichnen.
Der Quelltext ist folgender:

Java:
import java.awt.*;
class Test {
public int i=40;

   
    double getCircumference()
    {
    return i*4;
    }
    double getArea(){
        return i*i;
    }
    int getHeight(){
        return i;
    }
    int getWidth(){
        return i;
    }
    public void paint (Graphics g){};

    public static void main (String[]args){
        }
    }


Java:
import java.awt.*;
import java.awt.event.*;

public class AWTFigur extends Panel{
    Test f;
    AWTFigur (Test f)
    {
        this.f=f;
    }
   
    public void paint (Graphics g)
    {
        f.paint(g);
   
    }
    public Dimension getPreferredSize()
    {
        return new Dimension (f.getWidth()+100,f.getHeight()+100);
    }
   
    public static void main (String args[] ){
    Frame F=new Frame();
    F.setLayout(new FlowLayout());
    F.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent we){System.exit(0);}});
    AWTFigur P1=new AWTFigur(new Quadrat(30));
    F.add(P1);
    AWTFigur P2=new AWTFigur(new Circle(50));
    F.add(P2);
    F.pack();
    F.setVisible(true);
    }
}

So nun soll eigentlich statt return new Dimension (f.getWidth()+100,f.getHeight()+100);
return new Dimension (f.getWidth()+2,f.getHeight()+2); stehen. Das hab ich geändert damit ich was sehe.

Außerdem habe ich noch zwei Klassen Quadrat und Kreis. Nun möchte ich gern wissen wie man die Klassen gestalten soll damit ich das Quadrat und den Kreis sehen kann.
 
Ich hatte hier mal ein Swing SketchBoard:
Java:
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {
  public static void main(String... args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new SketchBoard();
      }
    });
  }
  
}

interface Drawable {
  void draw(Graphics g);
}

abstract class Shape implements Drawable {
  protected int x, y;
  
  public Shape(int x, int y) {
    this.x = x;
    this.y = y;
  }
  
  public int getX() {
    return x;
  }
  
  public int getY() {
    return y;
  }
}

class Circle extends Shape {
  
  private int radius;
  
  public Circle(int x, int y, int radius) {
    super(x, y);
    this.radius = radius;
  }
  
  @Override
  public void draw(Graphics g) {
    g.drawOval(x, y, radius, radius);
  }
}

class Rectangle extends Shape {
  
  private int width, height;
  
  public Rectangle(int x, int y, int width, int height) {
    super(x, y);
    this.width = width;
    this.height = height;
  }
  
  @Override
  public void draw(Graphics g) {
    g.drawRect(x, y, width, height);
  }
}

class Square extends Shape {
  
  private int width;
  
  public Square(int x, int y, int width) {
    super(x, y);
    this.width = width;
  }
  
  @Override
  public void draw(Graphics g) {
    g.drawRect(x, y, width, width);
  }
}

class SketchBoard {
  public SketchBoard() {
    JFrame frame = new JFrame("Sketch Board");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    frame.setSize(800, 600);
    frame.add(new SketchBoardCanvas());
    frame.setVisible(true);
  }
}

class SketchBoardCanvas extends JPanel {
  private static final long serialVersionUID = -841872441222336999L;
  List<Drawable> drawables = new ArrayList<>();
  
  public SketchBoardCanvas() {
    addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        
        Drawable drawable;
        if (SwingUtilities.isLeftMouseButton(e)) {
          drawable = new Circle(e.getX(), e.getY(), 30);
        } else if (SwingUtilities.isMiddleMouseButton(e)) {
          drawable = new Square(e.getX(), e.getY(), 50);
        } else {
          drawable = new Rectangle(e.getX(), e.getY(), 20, 40);
        }
        drawables.add(drawable);
        repaint();
      }
    });
  }
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Drawable drawable : drawables) {
      drawable.draw(g);
    }
  }
}

Musst eben statt paintComponents eben paint überschreiben. Rest ist analog dazu.
 

Zurück
Oben