+ 1
How do I pass values from a JFrame class (that contains the main method) to a JPanel class?
The values I want to pass to the JPanel class are arrays. I need to use the values stored in the array as parameters in the JPanel class.
16 odpowiedzi
+ 3
Look at the second answer; I hope it helps.
Make the class that contains the main method a subclass of JPanel.
+ 3
I’m not 100% sure, but maybe if you add a constructor to the ArcsPanel class so when you initialize it in the DrawArcs() function you can pass values.
...I’m not really sure; I don’t Java, but I hope that that will help you.
+ 2
You’re welcome! Good luck!
+ 1
Rowsej what if the values I want to pass to the JPanel are array object?
+ 1
Rowsej thank you. I'll try it out, because right now the main method is in the class that extends jFrame
+ 1
@Rowsej so my main class extends JFrame, and I am getting user input using the Scanner class. One of my user input is the percentage of certain subjects (this assignment requires getting the percentage of subjects on one semester). Anyway, so I'm supposed to create a pie chart, and I need to access this user inputs (the percentage) in order to draw the pie chart. I'm using the paintComponent and fillArc method. I stored my user inputs (in the main class) in an array. I'm not supposed to use JavaFx, and I have two classes, one that extends JFrame (has the main method) and one that extends JPanel.
+ 1
Could you possibly share your code?
+ 1
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
public class DrawArcs extends JFrame
{
public DrawArcs()
{
setTitle("DrawArcs");
add(new ArcsPanel());
}
/** Main method */
public static void main(String[] args)
{
DrawArcs frame = new DrawArcs();
frame.setSize(250, 300);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
// The class for drawing arcs on a panel
class ArcsPanel extends JPanel
{
// Draw four blades of a fan\
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.setColor(Color.RED);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.s
+ 1
Rowsej this is the template he sent us to modify.. Its not complete, it doesn't give the desired output
+ 1
// The class for drawing arcs on a panel
class ArcsPanel extends JPanel
{
// Draw four blades of a fan\
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
g.setColor(Color.RED);
g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
g.setColor(Color.YELLOW);
g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
}
}
+ 1
Jay SoloLearn limits the character count for answers; could you possibly save it in the Code Playground and give a link?
Looks great anyway!
+ 1
Rowsej this is what I've done so far
https://code.sololearn.com/c630fv5bZi5c/?ref=app
+ 1
Jay Thanks, I’ll look at it sometime soon.
+ 1
Rowsej thank you for you assistance. Much appreciated
+ 1
Rowsej thank you, I'll try that out