Testing out drawing shapes 2D to start
Author |
Message |
QuantumPhysics
|
Posted: Thu Sep 13, 2012 12:00 am Post subject: Testing out drawing shapes 2D to start |
|
|
I am doing this a fun side project, besides our school work. I am trying to make a spherical moving prism of sort. This is what I have so far. When I run the application, it creates the GUI that I made but for some reason it does not draw the shapes onto the screen. Why Java? Why do you hate me?
Java: |
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame {
public static void main (String args[]){
GUI window = new GUI("Spherical Rotating Prism");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setVisible(true);
} public GUI (String title){
super(title);
JPanel pane = (JPanel)getContentPane();
} public void draw (Graphics g){
for (int i = 0; i < 360; i += 30){
int x = 150 + (int)(100 * Math.cos(i *Math.PI / 180));
int y = 150 + (int)(100 * Math.sin(i *Math.PI / 180));
g.fillOval(x-10, y-10, 20, 20);
g.drawLine(150, 150, x, y);
}
}
}
|
My IDE is eclipse - Juno build |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Zren

|
Posted: Thu Sep 13, 2012 1:53 pm Post subject: RE:Testing out drawing shapes 2D to start |
|
|
Point out to me where draw(Graphics) is in the java documentation.
Also why are you writing the next method on the same line as the closing bracket of the last? |
|
|
|
|
 |
QuantumPhysics
|
Posted: Thu Sep 13, 2012 7:00 pm Post subject: RE:Testing out drawing shapes 2D to start |
|
|
Sorry can you elaborate? Draw(Graphics g) is the method - This is not what you were implying? I do not understand. I just wrote the method next to the token because it makes it look visually appealing as if it is all one big program, spread apart it just doesn't appeal to me. Of course it will look better if I comment everything, but until then I just like it like that. Does it make a difference either way? Can you explain please. |
|
|
|
|
 |
Zren

|
Posted: Thu Sep 13, 2012 8:11 pm Post subject: RE:Testing out drawing shapes 2D to start |
|
|
I'm hinting at the fact your draw(Graphics) function isn't being called. Mainly because you assume GUI.draw(Graphics) is overriding a method signature of draw(Graphics) in the JFrame or one of it's superclasses. Which isn't happening because draw(Graphics) doesn't exist in JFrame. Check for yourself.
http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html
Now, after looking at the documentation, what method do you think you should use?
Adding an @Override annotation onto the method will cause errors if the method doesn't actually override anything.
Java: |
@Override
public void draw (Graphics g ) {
for (int i = 0; i < 360; i += 30) {
int x = 150 + (int) (100 * Math. cos(i * Math. PI / 180));
int y = 150 + (int) (100 * Math. sin(i * Math. PI / 180));
g. fillOval(x - 10, y - 10, 20, 20);
g. drawLine(150, 150, x, y );
}
}
|
|
|
|
|
|
 |
|
|