简体   繁体   English

分配新变量(setter)并调用repaint()后,无法更改paintComponent的output

[英]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.我被困在同一个地方将近 1 天,但我仍然找不到我的代码错误在哪里。 Need your guys to help, this is my code.需要你们的帮助,这是我的代码。 (Quite a bit lengthy) (有点长)

My JFrame class:我的 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:我的绘图板 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.我的预期结果是用户输入圆的中心坐标并单击重绘 btn 后,圆将发生变化。

The problem is that you are not using never your variable drawCircle (line: DrawPanel drawCircle = new DrawPanel();)问题是你没有使用你的变量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.当您使用内部actionPerformed方法时,您永远不会使用该变量,因为它创建了 Circle Project 的另一个实例。 (line: CircleProject frame2 = new CircleProject();) (行: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. drawCircle变量来自您正在运行的当前 class,因此,当您再次实例化 CircleProject 时,该变量内部的更改将不起作用。

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() :您必须在构造函数CircleProject()中做的第一件事:

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

The change here was only one, because you have contentPanel.add(new DrawPanel());这里的变化只有一个,因为你有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);这是不正确的(正如我所说,您从未使用过变量drawCircle ),因此,您必须将其更改为contentPanel.add(drawCircle);

On the other hand, to avoid instancing the CircleProject you will have to remove some lines in the actionPerformed method, like:另一方面,为避免实例化CircleProject ,您必须删除actionPerformed方法中的一些行,例如:

@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我试过这个程序,它对我有用:我希望这也对你有用:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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