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);
}
}
你不能这样画,正确的技巧是
充分工作的例子
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);
}
}
问题内容: 我有一个JPanel,它打算充当游戏的HUD,自然,我已经重写了paint方法来进行自己的自定义显示,但确实会调用此方法,但是仅在调整大小或最大化,最小化框架时才调用,我的游戏循环告诉它repaint()。由于另外两个面板完全重新粉刷,对我来说,这尤其奇怪。 这是我的HUD课: 它在我的游戏类更新以及其他两个面板中被调用 由我的JFrame调用 很长一段时间以来一直在试图解决这个问题,
在helper类的静态方法中调用时,它会抛出一个NPE。我所做的是嘲笑MarkupMaker和它的返回值(一个Markup实例)。最后,我希望调用标记实例的。无论我做什么-的调用都是抛出一个NPE。我找不到任何文档告诉我如何在spock中详细模拟方法调用值。 编辑:我添加了示例。的调用返回null,即使我在spock测试中对其进行了嘲弄。 test.groovy java(执行模拟对象的方法) j
问题内容: 我有一个BaseActivity,其他所有活动都可以对其进行扩展。关键是,每当用户离开活动时,我都会将音乐静音。我也停止听电话。 问题是,只要用户在活动之间进行切换,就会被调用,这意味着该应用程序不必要地静音和停止,即使仅当用户离开该应用程序时该静音也应该停止。 现在说我在和之间切换。即使我只想 在用户离开应用程序时* 被调用,此开关也 不必要 执行。我该怎么办? * 感谢您的专家意见
很难将Game类中的方法调用到绞刑游戏的主方法。我们应该旋转一个轮子以获得100250或500美元的头奖金额,然后按照你的预期玩游戏……但方法是必要的。我还没有完成,我只是想在这一点上调用我的方法,看看它是如何工作的。这是我的代码: 返回错误是 游戏演示。java:11:error:找不到符号System.out。打印(“旋转轮子后,你得到了”spinWheel())^
我正在为Angular7模板中的函数编写单元测试用例。它是一个登录组件,登录功能在http请求中具有router.navigate,以便在正确登录时路由到仪表板。但是我得到了错误 - 错误:应使用[['/ProjectData/MasterSequence']]调用spy navigate,但从未调用过。堆叠(http://localhost:9876/absolute/home/hp/Downl
我在将E3 RCP应用程序迁移到E4时遇到了问题。我有三个部分。2.样品含量为Lars Vogel(http://www.vogella.com/tutorials/EclipseRCP/article.html#plugin_creatinge4“使用SWT电源小部件”)进行测试,并将其与我的部件进行比较。 长话短说(er):应该创建视图内容的方法(标记为@PostConstruct)不会被调用