Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by Chris ( 16 years ago )
import java.awt.EventQueue;

@SuppressWarnings("serial")
class MeinCanvas extends Canvas {
 /**
  * Paint-Methode von Canvas überschreiben
  */
 private int breite;
 private int hoehe;
 private int FieldX;
 private int FieldY;
 
 /**
  * Die Paint-Methode von Graphics überschreiben,
  * setzt die Objekte auf das Canvas und manipuliert diese
  */
 public void paint(Graphics g) {
  super.paint(g);
  this.setBreite(breite);
  this.setHoehe(hoehe);
 
     for(int i = 0; i < hoehe; i++) {
      for(int j = 0; j < breite; j++) {
       g.drawRect(15 * j, 15 * i, 15, 15);
      }
     }    
 }
 
 public void paint1(Graphics g) {
  super.paint(g);
  this.setFieldX(FieldX);
  this.setFieldY(FieldY);
  
  g.setColor(Color.RED);
  g.drawOval(15 * FieldX, 15 * FieldY, 15, 15);
  
 }
 
    public void set(int x, int y) {
     setBreite(x);
     setHoehe(y);
    }

 public void setBreite(int breite) {
  this.breite = breite;
 }

 public void setHoehe(int hoehe) {
  this.hoehe = hoehe;
 }
 
 public void setField(int x, int y) {
  setFieldX(x);
  setFieldY(y);
 }

 private void setFieldX(int x) {
  this.FieldX = x;
 }
 
 private void setFieldY(int y) {
  this.FieldY = y;
 }

}

public class buschfeuer {

 private JFrame frmBuschfeuersimulation;
 private JTextField tfstatus;
 private JTextField tfbreite;
 private JTextField tfhoehe;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     buschfeuer window = new buschfeuer();
     window.frmBuschfeuersimulation.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public buschfeuer() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private void initialize() {
  frmBuschfeuersimulation = new JFrame();
  frmBuschfeuersimulation.setTitle("Buschfeuer-Simulation");
  frmBuschfeuersimulation.setBounds(100, 100, 618, 524);
  frmBuschfeuersimulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frmBuschfeuersimulation.getContentPane().setLayout(null);

     final MeinCanvas Matrix = new MeinCanvas();
  Matrix.setBackground(Color.WHITE);
  Matrix.setBounds(10, 10, 380, 442);
  frmBuschfeuersimulation.getContentPane().add(Matrix);
  
  /**
   * Stellt Mausposition fest und zeichnet entsprechendes Feld neu
   */
        Matrix.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
    int x = (Integer.parseInt(e.getX() + "") / 15);
    int y = (Integer.parseInt(e.getY() + "") / 15);
    System.out.print("Click Kästchen (X, Y):  " + x + ", " + y);

    Matrix.setField(x, y);
    //Matrix.repaint();
   }
  });
  
  tfstatus = new JTextField();
  tfstatus.setEditable(false);
  tfstatus.setBounds(10, 458, 380, 20);
  frmBuschfeuersimulation.getContentPane().add(tfstatus);
  tfstatus.setColumns(10);
  
  tfbreite = new JTextField();
  tfbreite.setText("25");
  tfbreite.setBounds(409, 56, 183, 20);
  frmBuschfeuersimulation.getContentPane().add(tfbreite);
  tfbreite.setColumns(10);
  
  JLabel lblBreite = new JLabel("Breite");
  lblBreite.setBounds(409, 31, 46, 14);
  frmBuschfeuersimulation.getContentPane().add(lblBreite);
  
  tfhoehe = new JTextField();
  tfhoehe.setText("29");
  tfhoehe.setBounds(409, 112, 183, 20);
  frmBuschfeuersimulation.getContentPane().add(tfhoehe);
  tfhoehe.setColumns(10);
  
  JLabel lblHhe = new JLabel("Hu00F6he");
  lblHhe.setBounds(409, 87, 46, 14);
  frmBuschfeuersimulation.getContentPane().add(lblHhe);
  
  JButton bstarten = new JButton("Simulation starten");
  bstarten.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
    boolean fehler = false;
    try {
          Integer.parseInt(tfbreite.getText());
          Integer.parseInt(tfhoehe.getText());
        } catch(Exception ex) { 
          tfstatus.setText("Bitte nur Zahlen eingeben!");
          fehler = true;
        }
       
       if(fehler == false) {
     //Statusleiste aktualisieren
     tfstatus.setText("Breite " + tfbreite.getText() + " * Höhe " + tfhoehe.getText() + " = " + 
       (Integer.parseInt(tfbreite.getText()) * Integer.parseInt(tfhoehe.getText())) + " Felder");
       
       
       Matrix.repaint();
       Matrix.set(Integer.parseInt(tfbreite.getText()), Integer.parseInt(tfhoehe.getText()));
       
       }
   }
  });
  
  bstarten.setBounds(437, 452, 155, 23);
  frmBuschfeuersimulation.getContentPane().add(bstarten);

 }

}

 

Revise this Paste

Your Name: Code Language: