Results 1 to 2 of 2
- 11-23-2010, 12:53 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
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:
Java 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); } }
Java 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
- 11-23-2010, 01:53 PM #2
If you have a specific question, you should provide an SSCCE that deals only with that question. I'm not sure how useful all that extra code is, and honestly it probably causes people to just not read your post.
Are you trying to toggle the squares in the grid as you click them?
Similar Threads
-
Draw rectangle by dragging mouse
By Tyler in forum SWT / JFaceReplies: 11Last Post: 08-09-2010, 07:50 PM -
change color on mouse click
By hannerz06 in forum New To JavaReplies: 3Last Post: 03-31-2010, 09:46 PM -
how to draw a fill rectangle using mouse and paintComponent?
By java_fun2007 in forum New To JavaReplies: 7Last Post: 04-14-2009, 07:12 PM -
change object color on mouse click
By gotenks05 in forum Java AppletsReplies: 1Last Post: 04-05-2009, 07:14 PM -
How to color a rectangle throught JColorChooser?
By aRTx in forum New To JavaReplies: 1Last Post: 03-31-2009, 03:15 AM


LinkBack URL
About LinkBacks

Bookmarks