import java.awt.*; import java.applet.*; import java.awt.event.*; public class Test extends Applet implements ActionListener { Label lb; TextField tf1, tf2, tf3; Button bt; int size, size1, size2, s1, s, s2, total; float percSize, percSize1, percSize2; public void init() { setLayout(null); lb = new Label("Enter Statistic Figures"); tf1 = new TextField(); tf2 = new TextField(); tf3 = new TextField(); bt = new Button("Draw"); bt.addActionListener(this); lb.setBounds(20,40,140,20); tf1.setBounds(170,40,40,20); tf2.setBounds(220,40,40,20); tf3.setBounds(270,40,40,20); bt.setBounds(380,40,60,20); add(lb); add(tf1); add(tf2); add(tf3); add(bt); } public void paint(Graphics g) { //od lewej,od gory, pochylenie=200, szerokosc=200 (proporcjonalnie!), od kata, rozsz. o kat g.setColor(Color.BLUE); g.fillArc(100,200,400,400,0,s); g.setColor(Color.ORANGE); g.fillArc(100,200,400,400,s,s1); g.setColor(Color.GREEN); g.fillArc(100,200,400,400,s1,s2); } public void actionPerformed(ActionEvent e) { size = Integer.parseInt(tf1.getText()); size1 = Integer.parseInt(tf2.getText()); size2 = Integer.parseInt(tf3.getText()); total = size+size1+size2; percSize = (size * 100.0f) / total; percSize1 = (size1 * 100.0f) / total; percSize2 = (size2 * 100.0f) / total; s = percSize*360/100; s1 = percSize1*360/100; s2 = percSize2*360/100; repaint(); } } 

The error code does not work when compiling + does not display the diagram itself (Does not draw) When compiling this line of code, s = percSize * 360/100; Throws error possible lossy conversion from float to int

  • Added, entered a mistake that gives out - Vladislav Orlovsky
  • possible lossy conversion from float to int - says only that the accuracy of the number is lost, since it converts into an integer, which means throwing away everything after the comma ..... you need to understand whether the whole suits you or if you want to maintain accuracy - Alexey Shimansky

2 answers 2

Change these three lines to

 s = (int) (percSize*360/100); s1 = (int) (percSize1*360/100); s2 = (int) (percSize2*360/100); 
  • Thanks, a huge code has earned and compiled, but now it doesn’t draw the diagram itself, I will be very grateful if you can help, because I didn’t quite understand the theme with applets in the university - Vladislav Orlovsky
  • I copied myself earned from me. 1) How do you run? If through html throw the source html you are using. 2) text fields and a button see? You enter numbers into these fields (let's say 1 in each) and press a button. 3) if you do everything as written in paragraph 2 See if there is an error in the console? - IL Mare
  • I launch via BlueJ - Vladislav Orlovsky
  • Do you have a chart with any entered values? (I mean, the circle is building) - Vladislav Orlovsky
  • So your logic does not work. And I think the applet is not shown. In the latter, fillArc should not be from s1 but from s + s1. Because s1 does not start from scratch. g.setColor(Color.GREEN); g.fillArc(100, 200, 400, 400, s1+s, s2); - IL Mare
 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Test extends Applet implements ActionListener { Label lb; TextField tf1, tf2, tf3; Button bt; int size, size1, size2, s1, s, s2, total; float percSize, percSize1, percSize2; public void init() { setLayout(null); lb = new Label("Enter Statistic Figures"); tf1 = new TextField(); tf2 = new TextField(); tf3 = new TextField(); bt = new Button("Draw"); bt.addActionListener(this); lb.setBounds(20,40,140,20); tf1.setBounds(170,40,40,20); tf2.setBounds(220,40,40,20); tf3.setBounds(270,40,40,20); bt.setBounds(380,40,60,20); add(lb); add(tf1); add(tf2); add(tf3); add(bt); } public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillArc(100,200,400,400,0,s); g.setColor(Color.ORANGE); g.fillArc(100,200,400,400,s,s1); g.setColor(Color.GREEN); g.fillArc(100,200,400,400,s1+s,s2); } public void actionPerformed(ActionEvent e) { size = Integer.parseInt(tf1.getText()); size1 = Integer.parseInt(tf2.getText()); size2 = Integer.parseInt(tf3.getText()); total = size+size1+size2; s = (int) (percSize*360/100); s1 = (int) (percSize1*360/100); s2 = (int) (percSize2*360/100); repaint(); } } 

Here is all the code that turned out