• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to add JCompnent to Applet

 
Ed Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have exhausted all of my 10 weeks of Java knowledge with this issue. I have a PieChart grahpic that extends JComponent. The chart builds successfully as a program however, it will not add to the applet. This is the applet code:



Debug will step into the buildPieChartPanel method and again into the PieChart class instance to this code:

class IAPieChart{

double arcValue; // passes a value for the calculation of the arc.
Color color; // holds value for color (expressed as an integer

public IAPieChart(double value, Color color){

this.arcValue = value;
this.color = color;
}

public static class PieChart extends JComponent {


IAPieChart[] pieValue = {new IAPieChart(2, Color.green),
new IAPieChart(4, Color.orange),
new IAPieChart(4, Color.blue),
new IAPieChart(3, Color.red)

};

It ignores the remainder of the code and steps back out to the buildPieChartPanel method and continues. The code ignored is this:



I've tried making the class non-static however there are several issues such as modifier static not allowed here and constructor IAPieChart1 in class IAPieChart1 cannot be applied to given types; :



I honestly don't know where to go next. It's been 3 days just to build and graphic and add it to an applet. I know it's not this difficult. I hope someone at code ranch can help.
 
Ed Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've rewritten the applet in an attempt to make things simple and get some help displaying the graphic in the applet. I've tried adding both classes and the applet will still not render. Please review this code and try to give me some recommendation.

 
Paul Clapham
Sheriff
Posts: 21869
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all if you want to make a component where you're going to draw something (like a pie chart) then the usual class to extend is JPanel. And you extend it by providing a constructor where you put in the data and overriding the paintComponent() method with code that does the drawing.

You have a "drawPie()" method which suggests to me that you're going to call that method when you want to draw the pie chart. You don't do that in Swing. The GUI code, not your code, decides when things are going to be drawn. And when it decides that, it will call the paintComponent() method of the thing which it wants to draw. That's why you code the way I just described. And if you change something in your component (by calling one of its methods which changes its internal state) and that means the component needs to be redrawn, you just call the component's repaint() method and the GUI code will take care of redrawing it.

I have no idea why you would decide to use an inner class in your design so all I can say is don't do that.
 
Ed Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it!








 
Ed Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartender, I'm new at this and I was stuck. I usually don't come to these message boards because of the tone you took with me "I don't know why...."

Why don't you just not respond of give a solid answer?

When you get half assed explainations, and flat out lies, you tend to guess a lot. So there's your reason.

I'm an expert troubleshooter my friend. I just don't know how to program Java. I don't give up either. You guys on these forums are something else.
 
Ed Brooks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw this is what I duplicated

http://www.cbsnews.com/8301-503544_162-20028226-503544.html

Not bad for a new guy!
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys on these forums are something else


You are something else. You use multiple forums and cross post your questions. This wastes peoples time because we don't know what advice has already been given in other forums.

You don't listen to advice given. Twice not you have been told that your PieChart class should be a separate public class but you still refused to listen.

Your code is extending an Applet when it should extend JApplet since you are creating a Swing applet.

Didn't even look at the rest of the code since I'm sure the advice would have been ignored anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!