I am new to java and need some help! My professor wants me to create 4 methods, which ultimate goal is to output a animated rotating square like I did in this earlier code that I made:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GTest2 {
public static void main(String[] args) {
GraphicsPanel gp = new GraphicsPanel();
gp.delay(1000);
int x = gp.getWidth() / 2;
int y = gp.getHeight() / 2;
gp.setLocation(x, y);
for (int n=1; n <= 150; n++) {
gp.clear();
for (int angle=0; angle < 360; angle += 10) {
for (int i=0; i < 4; i++) {
gp.draw(100);
gp.turn(90);
}
gp.turn(10);
}
gp.render();
gp.delay(30);
gp.turn(2);
}
}
}
My professor supplied us class GraphicsPanel so we could do these programs of creating shapes and animations: http://pastebin.com/Ha1pdLrc
This is what I have so far, but I seem to only have been able to create a rotating square:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class G7 {
public static void main(String[] args) {
GraphicsPanel gp = new GraphicsPanel();
gp.delay(1000);
animateCircleOfPolygons(gp);
}
public static void animateCircleOfPolygons(GraphicsPanel gp){
for (int n = 1; n <= 150; n++) {
gp.clear();
drawCircleOfPolygons(gp);
gp.render();
gp.delay(30);
gp.turn(2);
}
}
public static void drawCircleOfPolygons(GraphicsPanel gp){
for (int angle = 0; angle < 360; angle += 10) {
drawPolygon(gp, 100, 4);
gp.turn(10);
}
}
public static void drawPolygon(GraphicsPanel gp, int sideLength, int sideCount){
for (int i = 0; i < sideCount; i++) {
gp.draw(sideLength);
gp.turn(360.0 / sideCount);
gp.delay(30);
}
}
}
I have looked everywhere for an answer to no avail, can anybody please help me understand what I am doing wrong.
I've had a quick read through you code...
Don't...
setSize
of JPanel
wihtout calling super.setSize
, doing so will have dire consequences for how your component will work super.paintComponent
regardless of what you are doing in the method, paintComponent
does important work and this will result in numerous paint artifacts been left on you component and/or other paint related issues JFrame
within a component's constructor, this makes assumptions about the use of the component which aren't healthy. You component should make no assumption about how it might be used. My professor supplied us class GraphicsPanel
With all due respect, your professor has absolutely no idea what they are talking about, this is a series of rudimentary bad ideas and problems waiting to happen.
Your core problem is, Swing is a single threaded framework. This means that your for
loop which you draw
and rotate
is been executed within the context of the EDT (in fact, it's likely that the component hasn't even been put on the screen yet), which means that until you exit the method, none of the paint events generated by your code can be processed.
The RepaintManager
, responsible for scheduling paint events is always highly optimized, meaning that it is capable of coalescing multiple repaint requests into only a few actually events
Also, remember, transformation will only affect what is painted after it, not before. Therefore, rotation must be done before you do any painting in order for it take affect
Take a look at Concurrency in Swing , Performing Custom Painting , Painting in AWT and Swing , 2D Graphics and Creating a GUI With JFC/Swing for more details
Not really sure if I understand your issue fully, but if it is that the second example code G7
doesn't appear to be animated (like the first) as it turns slower than in the first example code GTest2
it's because you have duplicated the delay: gp.delay(30);
in the second code; it appears both in the animateCircleOfPolygons
function as well as in the drawPolygon
function. It you remove either the code will produce identical output (as it should - it's the same logic after all).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.