Just started messing around with GUI. I want this program to draw rectangles/circles (depending on which the user clicks on) and keep drawing the same shape inside the previous one until it reaches the amount the user puts into the text field. When I run it, it never seems to make it to paintComponent. Thanks for any help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ShapePanel extends JPanel implements ActionListener {

  JTextField numOfShapes;
  JButton square, circle, black, red, blue;
  boolean isSquare = true;
  boolean isCircle = false;
  boolean isBlack = true;
  boolean isRed = false;
  boolean isBlue = false;
  int num = 0;

 ShapePanel() {

    setLayout( new BorderLayout());

    //Panel 1
    JPanel p1 = new JPanel();
    square = new JButton("Squares");
    p1.add(square);
    square.addActionListener( this );

    circle = new JButton("Circles");
    p1.add(circle);
    circle.addActionListener( this );

    numOfShapes = new JTextField(15);
    p1.add(numOfShapes);
    //numOfShapes.addKeyListener( this );
    add(p1, BorderLayout.NORTH);



    //Panel 3
    JPanel p3 = new JPanel();
    black = new JButton("Black");
    p3.add(black);
    black.addActionListener( this );

    red = new JButton("Red");
    p3.add(red);
    red.addActionListener( this );

    blue = new JButton("Blue");
    p3.add(blue);
    blue.addActionListener( this );
    add(p3, BorderLayout.SOUTH);
}

@Override
public void actionPerformed(ActionEvent ae) {
    String nOfS = numOfShapes.getText();
    System.out.println("made it to action performed");

    if(ae.getSource() == square){
        isSquare = 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;
    }
}  


class Paint extends JPanel {

    public void paintComponent(Graphics g){
        super.paintComponent(g);

        JPanel p2 = new JPanel();
        int c = 0;//change in size
        int x1 = 20;
        int x2 = 200;
        int y1 = 20;
        int y2 = 200;
        String nOfS = numOfShapes.getText(); //number of shapes to be drawn
        num = Integer.parseInt(nOfS);

        System.out.println("Made it to paint component");

        if (isBlack){
            g.setColor(Color.BLACK);
            isBlack = false;
        }
        if (isRed){
            g.setColor(Color.RED);
            isRed = false;
        }
        if (isBlue){
            g.setColor(Color.BLUE);
            isBlue = false;
        }

        if (isSquare){
            for(int i = 0; i < num; i++){
                x1+=c;
                y1+=c;
                x2-=c;
                y2-=c;
                g.drawRect(x1, y1, x2, y2);
                c+=10;
            }
            isSquare = false;
        }
        if (isCircle){
            for(int i = 0; i < num; i++){
                x1+=c;
                y1+=c;
                x2-=c;
                y2-=c;
                g.drawOval(x1, y1, x2, y2);
                c+=10;
            }
            isCircle = false;
        }

        add(p2, BorderLayout.CENTER);
    }
  }
}


public class lab14 {
  public static void main(String[] args) {
    JFrame application = new JFrame();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setSize(400, 400);
    application.setLocationRelativeTo(null);
    application.setTitle("Shapes");
application.add(new ShapePanel());
    application.setVisible(true);
 }
}
有帮助吗?

解决方案

Recommendations

  • I would rename the Paint JPanel to something else as the Paint class name is already used as one of the Java core classes. Rename it to something else, say DrawPanel.
  • I'd override paintComponent(...) in the DrawPanel class and call the super's method, as you are doing.
  • I'd get rid of any code that changes the object's state or adds components to the GUI from within paintComponet. It should be for painting and painting only.
  • I'd add a DrawPanel instance, say called `drawPanel, to the main JPanel, BorderLayout.CENTER.
  • I'd be sure to add an ActionListener to a JButton only once.
  • In the ActionListener, I'd call repaint() on my drawPanel instance after changing its state.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top