I'm in my first year of computer engineering, learning Java as my first programming language. I wanted to create a 'Fast Wheel Raffle' application, but I encountered a few issues. First of all:
1- I'm creating the Arcs using the fillArc method and increasing the spinAngel by spinAngelMultiplier in the spin() method to perform the spinning operation. 2- The calculateAngel() method returns the angle for each Arc.
The problem I'm facing is that I'm unable to center each value from the 'inputs' list within the Arcs. What can I do to achieve this?
There is a drawWheel() method for creating Arcs.
public void drawWheel(Graphics g){
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2D.setStroke(new BasicStroke(4));
g2D.drawOval(100,100,400,400);
if(this.inputs.size() > 0){
int extra = 0;
for(int i = 0; i < this.inputs.size(); i++){
g2D.setColor(colors[i]);
int angel = calculateAngel();
if ( ( i == this.inputs.size() - 1 ) && ( (angel + angel*i) != 360 ) ) {
extra = 360 - (angel+angel*i) ;
}
g2D.fillArc(100,100,400,400,angel*i+spinAngel, angel+extra);
System.out.println(angel*i+spinAngel + " and " + (angel+extra));
// I really confused on this part
g2D.setFont(new Font("Ariel", Font.BOLD, 10));
g2D.setColor(Color.black);
int xMultiplier = -1;
int yMultiplier = 1;
if(angel*i+spinAngel >=0 && angel*i+spinAngel <180){
yMultiplier = -1;
}if(angel*i+spinAngel >=270 && angel*i+spinAngel <90){
xMultiplier = 1;
}
int cosX = (int) ( Math.cos(angel+extra) );
int sinY = (int) ( Math.cos(angel+extra) );
int stringX = 300 + (xMultiplier * cosX);
int stringY = 300 + (yMultiplier * sinY);
g2D.drawString( inputs.get(i), stringX, stringY);
}
}
g2D.setColor(Color.BLACK);
g2D.fillOval(300,300,2,2);
}
And spin() method for increasing spinAngel and reducing spinAngelMultiplier randomly.
public void spin(){
if(spinAngelMultiplier == 0){
gameRunning = false;
return;
}
if(spinAngelMultiplierReducer >= randomNumber){
spinAngelMultiplierReducer = 0;
spinAngelMultiplier--;
minMaxSet();
}
spinAngelMultiplierReducer++;
spinAngel += spinAngelMultiplier;
}
@Override
public void actionPerformed(ActionEvent e) {
spin();
repaint();
}
In this part of drawWheel() method;
System.out.println(angel*i+spinAngel + " and " + (angel+extra));
g2D.setFont(new Font("Ariel", Font.BOLD, 10));
g2D.setColor(Color.black);
int xMultiplier = -1;
int yMultiplier = 1;
if(angel*i+spinAngel >=0 && angel*i+spinAngel <180){
yMultiplier = -1;
}if(angel*i+spinAngel >=270 && angel*i+spinAngel <90){
xMultiplier = 1;
}
int cosX = (int) ( Math.cos(angel+extra) );
int sinY = (int) ( Math.cos(angel+extra) );
int stringX = 300 + (xMultiplier * cosX);
int stringY = 300 + (yMultiplier * sinY);
g2D.drawString( inputs.get(i), stringX, stringY);
angel*i+spinAngel + " and " + (angel+extra)
What comes to mind when I look at these angle values is:
If the value of (angle * i + spinAngel) is greater than 0 and less than 180, I should set yMultiplier to -1. Because this means that the desired Arc is located on the upper side of the circle, and I need to write the String on the upper side of the circle.
I applied the same thought process to xMultiplier, but I couldn't achieve the desired result.