我的方法被调用,但它不画线
My method is called, but it does not draw the lines
下面是我绘制游泳池并同时计算其体积的代码。
有问题的方法是 paintSwimmingPool(),它在被 stateChanged() 侦听器方法调用时工作正常。但是,问题是我想在启动程序时绘制矩形。我添加了一些控制台消息来帮助调试,它告诉我该方法是从 createGUI() 方法正确调用的。但为什么 paintSwimmingPool() 不在面板上绘制任何东西?抱歉,我花了很多时间来解决这个问题,但无处可去。
提前感谢您的指点。
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwimingPoolCalculator implements ChangeListener {
private JFrame frame;
private JPanel primaryJPanel, drawingJPanel;
private JLabel deepJLabel, shallowJLabel;
private JSlider deepJSlider, shallJSlider;
private JTextField resultTextField;
final private int xPos = 20;
final private int yPos = 20;
final private int swimmingPoolLength = 200;
final private int swimmingPoolWidth = 50;
public static void main(String[] args) {
SwimingPoolCalculator swimingPoolCalculator = new SwimingPoolCalculator();
swimingPoolCalculator.createGUI();
}
private void createGUI(){
frame = new JFrame("Swiming Pool Calculator");
primaryJPanel = new JPanel();
primaryJPanel.setPreferredSize(new Dimension(250, 500));
drawingJPanel = new JPanel();
drawingJPanel.setPreferredSize(new Dimension(250, 300));
drawingJPanel.setBackground(Color.yellow);
shallowJLabel = new JLabel("S End");
deepJLabel = new JLabel("D End");
shallJSlider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
deepJSlider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
shallJSlider.addChangeListener(this);
deepJSlider.addChangeListener(this);
resultTextField = new JTextField(15);
primaryJPanel.add(drawingJPanel);
primaryJPanel.add(shallowJLabel);
primaryJPanel.add(shallJSlider);
primaryJPanel.add(deepJLabel);
primaryJPanel.add(deepJSlider);
primaryJPanel.add(resultTextField);
frame.getContentPane().add(primaryJPanel);
frame.pack();
frame.setVisible(true);
// why the first call does not draw any line?
paintSwimmingPool(deepJSlider.getValue(), shallJSlider.getValue());
}
private void paintSwimmingPool(int deepEnd, int shallowEnd){
Graphics drawingBoard = drawingJPanel.getGraphics();
drawingBoard.setColor(Color.yellow);
drawingBoard.fillRect(0, 0, 250, 250);
drawingBoard.setColor(Color.black);
drawingBoard.drawLine(xPos, yPos, xPos + swimmingPoolLength, yPos); // the top line
drawingBoard.drawLine(xPos, yPos, xPos, yPos + deepEnd); // the left line
drawingBoard.drawLine(xPos + swimmingPoolLength, yPos, xPos + swimmingPoolLength, yPos + shallowEnd); // the right line
drawingBoard.drawLine(xPos, yPos + deepEnd, xPos + swimmingPoolLength, yPos + shallowEnd); // the bottom line
}
private void calculateVolume(double averageDepth){
int swimmingPoolVolume = (int)(averageDepth * swimmingPoolLength * swimmingPoolWidth);
resultTextField.setText("Volume: " + swimmingPoolVolume);
}
@Override
public void stateChanged(ChangeEvent e) {
int shallowEnd = shallJSlider.getValue();
int deepEnd = deepJSlider.getValue();
double averageDepth = (shallowEnd + deepEnd) / 2;
paintSwimmingPool(deepEnd, shallowEnd);
calculateVolume(averageDepth);
}
}
Swing 可能在您绘制之后重新绘制 JPanel。 Swing 可以随时重绘组件,无论出于何种原因。因此,您不应使用 getGraphics
.
相反,您需要创建自己的组件 class(扩展现有组件,例如 JPanel
),并覆盖 paintComponent
.
有关详细信息,请参阅 this question。
不能这样画,正确的画法是
- 重写 JPanel subclass 中的 paintComponent() 方法并改为使用 Graphics 参数。
- 调用 repaint() 来触发重绘而不是您的 paintSwimmingPool() 方法
- 将参数,即 deepEnd 和 shallowEnd 移动到字段中,并从您的 paintComponent() 方法中引用它们 - 内部 class 我们能够 'see' 这些字段。理想情况下,这些将位于绘图面板和控制代码共享的单独模型中,但内部 class 完成了工作。
- 看看oracle tutorial on custom painting
完整的例子
public class SwimingPoolCalculator implements ChangeListener {
private JFrame frame;
private JPanel primaryJPanel, drawingJPanel;
private JLabel deepJLabel, shallowJLabel;
private JSlider deepJSlider, shallJSlider;
private JTextField resultTextField;
final private int xPos = 20;
final private int yPos = 20;
final private int swimmingPoolLength = 200;
final private int swimmingPoolWidth = 50;
private int deepEnd; // <== moved to fields
private int shallowEnd; // <== moved to fields
private class InnerDrawingPanel extends JPanel {
@Override
protected void paintComponent(Graphics drawingBoard) {
drawingBoard.setColor(Color.yellow); // <== drawing code now here
drawingBoard.fillRect(0, 0, 250, 250);
drawingBoard.setColor(Color.black);
drawingBoard.drawLine(xPos, yPos, xPos + swimmingPoolLength, yPos); // the top line
drawingBoard.drawLine(xPos, yPos, xPos, yPos + deepEnd); // the left line
drawingBoard.drawLine(xPos + swimmingPoolLength, yPos, xPos + swimmingPoolLength, yPos + shallowEnd); // the right line
drawingBoard.drawLine(xPos, yPos + deepEnd, xPos + swimmingPoolLength, yPos + shallowEnd); // the bottom line
}
}
public static void main(String[] args) {
SwimingPoolCalculator swimingPoolCalculator = new SwimingPoolCalculator();
swimingPoolCalculator.createGUI();
}
private void createGUI(){
frame = new JFrame("Swiming Pool Calculator");
primaryJPanel = new JPanel();
primaryJPanel.setPreferredSize(new Dimension(250, 500));
drawingJPanel = new InnerDrawingPanel(); // <== use inner class
drawingJPanel.setPreferredSize(new Dimension(250, 300));
drawingJPanel.setBackground(Color.yellow);
shallowJLabel = new JLabel("S End");
deepJLabel = new JLabel("D End");
shallJSlider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
deepJSlider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
shallJSlider.addChangeListener(this);
deepJSlider.addChangeListener(this);
resultTextField = new JTextField(15);
primaryJPanel.add(drawingJPanel);
primaryJPanel.add(shallowJLabel);
primaryJPanel.add(shallJSlider);
primaryJPanel.add(deepJLabel);
primaryJPanel.add(deepJSlider);
primaryJPanel.add(resultTextField);
frame.getContentPane().add(primaryJPanel);
frame.pack();
frame.setVisible(true);
// why the first call does not draw any line?
shallowEnd = deepJSlider.getValue(); // <== now update fields
deepEnd = shallJSlider.getValue();
drawingJPanel.repaint(); // <== trigger repaint
}
private void calculateVolume(double averageDepth){
int swimmingPoolVolume = (int)(averageDepth * swimmingPoolLength * swimmingPoolWidth);
resultTextField.setText("Volume: " + swimmingPoolVolume);
}
@Override
public void stateChanged(ChangeEvent e) {
shallowEnd = shallJSlider.getValue(); // <== update fields
deepEnd = deepJSlider.getValue();
double averageDepth = (shallowEnd + deepEnd) / 2;
drawingJPanel.repaint(); // <== trigger repaint
calculateVolume(averageDepth);
}
}
下面是我绘制游泳池并同时计算其体积的代码。
有问题的方法是 paintSwimmingPool(),它在被 stateChanged() 侦听器方法调用时工作正常。但是,问题是我想在启动程序时绘制矩形。我添加了一些控制台消息来帮助调试,它告诉我该方法是从 createGUI() 方法正确调用的。但为什么 paintSwimmingPool() 不在面板上绘制任何东西?抱歉,我花了很多时间来解决这个问题,但无处可去。
提前感谢您的指点。
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SwimingPoolCalculator implements ChangeListener {
private JFrame frame;
private JPanel primaryJPanel, drawingJPanel;
private JLabel deepJLabel, shallowJLabel;
private JSlider deepJSlider, shallJSlider;
private JTextField resultTextField;
final private int xPos = 20;
final private int yPos = 20;
final private int swimmingPoolLength = 200;
final private int swimmingPoolWidth = 50;
public static void main(String[] args) {
SwimingPoolCalculator swimingPoolCalculator = new SwimingPoolCalculator();
swimingPoolCalculator.createGUI();
}
private void createGUI(){
frame = new JFrame("Swiming Pool Calculator");
primaryJPanel = new JPanel();
primaryJPanel.setPreferredSize(new Dimension(250, 500));
drawingJPanel = new JPanel();
drawingJPanel.setPreferredSize(new Dimension(250, 300));
drawingJPanel.setBackground(Color.yellow);
shallowJLabel = new JLabel("S End");
deepJLabel = new JLabel("D End");
shallJSlider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
deepJSlider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
shallJSlider.addChangeListener(this);
deepJSlider.addChangeListener(this);
resultTextField = new JTextField(15);
primaryJPanel.add(drawingJPanel);
primaryJPanel.add(shallowJLabel);
primaryJPanel.add(shallJSlider);
primaryJPanel.add(deepJLabel);
primaryJPanel.add(deepJSlider);
primaryJPanel.add(resultTextField);
frame.getContentPane().add(primaryJPanel);
frame.pack();
frame.setVisible(true);
// why the first call does not draw any line?
paintSwimmingPool(deepJSlider.getValue(), shallJSlider.getValue());
}
private void paintSwimmingPool(int deepEnd, int shallowEnd){
Graphics drawingBoard = drawingJPanel.getGraphics();
drawingBoard.setColor(Color.yellow);
drawingBoard.fillRect(0, 0, 250, 250);
drawingBoard.setColor(Color.black);
drawingBoard.drawLine(xPos, yPos, xPos + swimmingPoolLength, yPos); // the top line
drawingBoard.drawLine(xPos, yPos, xPos, yPos + deepEnd); // the left line
drawingBoard.drawLine(xPos + swimmingPoolLength, yPos, xPos + swimmingPoolLength, yPos + shallowEnd); // the right line
drawingBoard.drawLine(xPos, yPos + deepEnd, xPos + swimmingPoolLength, yPos + shallowEnd); // the bottom line
}
private void calculateVolume(double averageDepth){
int swimmingPoolVolume = (int)(averageDepth * swimmingPoolLength * swimmingPoolWidth);
resultTextField.setText("Volume: " + swimmingPoolVolume);
}
@Override
public void stateChanged(ChangeEvent e) {
int shallowEnd = shallJSlider.getValue();
int deepEnd = deepJSlider.getValue();
double averageDepth = (shallowEnd + deepEnd) / 2;
paintSwimmingPool(deepEnd, shallowEnd);
calculateVolume(averageDepth);
}
}
Swing 可能在您绘制之后重新绘制 JPanel。 Swing 可以随时重绘组件,无论出于何种原因。因此,您不应使用 getGraphics
.
相反,您需要创建自己的组件 class(扩展现有组件,例如 JPanel
),并覆盖 paintComponent
.
有关详细信息,请参阅 this question。
不能这样画,正确的画法是
- 重写 JPanel subclass 中的 paintComponent() 方法并改为使用 Graphics 参数。
- 调用 repaint() 来触发重绘而不是您的 paintSwimmingPool() 方法
- 将参数,即 deepEnd 和 shallowEnd 移动到字段中,并从您的 paintComponent() 方法中引用它们 - 内部 class 我们能够 'see' 这些字段。理想情况下,这些将位于绘图面板和控制代码共享的单独模型中,但内部 class 完成了工作。
- 看看oracle tutorial on custom painting
完整的例子
public class SwimingPoolCalculator implements ChangeListener {
private JFrame frame;
private JPanel primaryJPanel, drawingJPanel;
private JLabel deepJLabel, shallowJLabel;
private JSlider deepJSlider, shallJSlider;
private JTextField resultTextField;
final private int xPos = 20;
final private int yPos = 20;
final private int swimmingPoolLength = 200;
final private int swimmingPoolWidth = 50;
private int deepEnd; // <== moved to fields
private int shallowEnd; // <== moved to fields
private class InnerDrawingPanel extends JPanel {
@Override
protected void paintComponent(Graphics drawingBoard) {
drawingBoard.setColor(Color.yellow); // <== drawing code now here
drawingBoard.fillRect(0, 0, 250, 250);
drawingBoard.setColor(Color.black);
drawingBoard.drawLine(xPos, yPos, xPos + swimmingPoolLength, yPos); // the top line
drawingBoard.drawLine(xPos, yPos, xPos, yPos + deepEnd); // the left line
drawingBoard.drawLine(xPos + swimmingPoolLength, yPos, xPos + swimmingPoolLength, yPos + shallowEnd); // the right line
drawingBoard.drawLine(xPos, yPos + deepEnd, xPos + swimmingPoolLength, yPos + shallowEnd); // the bottom line
}
}
public static void main(String[] args) {
SwimingPoolCalculator swimingPoolCalculator = new SwimingPoolCalculator();
swimingPoolCalculator.createGUI();
}
private void createGUI(){
frame = new JFrame("Swiming Pool Calculator");
primaryJPanel = new JPanel();
primaryJPanel.setPreferredSize(new Dimension(250, 500));
drawingJPanel = new InnerDrawingPanel(); // <== use inner class
drawingJPanel.setPreferredSize(new Dimension(250, 300));
drawingJPanel.setBackground(Color.yellow);
shallowJLabel = new JLabel("S End");
deepJLabel = new JLabel("D End");
shallJSlider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
deepJSlider = new JSlider(JSlider.HORIZONTAL, 50, 200, 100);
shallJSlider.addChangeListener(this);
deepJSlider.addChangeListener(this);
resultTextField = new JTextField(15);
primaryJPanel.add(drawingJPanel);
primaryJPanel.add(shallowJLabel);
primaryJPanel.add(shallJSlider);
primaryJPanel.add(deepJLabel);
primaryJPanel.add(deepJSlider);
primaryJPanel.add(resultTextField);
frame.getContentPane().add(primaryJPanel);
frame.pack();
frame.setVisible(true);
// why the first call does not draw any line?
shallowEnd = deepJSlider.getValue(); // <== now update fields
deepEnd = shallJSlider.getValue();
drawingJPanel.repaint(); // <== trigger repaint
}
private void calculateVolume(double averageDepth){
int swimmingPoolVolume = (int)(averageDepth * swimmingPoolLength * swimmingPoolWidth);
resultTextField.setText("Volume: " + swimmingPoolVolume);
}
@Override
public void stateChanged(ChangeEvent e) {
shallowEnd = shallJSlider.getValue(); // <== update fields
deepEnd = deepJSlider.getValue();
double averageDepth = (shallowEnd + deepEnd) / 2;
drawingJPanel.repaint(); // <== trigger repaint
calculateVolume(averageDepth);
}
}