Mouse Click changes Color of Rectangle
Hey,
I have written a code that generates a matrix on a Panel.
You can choose the numbers of rows and columns, if you enter them to a Textfield:
Code:
import burn.DrawPanel;
import burn.Matrixframe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BushGui extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String args[]){
Matrixframe F = new Matrixframe ("Matrizen zeichnen");
F.setVisible(true);
}
}
class Matrixframe extends Frame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final Object[] RECT = null;
DrawPanel matrizen = new DrawPanel(0,0);
Panel p1 = new Panel();
Label hintergrund = new Label();
Choice form = new Choice();
TextField zeile = new TextField();
TextField spalte = new TextField();
Label lStatus = new Label();
Color exa = Color.LIGHT_GRAY;
//MouseListener
public Matrixframe(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent m){
int x = m.getX();
int y = m.getY();
if (((JComponent) RECT[0]).contains(x,y)){
Color exa = Color.green;
repaint();
}
}
});
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
paint(g);
}
//Eigenschaften der Oberfläche, inkl. Closing Adapter
public Matrixframe (String T)
{
this.setTitle(T);
this.setSize(800,600);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
int rows = 0;
int cols = 0;
String temp = zeile.getText();
try {
rows = Integer.parseInt(temp);
if (rows < 0) {
updateStatus ("Negative Zeilenwerte sind nicht möglich");
}
} catch (Exception ex) {
updateStatus(temp + " ist kein gültiger Zeilenwert");
}
temp = spalte.getText();
try {
cols = Integer.parseInt(temp);
if (cols < 0) {
}
} catch (Exception ex) {
updateStatus ("Negative Spaltenwerte sind nicht möglich");
updateStatus(temp + " ist kein gültiger Spaltenwert");
}
String toDraw = form.getSelectedItem();
matrizen.setRows(rows);
matrizen.setCols(cols);
if (toDraw.equals("Rechteck")) {
// draw rectangle
matrizen.setShape(DrawPanel.myShape.RECT);
} else if (toDraw.equals("Kreis")) {
// draw circle
matrizen.setShape(DrawPanel.myShape.CIRC);
} else {
}
matrizen.repaint();
updateStatus("Zeichne Matrix mit " + rows + " Zeilen und " + cols + " Spalten.");
}
private void updateStatus(String text) {
// output to status field
lStatus.setText(text);
}
{
// Eigenschaften der Objekte des Frames
setBackground(Color.LIGHT_GRAY);
add (matrizen);
matrizen.setBounds(25,75,500,500); //(links & rechts, oben&unten)
// Choice Fenster für die Form der Matrix
form.setBackground(Color.white);
add (form);
form.setBounds(550,75,150,50);
form.addItem("Rechteck");
//form.addItem("Kreis");
//form.addItem("Dreieck");
//Form Eigenschaften Ende
//Überschrift "Zeile"
Label Zeile = new Label ("Zeile(n):");
add (Zeile);
Zeile.setBounds(550, 180, 150, 20);
//Eiegnschaften Textfeld "Zeile"
zeile.setBackground(Color.white);
add (zeile);
zeile.setBounds(550, 200, 150, 20);
//Überschrift "Spalte"
Label Spalte = new Label("Spalte(n):");
add (Spalte);
Spalte.setBounds(550,230,150,20);
//Spalte.setBackground(Color.white);
//Eigenschaften Textfeld "Spalte"
add (spalte);
spalte.setBounds(550, 250, 150,20);
//Einfügen des Buttons "Start"
Button draw = new Button ("Zeichnen !");
draw.addActionListener(this);
add (draw);
draw.setBounds(550, 400,150,30);
Button feuer = new Button ("Feuer");
draw.addActionListener(this);
add (feuer);
feuer.setBounds(550,450, 150, 30);
lStatus.setText("Statusfeld");
lStatus.setBounds(25, 580, 500, 10);
add(lStatus);
add(hintergrund);
add(p1);
}
}
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public enum myShape{RECT, CIRC};
private int m_cols;
private int m_rows;
private myShape m_shape;
public DrawPanel(int rows, int cols) {
m_rows = rows;
m_cols = cols;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r = 0; r < m_rows; ++r) {
for (int c = 0; c < m_cols; ++c) {
switch(m_shape) {
case RECT: g.drawRect(c*15+5, r*15+5, 10, 10);
break;
case CIRC: g.drawOval(c*15+5, r*15+5, 10, 10);
break;
}
}
}
}
public void setCols(int cols) {
m_cols = cols;
}
public void setRows(int rows) {
m_rows = rows;
}
public void setShape(myShape s) {
m_shape = s;
}
}
Now I want to change the color of one rectangle through a Mouseclick.
How can I implement this? I know, that I have to use the MouseListener/ Adapter, but I don't know how to fill up one of this rectangles I made with the code.
I appreciate helpfull links or hints.
Thank you!
Greetings from germany
Doc