简体   繁体   中英

Can't change the output of paintComponent after assigned new variable (setter) and called repaint()

I was stucked in the same place for almost 1 day still I can't find where's the error on my code. Need your guys to help, this is my code. (Quite a bit lengthy)

My JFrame class:

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

public class CircleProject extends JFrame implements ActionListener{
    JTextField xTextField1;
    JTextField yTextField1;
    JTextField textFieldRadiusLeft;
    JTextField xTextField2;
    JTextField yTextField2;
    JTextField textFieldRadiusRight;
    JButton redraw;

public static void main(String[] args) {
    CircleProject frame = new CircleProject();
    frame.setResizable(true);
    frame.setVisible(true);
    frame.setSize(580, 700);
    frame.setTitle("YONG JING PING FINAL PROJECT");
}

public CircleProject() {
    JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JLabel theTitle = new JLabel("Two Circles Intersect?");
    JLabel intersection = new JLabel("");
    titlePanel.add(theTitle);
    titlePanel.add(intersection);

    JPanel contentPanel = new JPanel(new GridLayout(2, 1));
    JPanel userInputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JPanel gridPanel = new JPanel(new GridLayout(1, 2, 8, 0));

    JPanel leftInputPanel = new JPanel(new GridLayout(4, 1));
    JLabel inputTitle1 = new JLabel("Enter circle 1 info: ");
    inputTitle1.setHorizontalAlignment(JLabel.CENTER);

    JPanel xInputPanelLeft = new JPanel();
    xTextField1 = new JTextField(5);
    xInputPanelLeft.add(new JLabel("Center X: "));
    xInputPanelLeft.add(xTextField1);

    JPanel yInputPanelLeft = new JPanel();
    yTextField1 = new JTextField(5);
    yInputPanelLeft.add(new JLabel("Center Y: "));
    yInputPanelLeft.add(yTextField1);

    JPanel radiusLeft = new JPanel();
    textFieldRadiusLeft = new JTextField(5);
    radiusLeft.add(new JLabel("Radius: "));
    radiusLeft.add(textFieldRadiusLeft);

    leftInputPanel.add(inputTitle1);
    leftInputPanel.add(xInputPanelLeft);
    leftInputPanel.add(yInputPanelLeft);
    leftInputPanel.add(radiusLeft);
    leftInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JPanel rightInputPanel = new JPanel(new GridLayout(4, 1));
    JLabel inputTitle2 = new JLabel("Enter circle 2 info: ");
    inputTitle2.setHorizontalAlignment(JLabel.CENTER);

    JPanel xInputPanelRight = new JPanel();
    xTextField2 = new JTextField(5);

    xInputPanelRight.add(new JLabel("Center X: "));
    xInputPanelRight.add(xTextField2);

    JPanel yInputPanelRight = new JPanel();
    yTextField2 = new JTextField(5);
    yInputPanelRight.add(new JLabel("Center Y: "));
    yInputPanelRight.add(yTextField2);

    JPanel radiusRight = new JPanel();
    textFieldRadiusRight = new JTextField(5);
    radiusRight.add(new JLabel("Radius: "));
    radiusRight.add(textFieldRadiusRight);

    rightInputPanel.add(inputTitle2);
    rightInputPanel.add(xInputPanelRight);
    rightInputPanel.add(yInputPanelRight);
    rightInputPanel.add(radiusRight);
    rightInputPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    gridPanel.add(leftInputPanel);
    gridPanel.add(rightInputPanel);
    userInputPanel.add(gridPanel);
    contentPanel.add(new DrawPanel());
    contentPanel.add(userInputPanel);

    redraw = new JButton("Redraw Circles");
    redraw.setEnabled(true);
    redraw.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(redraw);

    setLayout(new BorderLayout());
    add(titlePanel, BorderLayout.NORTH);
    add(contentPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

}

DrawPanel drawCircle = new DrawPanel();

@Override
public void actionPerformed(ActionEvent e) {
    this.setVisible(false);
    if (e.getSource() == redraw) {
        CircleProject frame2 = new CircleProject();
        frame2.setResizable(false);
        frame2.setVisible(true);
        frame2.setSize(580,700);
        frame2.setTitle("YONG JING PING FINAL PROJECT");


        String text = xTextField1.getText();
        int x1 = Integer.parseInt(text);

        String text1 = xTextField2.getText();
        int x2 = Integer.parseInt(text1);

        String text2 = yTextField1.getText();
        int y1 = Integer.parseInt(text2);

        String text3 = yTextField2.getText();
        int y2 = Integer.parseInt(text3);

        String text4 = textFieldRadiusLeft.getText();
        int r1 = Integer.parseInt(text4);

        String text5 = textFieldRadiusRight.getText();
        int r2 = Integer.parseInt(text5);

        drawCircle.setCenterColumn1(x1, y1, r1);
        drawCircle.setCenterColumn2(x2, y2, r2);
        }
    }
}

My Drawpanel Class:

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

public class DrawPanel extends JPanel{
    //    the coordinate's variables is the start point of it.
    int coordinateX1=50, coordinateX2=300, coordinateY1=50, coordinateY2=50, radius1=0, radius2=0, length1=200, length2=200;

public void setCenterColumn1(int x, int y, int radius1){
    this.coordinateX1 = x-radius1;
    this.coordinateY1 = y-radius1;
    this.radius1 = radius1;
    length1 = radius1*2;
    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------1-------");
    repaint();
}
public void setCenterColumn2(int x, int y, int radius2){
    this.coordinateX2=x-radius2;
    this.coordinateY2=y-radius2;
    this.radius2=radius2;
    length2 = radius2*2;
    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------2-------");
    repaint();
}

public DrawPanel(){
    setBackground(Color.WHITE);
}

//    error from here, when redraw pressed this paintComponent does not
//    show up the new circle.
    @Override
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);

//        Circle for column 1
        g.drawOval(coordinateX1,coordinateY1,length1,length1);

//        Circle for column 2
        g.drawOval(coordinateX2,coordinateY2,length2,length2);

    System.out.println(coordinateX1);
    System.out.println(coordinateY1);
    System.out.println(length1);
    System.out.println(coordinateX2);
    System.out.println(coordinateY2);
    System.out.println(length2);
    System.out.println("-------3-------");
    }
}

My expected result is the circle will be changed after the user input the centre coordinate of the circle and clicked redraw btn.

The problem is that you are not using never your variable drawCircle (line: DrawPanel drawCircle = new DrawPanel();)

And when you use inside actionPerformed method, you never use that variable because it's created another instance of Circle Project. (line: CircleProject frame2 = new CircleProject();)

The drawCircle variable is from the current class you are running,so, when you instance again CircleProject, the changes inside that variabe will not have effect.

To fix that, it's not necessary to instance again, you only must use that variable.

The first thing you will have to do inside the Constructor CircleProject() :

....
userInputPanel.add(gridPanel);
contentPanel.add(drawCircle);
contentPanel.add(userInputPanel);
....

The change here was only one, because you have contentPanel.add(new DrawPanel()); and that's not correct (as I said, you never are using the variable drawCircle ), so, instead, you have to change it for contentPanel.add(drawCircle);

On the other hand, to avoid instancing the CircleProject you will have to remove some lines in the actionPerformed method, like:

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == redraw) {
  
    String text = xTextField1.getText();
    int x1 = Integer.parseInt(text);

    String text1 = xTextField2.getText();
    int x2 = Integer.parseInt(text1);

    String text2 = yTextField1.getText();
    int y1 = Integer.parseInt(text2);

    String text3 = yTextField2.getText();
    int y2 = Integer.parseInt(text3);

    String text4 = textFieldRadiusLeft.getText();
    int r1 = Integer.parseInt(text4);

    String text5 = textFieldRadiusRight.getText();
    int r2 = Integer.parseInt(text5);

    drawCircle.setCenterColumn1(x1, y1, r1);
    drawCircle.setCenterColumn2(x2, y2, r2);
    }
}

I tried the program and it worked for me: I hope this also works for you :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM