Reputation: 41
I want to use the run_1 and run_2 arrays from the snowBoarding class within the paintComponent() when I redraw. I want to use the values to make a bar graph out of rectangles, but I cannot figure out how to bring the values from snowBoarding into the 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;
}
}
If you could please answer with clear code examples with descriptions that would be much appreciated.
edit placed the myJpanel DrawPanel = null line in. and DrawPanel Method
Upvotes: 0
Views: 50
Reputation: 2732
pass SnowBoarding class's object reference to MyJpanel constrcutor during the creation of MyJpanel
inside your SnowBoarding class.
so
MyJpanel should look like below
class MyJPanel extends JPanel {
SnowBoarding snowBoarding;
public MyJPanel(SnowBoarding snowBoarding) {
this.snowBoarding = snowBoarding;
}
//other code goes here
}
and inside your SnowBoarding class,
MyJpanel panel = new MyJpanel(snowBoarding);
where snowBoarding is the object which holds those 2 arrays.
now access run1 and run2 inside
public void paintComponent(Graphics g) {
snowBoarding.run1 // use it
snowBoarding.run2 //use it
rest of the code
}
Upvotes: 0
Reputation: 41
Pass the reference to snowBoarding class object (please, write the class names starting with upper letter)
class MyJPanel extends JPanel {
SnowBoarding snowBoarding;
public MyJPanel(SnowBoarding snowBoarding) {
this.snowBoarding = snowBoarding;
}
then in paintComponent method you can simply get these arrays using snowBoarding.run_1
Upvotes: 1