I programmed a code for a cube which can be turned on all pivots. Now I tried to put in a loop with Thread.sleep but each time it repaints it just gives me the half cube or nothing (it's kinda flickering and faltering).
Maybe it doesn't work because my laptop is too slow but I don't think that this is the case.
Here's the code:
import java.awt.*;
import javax.swing.*;
public class Würfel1 extends JApplet {
Container container;
Dimension Screen = new Dimension(400,400);
double c[] = new double[8];
double wx = 90; double wy = 90; double wz = 90;
public Würfel1() {
init();
}
public void init() {
this.setSize(Screen);
container = this.getContentPane();
}
public void paint(Graphics g) {
super.paint(g);
drawcube(g);
wx = wx - 2;
wy = wy + 1;
wz = wz + 3;
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
repaint();
}
private void drawcube(Graphics g) {
/*
* Punkt links oben vorne
*/
int xStart = 100;
int yStart = 100;
/*
* Breite, Höhe und Länge des Körpers
*/
int b = 200;
int h = 200;
int l = 200;
/*
* Winkel der X-, Y- und Z-Achse des Körpers
*/
// int wx = 90;
// int wy = 90;
// int wz = 90;
/*
* Mittelpunkt des Körpers
*/
int x = xStart + b/2;
int y = yStart + h/2;
/*
* erzeugt die Grundwerte für den Winkel 90,90,90
*/
double xfield[] = {(-b/2),(b/2),(b/2),(-b/2),(-b/2),(b/2),(b/2),(-b/2)};
double yfield[] = {(-h/2),(-h/2),(h/2),(h/2),(-h/2),(-h/2),(h/2),(h/2)};
double zfield[] = {(l/2),(l/2),(l/2),(l/2),(-l/2),(-l/2),(-l/2),(-l/2)};
/*
* verändert die Werte unter Berücksichtigung der Winkel
*/
for (int i = 0; i < 8; i++)
{
double newx,newy,newz;
newy = yfield[i] * Math.cos(Math.PI*(90-wx)/180) - zfield[i] * Math.sin(Math.PI*(90-wx)/180);
newz = yfield[i] * Math.sin(Math.PI*(90-wx)/180) + zfield[i] * Math.cos(Math.PI*(90-wx)/180);
yfield[i] = newy;
zfield[i] = newz;
newx = xfield[i] * Math.cos(Math.PI*(90-wy)/180) - zfield[i] * Math.sin(Math.PI*(90-wy)/180);
newz = xfield[i] * Math.sin(Math.PI*(90-wy)/180) + zfield[i] * Math.cos(Math.PI*(90-wy)/180);
xfield[i] = newx;
zfield[i] = newz;
newx = xfield[i] * Math.cos(Math.PI*(90-wz)/180) - yfield[i] * Math.sin(Math.PI*(90-wz)/180);
newy = xfield[i] * Math.sin(Math.PI*(90-wz)/180) + yfield[i] * Math.cos(Math.PI*(90-wz)/180);
xfield[i] = newx;
yfield[i] = newy;
}
for (int i = 0; i < 8; i++) {
c[i] = 1;
}
/*
* Malt die Linien des Körpers
*/
DrawPolygon(0,1,2,3,xfield,yfield,x,y,g,1);
DrawPolygon(6,5,4,7,xfield,yfield,x,y,g,2);
DrawPolygon(5,1,0,4,xfield,yfield,x,y,g,3);
DrawPolygon(3,2,6,7,xfield,yfield,x,y,g,4);
DrawPolygon(2,1,5,6,xfield,yfield,x,y,g,5);
DrawPolygon(4,0,3,7,xfield,yfield,x,y,g,6);
}
public void DrawPolygon(int a, int s, int d, int f, double[] xfield, double yfield[],int b,int h,Graphics g,int c) {
if((xfield[a] - xfield[s]) * (yfield[d] - yfield[s])
- (yfield[a] - yfield[s]) * (xfield[d] - xfield[s]) > 0) {
// |j->i x j->k| > 0
int xCoords[] = {(int)(xfield[a])+b,(int)(xfield[s])+b,
(int)(xfield[d])+b,(int)(xfield[f])+b};
int yCoords[] = {(int)(yfield[a])+h,(int)(yfield[s])+h,
(int)(yfield[d])+h,(int)(yfield[f])+h};
Color color = new Color(0,0,0);
if (c == 1) color = new Color(255,0,0);
if (c == 2) color = new Color(255,255,0);
if (c == 3) color = new Color(0,255,0);
if (c == 4) color = new Color(0,255,255);
if (c == 5) color = new Color(0,0,255);
if (c == 6) color = new Color(255,0,255);
g.setColor(color);
g.fillPolygon(xCoords, yCoords, 4);
}
}
public static void main(String[] args) {
new Würfel1();
}
}
I used this Idea because I saw it in another code, but there the Polygon is drawed with an Image called buffer (i do not really know what this is) I also use JApplet because its easier to use then a JFrame where i add my JPanel.
My latest try was to replace the try [...] catch [...] code with
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
};
new Timer(100,action).start();
but for some reason it just speeded up my applet and still had the problem i had. I already read in other question that they got a similar problem but i didnt find a way to solve mine in using the answers to this problem.
I now changed the normal draw[...] and put my cube on an image: ... first is right on top in the drawcube method
// Double-Buffering
if (buffer==null) {
buffer=createImage(this.getSize().width, this.getSize().height);
gBuffer=(Graphics2D)buffer.getGraphics();
}
gBuffer.clearRect(0,0, this.getSize().width, this.getSize().height);
// Antialiasing
gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
... the next quote is set in the end of drawcube
g.drawImage(buffer,0,0,this);
... i also had to change the fillPoly method (which is quite obvious)
gBuffer.setColor(color);
gBuffer.fillPolygon(xCoords, yCoords, 4);
... i put a timer in the init method - now its almost working fine
public void init() {
this.setSize(Screen);
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
};
new Timer(100,action).start();
}