I have this code and for some reason I keep getting an error where it says "exporting non-public type through public api ".. I'm not sure why this keeps happening but it does so for getCircleInfo / drawCircle. Thanks for your help.
- Me. -
package shapemaker;
-
-
import java.awt.*;
-
import javax.swing.*;
-
-
public class ShapeMaker {
-
-
private JFrame win;
-
private Container contentPane;
-
-
public ShapeMaker ( ) {
-
win = new JFrame("Shape Maker");
-
win.setSize(400, 300);
-
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
win.setVisible(true);
-
contentPane = win.getContentPane();
-
contentPane.setBackground(Color.WHITE);
-
}
-
-
public static void main(String[] args) {
-
ShapeMaker shapeMaker = new ShapeMaker();
-
shapeMaker.start();
-
}
-
-
public void start ( ) {
-
Circle circle = new Circle ( );
-
getCircleInfo(circle);
-
drawCircle(circle);
-
}
-
-
public void getCircleInfo ( Circle circle ) {
-
double radius;
-
int x, y;
-
radius = Double.parseDouble ( JOptionPane.showInputDialog (win, "Enter Radius") );
-
circle.setRadius(radius);
-
circle.setFillColor(Color.orange);
-
circle.setPenColor(Color.black);
-
x = Integer.parseInt( JOptionPane.showInputDialog ( win, "Enter X Cord:" ) );
-
circle.setXCord(x);
-
y = Integer.parseInt( JOptionPane.showInputDialog ( win, "Enter Y Cord:" ) );
-
circle.setYCord(y);
-
}
-
-
public void drawCircle ( Circle circle ) {
-
Graphics g = contentPane.getGraphics();
-
circle.draw(g);
-
}
-
}
14 13948
Could we have a look at that Circle class? Also, when do you get this error?
Greetings,
Nepomuk
The error I get is that no circle comes up when I run this code and I am not sure why.
Circle Class: -
package shapemaker;
-
-
import java.awt.*;
-
-
class Circle {
-
-
private double Radius;
-
private int XCord, YCord;
-
private Color penColor, fillColor;
-
-
// Constructors
-
public Circle ( ) {
-
//setRadius(0);
-
this(0);
-
}
-
-
public Circle ( double r ) {
-
setRadius(r);
-
penColor = Color.BLACK;
-
fillColor = Color.WHITE;
-
XCord = 0;
-
YCord = 0;
-
}
-
-
// Set Methods
-
public void setRadius ( double r ) {
-
Radius = 0;
-
-
if (r > 0) {
-
Radius = r;
-
}
-
}
-
-
public void setXCord ( int x ) {
-
XCord = 0;
-
-
if (x > 0) {
-
XCord = x;
-
}
-
}
-
-
public void setYCord ( int y ) {
-
YCord = 0;
-
-
if (y > 0) {
-
YCord = y;
-
}
-
}
-
-
public void setPenColor ( Color pen ) {
-
penColor = pen;
-
}
-
-
public void setFillColor ( Color fill ) {
-
fillColor = fill;
-
}
-
-
// Get Methods
-
public double getArea ( ) {
-
return Math.pow( ( Math.PI * Radius ) , 2 );
-
}
-
-
public double getCircumference ( ) {
-
return ( Math.PI * ( Radius * 2 ) );
-
}
-
-
public void draw ( Graphics g ) {
-
int width = (int) (Radius * 2);
-
int height = (int) (Radius * 2);
-
g.drawString("This", -10, -10);
-
-
g.drawOval(XCord, YCord, width, height);
-
g.setColor(penColor);
-
g.fillOval(XCord, YCord, width, height);
-
g.setColor(fillColor);
-
-
g.dispose();
-
}
-
}
-
@DeadSilent
So the error you mentioned in your original posting automagically disappeared? btw, I expected to see a paintComponent(Graphics g) method for drawing purposes. Where is it?
kind regards,
Jos
This was for a project and paintComponent() was not required to run the program in NetBeans, I'm just starting to learn Java so the graphics / drawing class functions I'm starting to learn. What exactly would I use the paintComponent() for?
@DeadSilent
I don't believe that; there's always a paintComponent() method under the hood somewhere. NetBeans is not a magic wizard that solves it all for you. @DeadSilent
Please check the "Read This First" article; it's the first article in this group; it contains a link to the tutorials; one of them is the Swing tutorial. At least read that one before you attempt to draw anything at all.
kind regards,
Jos
ps. how come that mysterious error message you mentioned before disappeared automagically?
Ohh its still there, I'm just going to research swing more in depth and hope I can get something figured out before tomorrow.
@DeadSilent
You can't expect a program to run correctly (or run at all) when such error messages are printed. What exactly issued that message? The compiler? The Java virtual machine itself? Something else?
kind regards,
Jos
Read my first post, I believe I stated the issue clearly. :\
@DeadSilent
Yes you did and you immediately forgot all about it and mentioned another problem (Swing related); that's why I kept asking about it. Don't add more problems to the problem pool but first solve the original problem(s).
kind regards,
Jos
LOL! Sorry about that, I got confused for a second.
For the exporting non-public type through public API thing, try making the method headings just void getCircleInfo ( Circle circle ) and void drawCircle ( Circle circle ). Take away the publics. Because every class in the program is part of one package, all the classes should be able to access the methods and fields from all other classes in the package even if the methods and fields have no public-private declaration. So this should work, because it gets rid of the public API, but still allows all classes access. I'm not sure exactly why all this happens, but I get the same thing with NetBeans when I try to use certain classes as parameters. I've found that this usually makes the IDE shut up. I've used other compilers/editors (i.e.) JCreator and this seems to be a problem specific to NetBeans. As for the paintComponent() method, chances are that it is in one of those collapsed sections of uneditable text that are generated by the design panel.
The warning is coming from your line five (5): the class Circle has no designation [i.e. public, private, etc];
Make it public and warning will disappear.
As to your other issues, only Classes should be capitalized; for the sake of future debugging efforts, change your variables [like Radius to 'radius'] to lower-case, so in-code references do not imply it is a reference to a static Class.
Based on your codes:
// Constructors
public Circle ( ) { //setRadius(0); this(0);
}
public Circle ( double r ) {
setRadius(r);
penColor = Color.BLACK;
fillColor = Color.WHITE; XCord = 0; YCord = 0;
}
and from shapemaker
public void start ( ) { Circle circle = new Circle ( );
getCircleInfo(circle);
drawCircle(circle);
}
Your constructor for a Circle requires a radius parameter; your default is 0 and no other value is passed; I contend you ARE drawing a circle, though one of radius = 0, at coords (0,0).
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
4 posts
views
Thread by Angel Cat |
last post: by
|
3 posts
views
Thread by sridevi |
last post: by
|
1 post
views
Thread by Janne Ruuttunen |
last post: by
|
reply
views
Thread by Mike P |
last post: by
|
1 post
views
Thread by Mustufa Baig |
last post: by
|
7 posts
views
Thread by victorsk |
last post: by
|
2 posts
views
Thread by bienwell |
last post: by
|
2 posts
views
Thread by Snozz |
last post: by
|
reply
views
Thread by =?Utf-8?B?ZGVuIDIwMDU=?= |
last post: by
| | | | | | | | | | | |