swing - Trying to draw rectangles/ovals after user selects what color and shape to be drawn in Java -


Just started to wander with the GUI I want to draw rectangles / circles to this program (which the user clicks on) and Maintaining the same size in the previous one until the user accesses the amount entered in the text field. When I run it, it seems that it never seems to color it. Thanks for any help.

  import javax.swing. *; Import java.awt. *; Import java.awt.event. *; ClassLocal Applicability of ActionListener {JTextField numOfShapes; Pinkton square, circle, black, red, blue; Boolean is Square = true; Boolean's cycle = false; Boolean is Black = true; Boolean isRed = Wrong; Boolean is blue = false; Int num = 0; SizePanel () {setLayout (new border layout ()); // Panel 1 JPNL P1 = new zpn (); Class = new pocket ("square"); P1.add (square); Square.addActionListener (this); Circle = new pocket ("circles"); P1.add (cycle); Circle.addActionListener (this); NumOfShapes = new JTextField (15); P1.add (numOfShapes); //numOfShapes.addKeyListener (this); Joint (P1, Border Layout. North); // Panel 3 JPNL P3 = new JPNL (); Black = new pocket ("black"); p3.add (black); Black.addActionListener (this); Red = new pocket ("red"); p3.add (red); Red.addActionListener (this); Blue = new pocket ("blue"); P3.add (blue); Blue.addActionListener (this); Add (P3, Border Layout. South); } @ Override Public Zero Action Perfid (ActionAge AE) {String nOfS = numOfShapes.getText (); System.out.println ("Made to take action"); If (ae.getSource () == class) {hequire = true; } If (ae.getSource () == circle) {isCircle = true; } If (ae.getSource () == black) {isBlack = true; } If (ae.getSource () == Red) {isRed = true; } If (ae.getSource () == blue) {isBlue = true; }} Square Paint JPanel {Public Zero Paint (Main Graphics) {super.paintComponent (G); Jpnell p2 = new zpn (); Int c = 0; // change in size int x1 = 20; int x2 = 200; Int y1 = 20; Int y2 = 200; String nOfS = numOfShapes.getText (); // num = integer.parseInt (nOfS) should be prepared for the number of shapes; System.out.println ("This is designed to color the component"); If (black) {g.setColor (Color.BLACK); isBlack = false; } If (IRDEED) {G. Seat color (color .rd); isRed = false; } If (blue) {g.setColor (Color.BLUE); isBlue = false; } If issquare {for (int i = 0; i & lt; num; i ++) {x1 + = c; y1 + = c; x2- = c; Y2- = c; G.drawRect (x1, y1, x2, y2); C + = 10; } IsSquare = false; } If (iChral) {for (int i = 0; i    

Recommendations
  • I will rename it The name of the paint class is already being used as one of the Java core classes, the JPanel color to something else. DrawPanel says, give it a few more names.
  • I can override the paintComponent (...) in DrawPanel class and call the method of super, as you are doing.
  • I can get rid of any code that changes the position of the object or connects the component inside with paintComponet . It should be only for painting and painting.
  • I will add a droplet example called 'drop panel', main zennial, border layout. Cxair
  • I'm sure to add a pocket button only once to an ActionListener.
  • In the ActionListener, after I change my state, I call repaint () on my drawPanel example.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -