当我重新绘制时,我想在paintComponent()中使用snowBoarding类中的run_1和run_2数组。我想用这些值画出矩形的柱状图,但是我不知道如何将snowBoarding中的值带入myJPanel。
public class snowBoarding extends JFrame {
public int[] run_1 = new int[6];
public int[] run_2 = new int[6];
private myJPanel DrawPanel = null;
private myJPanel getDrawPanel() {
if (DrawPanel == null) {
DrawPanel = new myJPanel();
DrawPanel.setLayout(new GridBagLayout());
DrawPanel.setBounds(new Rectangle(258, 39, 326, 361));
DrawPanel.setBackground(Color.white);
DrawPanel.setEnabled(true);
DrawPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
//Instantiate the BufferedImage object and give it the same width
// and height as that of the drawing area JPanel
img = new BufferedImage(DrawPanel.getWidth(),
DrawPanel.getHeight(),
BufferedImage.TYPE_INT_RGB);
//Get its graphics context. A graphics context of a particular object allows us to draw on it.
g2dImg = (Graphics2D)img.getGraphics();
//Draw a filled white coloured rectangle on the entire area to clear it.
g2dImg.setPaint(Color.WHITE);
g2dImg.fill(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
}
return DrawPanel;
}
public JButton getButton_calc_draw() {
if (Button_calc_draw == null) {
Button_calc_draw = new JButton();
Button_calc_draw.setBounds(303, 411, 131, 39);
Button_calc_draw.setFont(new Font ("Dialog", Font.BOLD, 18));
Button_calc_draw.setText("Draw");
Button_calc_draw.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// Get values from the text fields
//I want these values from the array//
run_1[0] = Integer.parseInt(textField.getText());
run_1[1] = Integer.parseInt(textField_1.getText());
run_1[2] = Integer.parseInt(textField_2.getText());
run_1[3] = Integer.parseInt(textField_3.getText());
run_1[4] = Integer.parseInt(textField_4.getText());
run_1[5] = Integer.parseInt(textField_5.getText());
for (int i = 0; i < run_1.length; i++) {
temp[i] = run_1[i];
}
Arrays.sort(temp);
for (int i = 1; i < (temp.length -1) ; i++){
avg1+=temp[i];
}
avg1 = avg1/4;
//and these as well
run_2[0] = Integer.parseInt(textField_6.getText());
run_2[1] = Integer.parseInt(textField_7.getText());
run_2[2] = Integer.parseInt(textField_8.getText());
run_2[3] = Integer.parseInt(textField_9.getText());
run_2[4] = Integer.parseInt(textField_10.getText());
run_2[5] = Integer.parseInt(textField_11.getText());
for (int i = 0; i < run_2.length; i++) {
temp[i] = run_2[i];
}
Arrays.sort(temp);
for (int i = 1; i < (temp.length -1) ; i++){
avg2+=temp[i];
}
avg2 = avg2/4;
if (avg1 > avg2){
OverallScore = avg1;
}
else {
OverallScore = avg2;
}
total_1.setText(Integer.toString(avg1));
total_2.setText(Integer.toString(avg2));
Overall.setText(Integer.toString(OverallScore));
DrawPanel.setShallPaint(true);
DrawPanel.repaint();
}
;
});
class myJPanel extends JPanel {
SnowBoarding snowBoarding;
public void MyJPanel(SnowBoarding snowBoarding) {
this.snowBoarding = snowBoarding;
}
private static final long serialVersionUID = 1L;
private Rectangle2D.Double rectangle;
int[] score = new int[12];
// placed into this array
/* score[0] = run_1[0];
score[1] = run_1[1];
score[2] = run_1[2];
score[3] = run_1[3];
score[4] = run_1[4];
score[5] = run_1[5];
score[6] = run_2[0];
score[7] = run_2[1];
score[8] = run_2[2];
score[9] = run_2[3];
score[10] = run_2[4];
score[11] = run_2[5];
*/
/*
for (i = 0; i < run_1.length, i++){
score[i] = run_1[i];
}
for (i = 0; i < run_2.length, i++){
score[i+6] = run_2[i];
}
*/
private boolean shallPaint = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (shallPaint) {
Graphics2D g2D = (Graphics2D) g;
rectangle = new Rectangle2D.Double(0, 350-score[1] * 2, 25, score[1] * 2);
g2D.setPaint(Color.blue);
g2D.fill(rectangle);
g2D.draw(rectangle);
}
}
public void setShallPaint(boolean pShallPaint) {
shallPaint = pShallPaint;
}
}
如果你能回答清晰的代码示例和描述,将不胜感激。
编辑放置myJpanel DrawPanel = null行。和DrawPanel方法
传递对snowBoarding类对象的引用(请将类名以大写字母开头)
class MyJPanel extends JPanel {
SnowBoarding snowBoarding;
public MyJPanel(SnowBoarding snowBoarding) {
this.snowBoarding = snowBoarding;
}
那么在paintComponent方法中,你可以简单地使用snowBoarding.run_1
在创建MyJpanel时将SnowBoarding类的对象引用传递给MyJpanel构造器
在你的SnowBoarding类中
MyJpanel should look like below
class MyJPanel extends JPanel {
SnowBoarding snowBoarding;
public MyJPanel(SnowBoarding snowBoarding) {
this.snowBoarding = snowBoarding;
}
//other code goes here
}
和在SnowBoarding类中
MyJpanel panel = new MyJpanel(snowBoarding);
,其中snowBoarding是保存这两个数组的对象。
现在访问run1和run2
public void paintComponent(Graphics g) {
snowBoarding.run1 // use it
snowBoarding.run2 //use it
rest of the code
}