JavaFX Scene/background Color: How to Change? - java

How do I change the background color of this scene? what am I missing? I tried the following:
This command actually resolves / has no error BUT it doesn't change the color.
scene.setFill(Color.GRAY);
This command also resolves / has no error but it also doesn't change the color.
Scene scene = new Scene(pane, 250, 250, Color.GRAY);
Thank you for your response.
CODE: ===================================================
...
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
public class DisplayResizableClock extends Application {
#Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a clock and a label
ClockPane2 clock = new ClockPane2();
//clock.setF;
String timeString = clock.getHour() + ":" + clock.getMinute()
+ ":" + clock.getSecond();
Label lblCurrentTime = new Label(timeString);
// Place clock and label in border pane
BorderPane pane = new BorderPane();
pane.setCenter(clock);
pane.setBottom(lblCurrentTime);
BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane, 250, 250);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Display Resizable Clock"); // Set the stage title===========
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
pane.widthProperty().addListener(ov ->
clock.setWidth(pane.getWidth())
);
pane.heightProperty().addListener(ov ->
clock.setHeight(pane.getHeight())
);
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
//=====================
class ClockPane2 extends Pane {
private int hour;
private int minute;
private int second;
/** Construct a default clock with the current time*/
public ClockPane2() {
setCurrentTime();
}
/** Construct a clock with specified hour, minute, and second */
public ClockPane2(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
/** Return hour */
public int getHour() {
return hour;
}
/** Set a new hour */
public void setHour(int hour) {
this.hour = hour;
paintClock();
}
/** Return minute */
public int getMinute() {
return minute;
}
/** Set a new minute */
public void setMinute(int minute) {
this.minute = minute;
paintClock();
}
/** Return second */
public int getSecond() {
return second;
}
/** Set a new second */
public void setSecond(int second) {
this.second = second;
paintClock();
}
/* Set the current time for the clock */
public void setCurrentTime() {
// Construct a calendar for the current date and time
Calendar calendar = new GregorianCalendar();
// Set current hour, minute and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock(); // Repaint the clock
}
/** Paint the clock */
private void paintClock() {
// Initialize clock parameters
double clockRadius =
Math.min(getWidth(), getHeight()) * 0.8 * 0.5;
double centerX = getWidth() / 2;
double centerY = getHeight() / 2;
// Draw circle
Circle circle = new Circle(centerX, centerY, clockRadius);
circle.setFill(Color.YELLOW); //=====changed color==============
circle.setStroke(Color.BLACK);
Text t1 = new Text(centerX - 5, centerY - clockRadius + 12, "12");
Text t2 = new Text(centerX - clockRadius + 3, centerY + 5, "9");
Text t3 = new Text(centerX + clockRadius - 10, centerY + 3, "3");
Text t4 = new Text(centerX - 3, centerY + clockRadius - 3, "6");
// Draw second hand
double sLength = clockRadius * 0.8;
double secondX = centerX + sLength *
Math.sin(second * (2 * Math.PI / 60));
double secondY = centerY - sLength *
Math.cos(second * (2 * Math.PI / 60));
Line sLine = new Line(centerX, centerY, secondX, secondY);
sLine.setStroke(Color.RED);
// Draw minute hand
double mLength = clockRadius * 0.65;
double xMinute = centerX + mLength *
Math.sin(minute * (2 * Math.PI / 60));
double minuteY = centerY - mLength *
Math.cos(minute * (2 * Math.PI / 60));
Line mLine = new Line(centerX, centerY, xMinute, minuteY);
mLine.setStroke(Color.BROWN); //changed color to brown======================
// Draw hour hand
double hLength = clockRadius * 0.5;
double hourX = centerX + hLength *
Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
double hourY = centerY - hLength *
Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12));
Line hLine = new Line(centerX, centerY, hourX, hourY);
hLine.setStroke(Color.GREEN);
getChildren().clear(); // Clear the pane
getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
Group ticks = new Group();//create tick hands============================
Group numbers = new Group(); //create numbers==========================
// creating the big ticks (12)===============================
for (int i = 0; i < 12; i++) {
/*creating a line with a width of 10 and placing at 'clockRadius'
distance away from center*/
Line tick = new Line(0, clockRadius, 0, clockRadius - 10);
tick.setTranslateX(centerX);
tick.setTranslateY(centerY);
//applying proper rotation to rotate the tick
tick.getTransforms().add(new Rotate(i * (360 / 12)));
//adding to ticks group
ticks.getChildren().add(tick);
}
// creating the small ticks=========================================
for (int i = 0; i < 60; i++) {
//lines will have a width of 5
Line tick = new Line(0, clockRadius, 0, clockRadius - 5);
tick.setTranslateX(centerX);
tick.setTranslateY(centerY);
tick.getTransforms().add(new Rotate(i * (360 / 60)));
ticks.getChildren().add(tick);
}
// creating the numbers==================================================
int num = 12; // starting with 12
for (int i = 0; i < 12; i++) {
//finding proper position x and y by applying the equation
double x = centerX + (clockRadius - 20) * Math.sin((i % 12) * (2 * Math.PI / 12));
double y = centerY - (clockRadius - 20) * Math.cos((i % 12) * (2 * Math.PI / 12));
//defining a text with hour label, (x-5 and y+5 are used to align text
//in proper position, considering font height & width)
Text t = new Text(x - 5, y + 5, "" + num);
numbers.getChildren().add(t);
num++;
if (num > 12) {
num = 1;
}
}
// adding ticks and numbers======================
getChildren().add(ticks);
getChildren().add(numbers);
}
#Override
public void setWidth(double width) {
super.setWidth(width);
paintClock();
}
#Override
public void setHeight(double height) {
super.setHeight(height);
paintClock();
}
}
...

I have looked all over the internet to figure out what the issue was as well. But, I managed to find a way to change the scene.
Instead of using the setfill() method for the scene, use the node's setStyle() method (in this case BorderPane).
For example, if you use something like:
pane.setStyle("-fx-background-color: grey;");
It should set the color of the pane, which is inside the scene, to grey. The best part is that the command can be placed before or after setting the scene to work.
Here is an image of one of my scenes after using the command to get a black background as an example:

Related

reposition object in circle

As you can see on the image, I have a p1 and p2 objects with (x,y) coordinates which I know the values, and I know radius of all these circle objects.
However, I want to calculate new position x,y which would be p3 center point. Basically, as you can see it's p2 position + radius.
I am doing this for java game which is based on libgdx. I would appreciate any math or java language directions/examples.
See code comments for explanation.
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.*;
class CenteredCircle extends Ellipse2D.Double {
CenteredCircle(Point2D.Double p, double radius) {
super(p.x - radius, p.y - radius, 2 * radius, 2 * radius);
}
}
public class CircleDemo extends JFrame {
public CircleDemo() {
int width = 640; int height = 480;
setSize(new Dimension(width, height));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JPanel p = new JPanel() {
#Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// center p1
Point2D.Double p1 = new Point2D.Double(getSize().width/2, getSize().height/2);
double radius = 130.0;
// big circle
Shape circle2 = new CenteredCircle(p1, radius);
g2d.draw(circle2);
// 12 small circles
for (int angle = 0; angle < 360; angle += 30) {
// this is the magic part
// a polar co-ordinate has a length and an angle
// by changing the angle we rotate
// the transformed co-ordinate is the center of the small circle
Point2D.Double newCenter = polarToCartesian(radius, angle);
// draw line just for visualization
Line2D line = new Line2D.Double(p1.x, p1.y, p1.x + newCenter.x, p1.y+ newCenter.y);
g2d.draw(line);
// draw the small circle
Shape circle = new CenteredCircle(
new Point2D.Double(p1.x + newCenter.x, p1.y + newCenter.y),
radius/4);
g2d.draw(circle);
}
}
};
setTitle("Circle Demo");
getContentPane().add(p);
}
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CircleDemo();
}
});
}
static Point2D.Double polarToCartesian(double r, double theta) {
theta = (theta * Math.PI) / 180.0; // multiply first, then divide to keep error small
return new Point2D.Double(r * Math.cos(theta), r * Math.sin(theta));
}
// not needed, just for completeness
public static Point2D.Double cartesianToPolar(double x, double y) {
return new Point2D.Double(Math.sqrt(x * x + y * y), (Math.atan2(y, x) * 180) / Math.PI);
}
}
Now using libgdx for the graphics. Thus no need for polar co-ordinates, on the outside.
I am not doing frame rate relative animation. Therefore, this is no perfect match to your code.
Using the following calculation (if (theta >= 360) { theta = 0.0f; }) at the end of the render method will let the animation restart with its original value.
package org.demo;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public class CircleDemo extends ApplicationAdapter {
ShapeRenderer shapeRenderer;
float theta = 0.0f;
#Override
public void create () {
shapeRenderer = new ShapeRenderer();
}
#Override
public void render () {
ScreenUtils.clear(0, 0.4f, 0.4f, 1);
Vector2 p1 = new Vector2( Gdx.graphics.getWidth() / 2.0f , Gdx.graphics.getHeight() / 2.0f);
Vector2 smallCircleCenter = new Vector2(150.0f, 0.0f);
smallCircleCenter.add(p1); // translate center by p1
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// static lines and circles
for (int angle = 0; angle < 360; angle += 30) {
Vector2 lineEnd = new Vector2(smallCircleCenter);
lineEnd.rotateAroundDeg(p1, angle);
shapeRenderer.line(p1, lineEnd);
shapeRenderer.circle(lineEnd.x, lineEnd.y, 20);
}
// animated line and circle in red
shapeRenderer.setColor(0.75f, 0, 0, 1);
Vector2 movingCircleCenter = new Vector2(smallCircleCenter);
movingCircleCenter.rotateAroundDeg(p1, theta);
shapeRenderer.line(p1, movingCircleCenter);
shapeRenderer.circle(movingCircleCenter.x, movingCircleCenter.y, 20);
shapeRenderer.setColor(1, 1, 1, 1);
shapeRenderer.end();
theta++;
// for the screenshot stop at 90 degrees
if (theta >= 90) {
theta = 90.0f;
}
}
#Override
public void dispose () {
shapeRenderer.dispose();
}
}
So I wrote a test in my project, based on your approach:
package com.bigbang.test.impl;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.bigbang.Game;
import com.bigbang.graphics.g2d.shapes.impl.Ellipse;
import com.bigbang.graphics.g2d.shapes.impl.Line;
import com.bigbang.graphics.gl.Color;
import com.bigbang.math.BBMath;
public class PolarToCartesianTest extends AbstractTest {
private Array<GraphicalObject> graphicalObjectArray;
private GraphicalObject dynamicGraphicalObject;
private float radius, smallCircleRadius;
private float centerX, centerY;
public PolarToCartesianTest(Game game) {
super(game);
}
#Override
public void create() {
radius = 200f;
centerX = game.getScreenController().getScreenWidth() / 2;
centerY = game.getScreenController().getScreenHeight() / 2;
smallCircleRadius = radius / 4;
graphicalObjectArray = new Array<>();
for (int angle = 0; angle < 360; angle += 30) {
GraphicalObject graphicalObject = new GraphicalObject();
graphicalObject.angle = angle;
graphicalObjectArray.add(graphicalObject);
}
dynamicGraphicalObject = new GraphicalObject();
game.getCameraController().getCamera().position.x = game.getScreenController().getScreenWidth() / 2;
game.getCameraController().getCamera().position.y = game.getScreenController().getScreenHeight() / 2;
}
#Override
public void update(float deltaTime) {
for (GraphicalObject graphicalObject : graphicalObjectArray) {
Vector2 polarToCartesianPosition = BBMath.polarToCartesian(radius, graphicalObject.angle);
graphicalObject.line.x1 = centerX + 0;
graphicalObject.line.y1 = centerY + 0;
graphicalObject.line.x2 = centerX + polarToCartesianPosition.x;
graphicalObject.line.y2 = centerY + polarToCartesianPosition.y;
graphicalObject.line.color = Color.WHITE_COLOR;
graphicalObject.ellipse.x = centerX + polarToCartesianPosition.x;
graphicalObject.ellipse.y = centerY + polarToCartesianPosition.y;
graphicalObject.ellipse.width = 2 * smallCircleRadius;
graphicalObject.ellipse.height = 2 * smallCircleRadius;
graphicalObject.ellipse.color = Color.WHITE_COLOR;
}
float shift = 0;
float theta = (shift * smallCircleRadius) * (centerY / centerX);
Vector2 pos = BBMath.polarToCartesian(radius, theta);
dynamicGraphicalObject.line.color = new Color(Color.RED);
dynamicGraphicalObject.line.x1 = centerX + 0;
dynamicGraphicalObject.line.y1 = centerY + 0;
dynamicGraphicalObject.line.x2 = centerX + pos.x;
dynamicGraphicalObject.line.y2 = centerY + pos.y;
dynamicGraphicalObject.ellipse.x = centerX + pos.x;
dynamicGraphicalObject.ellipse.y = centerY + pos.y;
dynamicGraphicalObject.ellipse.width = 2 * smallCircleRadius;
dynamicGraphicalObject.ellipse.height = 2 * smallCircleRadius;
dynamicGraphicalObject.ellipse.color = new Color(Color.RED);
}
#Override
public void draw() {
game.getShapeRenderer().begin(ShapeRenderer.ShapeType.Line);
for (GraphicalObject graphicalObject : graphicalObjectArray) {
graphicalObject.line.draw();
graphicalObject.ellipse.draw();
}
dynamicGraphicalObject.line.draw();
dynamicGraphicalObject.ellipse.draw();
game.getShapeRenderer().end();
}
class GraphicalObject {
Ellipse ellipse;
Line line;
float angle;
public GraphicalObject() {
this.ellipse = new Ellipse(game);
this.line = new Line(game);
}
}
}
Which is same math like in your example, with some modifications:
However, you can notice I have this dynamicGraphicalObject (red circle), which I want to shift position around circle by using theta value calculated as (shift * smallCircleRadius) * (centerY / centerX);. This works perfect for shift=0 value. It's properly positioned/overlapping white. But if I would change shift variable to 1, 2, 3, or 11, you can see that it's not precisely aligned with white circles. Is this floating point issue or am I missing something in calculation of theta ?
shift values used: 2,6 and 11 in order by images
--
SOLUTION:
float fixPrecision = 1.1f;
float theta = (shift * fixPrecision) + ((shift * smallCircleRadius) * (centerY / centerX));

Set an analog clock with an initial time using Java

For my programming class I'm working on a clock. The clock has to be set at an initial time, which I cannot figure out how to do. The clock I'm currently working with just uses the system time. I've tried setting a time using cal.set but then it just freezes on what I want to be the initial time and time doesn't progress. How can I edit this to allow me to have a set initial time, but have the clock still work?
package Clock;
package Clock;
import java.applet.Applet;
import java.awt.Color;
import java.lang.Object;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Component extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
public static String name = "My Clock";
public static int size = 600;
public static boolean isRunning = false;
public static Graphics g;
public static Image screen;
public Numbers number;
public static JFrame frame;
public static void main(String [] args) {
Component component = new Component();
frame = new JFrame();
frame.add(component);
frame.setSize(size+6, size + 28);
frame.setTitle(name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
component.start();
}
public void start() {
requestFocus();
number = new Numbers();
isRunning = true;
Thread th = new Thread(this);
th.start();
}
public void run() {
screen = createVolatileImage(size, size);
while (isRunning) {
tick();
render(g);
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void tick() {
}
public double time;
public int anim;
public int anim2;
public int anim3;
public int anim4;
public int center = size/2;
public int radius = (size-40)/2;
public void render(Graphics g) {
// Drawing to image
screen = createImage(size, size);
g = screen.getGraphics();
//Drawing the background
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, size, size);
// Drawing the frame(outside circle)
g.setColor(Color.black);
g.fillOval(5, 5, size - 10, size - 10);
g.setColor(Color.white);
//g.setColor(new Color(new Random().nextInt(255). new Random().nextInt(255), new Random().nextInt(255)));
//g.drawOval(10, 10, size - 20, size - 20);
g.fillOval(20, 20, size - 40, size - 40);
number.render(g);
// Math and Drawing for Lines
for (int i = 0; i < 60; i++) {
radius = size - 40;
anim = center + (int) ((Math.sin(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
anim2 = center - (int) ((Math.cos(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
radius = size - 60;
anim3 = center + (int) ((Math.sin(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
anim4 = center - (int) ((Math.cos(i % 60.0 / 60 * Math.PI * 2) * (radius / 2)));
g.drawLine(anim, anim2, anim3, anim4);
}
// Math for hour hand
radius = size - 140;
// time = System.currentTimeMillis() % 3600000 / 3600000 * Math.PI;
int t = (int) (System.currentTimeMillis() + 17300000) + 3600000+ 3600000 + 3600000 + 3600000 + 3600000 + 3600000 + 3600000 + 3600000;
anim = center
+ (int) ((Math.sin(t % 43200000.0
/ 43200000 * Math.PI * 2) * (radius / 2))) + 7;
anim2 = center
- (int) ((Math.cos(t % 43200000.0
/ 43200000 * Math.PI * 2) * (radius / 2))) + 7;
// Drawing the hour hand
g.setColor(Color.black);
g.fillOval(center - 8, center - 8, 16, 16);
g.drawLine(center, center, anim, anim2);
g.drawLine(center + 1, center, anim + 1, anim2);
g.drawLine(center, center + 1, anim, anim2 + 1);
g.drawLine(center - 1, center, anim - 1, anim2);
g.drawLine(center, center - 1, anim, anim2 - 1);
g.drawLine(center + 1, center + 1, anim, anim2);
g.drawLine(center + 1, center - 1, anim, anim2);
g.drawLine(center - 1, center + 1, anim, anim2);
g.drawLine(center - 1, center - 1, anim, anim2);
// Math for minute hand
radius = size - 90;
// time = System.currentTimeMillis() % 3600000 / 3600000 * Math.PI;
anim = center
+ (int) ((Math.sin(System.currentTimeMillis() % 3600000.0
/ 3600000 * Math.PI * 2) * radius / 2));
anim2 = center
- (int) ((Math.cos(System.currentTimeMillis() % 3600000.0
/ 3600000 * Math.PI * 2) * radius / 2));
// Drawing the minute hand
g.setColor(Color.black);
g.drawLine(center, center, anim, anim2);
g.drawLine(center + 1, center, anim + 1, anim2);
g.drawLine(center, center + 1, anim, anim2 + 1);
g.drawLine(center - 1, center, anim - 1, anim2);
g.drawLine(center, center - 1, anim, anim2 - 1);
//Math for second hand
DateFormat dateFormat = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
String s = dateFormat.format(cal.getTime());
radius = size - 70;
// time = System.currentTimeMillis() % 60000 / 60000 * Math.PI;
anim = center
+ (int) ((Math.sin(Integer.parseInt(s) % 60.0 / 60 * Math.PI
* 2) * (radius / 2)));
anim2 = center
- (int) ((Math.cos(Integer.parseInt(s) % 60.0 / 60 * Math.PI
* 2) * (radius / 2)));
// Drawing the second hand
g.setColor(Color.red);
g.drawLine(center, center, anim, anim2);
g.drawLine(center + 1, center, anim + 1, anim2);
g.drawLine(center, center + 1, anim, anim2 + 1);
g.drawLine(center - 1, center, anim - 1, anim2);
g.drawLine(center, center - 1, anim, anim2 - 1);
// Center circle
g.fillOval(center - 5, center - 5, 10, 10);
g.setColor(Color.black);
g.fillOval(center - 2, center - 2, 4, 4);
// g.setColor(new Color(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255)));
// g.fillRect(0, 0, getWidth(), getHeight());
// Drawing to screen
g = getGraphics();
g.drawImage(screen, 0, 0, size, size, this);
g.dispose();
}
}
and
package Clock;
import java.applet.Applet;
import java.awt.Color;
import java.lang.Object;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Numbers extends JPanel {
private static final long serialVersionUID=1L;
public int size = Component.size;
public int center = size/2;
public void setFont(Font font) {
super.setFont(font);
repaint();
}
public String getDay() {
DateFormat dateFormat=new SimpleDateFormat("dd");
Calendar cal = Calendar.getInstance();
String s = dateFormat.format(cal.getTime());
int day = Integer.parseInt(s);
String d = day + "";
// sets ordinal indicator
switch(day) {
case 1:
case 21:
case 31:
d += "st";
break;
case 2:
case 22:
d += "nd";
break;
case 3:
case 23:
d += "rd";
break;
default:
d += "th";
}
return d;
}
public String getHour() {
DateFormat dateFormat = new SimpleDateFormat("HH");
Calendar cal = Calendar.getInstance();
String s = dateFormat.format(cal.getTime());
int hour = Integer.parseInt(s) -1;
String n = hour + "";
if(hour < 10) {
n=hour+"";
}
return n;
}
public String getMonth() {
DateFormat dateFormat = new SimpleDateFormat("MM");
Calendar cal = Calendar.getInstance();
String s = dateFormat.format(cal.getTime());
int month = Integer.parseInt(s);
// sets month name to number
String m = "";
switch (month)
{
case 1:
m= "January";
break;
case 2:
m= "February";
break;
case 3:
m= "March";
break;
case 4:
m= "April";
break;
case 5:
m= "May";
break;
case 6:
m= "June";
break;
case 7:
m= "July";
break;
case 8:
m= "August";
break;
case 9:
m= "September";
break;
case 10:
m= "October";
break;
case 11:
m= "November";
break;
case 12:
m= "December";
}
return m;
}
public void render(Graphics g) {
g.setColor(Color.black);
DateFormat dateFormat = new SimpleDateFormat(":mm:ss");
Calendar cal = Calendar.getInstance();
String s = dateFormat.format(cal.getTime());
int n = center - ((s.length() *13)/2);
//265
g.setFont(new Font("Arial", 1, 20));
s = (Integer.parseInt(getHour(), 10) % 12 + 1) + "" + dateFormat.format(cal.getTime());
n = center - (s.length() * 10 / 2);
g.setColor(Color.DARK_GRAY);
g.fillRoundRect(250, 348, 100, 30, 6, 6);
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(252, 350, 96, 26, 6, 6);
g.setColor(Color.BLACK);
g.drawString("TIME", 275, 345);
g.drawString("DATE", 275, 225);
g.drawString("AM", 255, 150);
g.drawString("PM", 315, 150);
g.drawString(s, n, 370);
int p = Integer.parseInt(getHour(), 10);
if(p < 11 || p == 24) {
g.fillOval(265, 160, 10, 10);
g.drawOval(325, 160, 10, 10);
} else {
g.drawOval(265, 160, 10, 10);
g.fillOval(325, 160, 10, 10);
}
dateFormat = new SimpleDateFormat("yyyy");
cal = Calendar.getInstance();
s = getMonth() + " " + getDay() + ", " + dateFormat.format(cal.getTime());
n = center - (int) ((s.length() * 10.25) / 2);
g.setColor(Color.DARK_GRAY);
g.fillRoundRect(200, 228, 200, 30, 6, 6);
g.setColor(Color.LIGHT_GRAY);
g.fillRoundRect(202, 230, 196, 26, 6, 6);
g.setColor(Color.BLACK);
g.drawString(s, n, 250);
s = Component.name;
n=center - (int)((s.length()*10)/2);
g.drawString(s, n , 450);
g.setFont(new Font("Arial", 1, 30));
int radius = size - 100;
for(int i = 0; i < 12; i++) {
double anim = (int) ((Math.sin((i+1) % 12.0 / 12 * Math.PI * 2) * (radius / 2)));
double anim2 = (int) ((Math.cos((i+1) % 12.0 / 12 * Math.PI * 2) * (radius / 2)));
if(i >= 9){
anim -= 10;
}
g.drawString((i+1) + "", center + (int) anim - 6, center - (int) anim2 + 12);
}
}
}
Lot's of issues -- where to even begin?
You've named a class Component, a name that will easily cause conflicts with a key Java core GUI class, java.awt.Component. Rename it to something else.
This same class extends Applet but is not being used as an Applet. Rather it is being used as a component to be added to a JFrame -- this makes no sense, trying to add one top-level window into another, and would require some justification as to why you're doing it in such a strange fasion. Why not extend JPanel or JComponent something else that makes more sense?
You're using a Graphics field and drawing to it, something that risks NullPointerException. Instead Google and read the Swing drawing tutorials and follow their lead -- draw passively within the paintComponent method of a JPanel. There are other important details to understand which the tutorials will show and tell you.
You're drawing with a Graphics object obtained by calling getGraphics() on a component, something that will lead to unstable drawings and possible NullPointerExceptions. Again read the tutorials on how to do this correctly.
You're making Swing calls in a background thread, something that can lead to intermittent difficult to debug threading errors. Use a Swing Timer to make things easier on yourself. Google the tutorial for the gory details.
You appear to be creating a JPanel, Numbers, but aren't adding it to the GUI (that I can tell), but rather are trying to render it in a strange way -- why, I have no idea. Don't do this. Again do graphics as per the Swing drawing tutorials. Here's the links:
Lesson: Performing Custom Painting: introductory tutorial to Swing graphics
Painting in AWT and Swing: advanced tutorial on Swing graphics
..... more

Calculate 'interval and increment' for slide in animation in Java Swing

I'm building an application which has a slideshow in its homepage, currently I use Thread.sleep(10) and add/sub the x position of panel I want to slide.
For example: slideIn(30, panel_1, 10) < this will cause panel_1 to slide in with interval of 30ms and subtracts its x by 10 overtime until the panel is in center/occupy the slideshow_panel. But the con of this method is that the sliding animation won't smooth, I want the sliding animation/transition as smooth as Bootstrap Carousel. Is there a way to calculate the speed and increment/decrement value for slide transition speed?
Actually, I've something that's almost perfect for this. I assume you can create a Path2D for your animation path, right? And it also seems like you want a constant speed. There are a couple of references to my project (http://sourceforge.net/p/tus/code/HEAD/tree/) for calculating distance and showing the JPanel for instance, but it shouldn't be hard to remove them and replace with standard java. Try it out
public abstract class PathAnimation {
private Path2D mPath;
private double totalLength;
/**
* Be careful to call path.closePath before invoking this constructor
* #param path
*/
public PathAnimation(Path2D path) {
mPath = path;
totalLength = 0;
PathIterator iterator = mPath.getPathIterator(null);
//Point2D currentLocation;// = path.getCurrentPoint();
double[] location = new double[6];
iterator.currentSegment(location);
while (!iterator.isDone()) {
double[] loc = new double[6];
iterator.next();
iterator.currentSegment(loc);
if (loc[0] == 0 && loc[1] == 0) continue;
double distance = MathUtils.distance(location[0], location[1], loc[0], loc[1]);
totalLength += distance;
location = loc;
}
}
#Override
public Point2D getLocationAtTime(int time) {
return getLocationAtTime(time / (double) getTotalAnimationTime());
}
public Point2D getLocationAtTime(double pctTime) {
double len = totalLength * pctTime;
PathIterator iterator = mPath.getPathIterator(null);
double[] location = new double[6];
iterator.currentSegment(location);
while (!iterator.isDone()) {
double[] loc = new double[6];
iterator.next();
iterator.currentSegment(loc);
double distance= MathUtils.distance(location[0], location[1], loc[0], loc[1]);
if (distance > len) {
double pctThere = len / distance;
double xSpot = location[0] * (1 - pctThere) + loc[0] * pctThere;
double ySpot = location[1] * (1 - pctThere) + loc[1] * pctThere;
return new Point2D.Double(xSpot, ySpot);
}
len -= distance;
location = loc;
}
throw new ArrayIndexOutOfBoundsException("Path is too short or time is too long!");
}
/**
* Number of milliseconds that this animation spans
* #return
*/
public abstract int getTotalAnimationTime();
public static void main(String args[]) {
Rectangle rect = new Rectangle(10,10,20,20);
final Path2D.Double myPath = new Path2D.Double((Shape)rect);
myPath.closePath();
final PathAnimation myAnimation = new PathAnimation(myPath) {
Area star = new Area(PaintUtils.createStandardStar(15, 15, 5, .5, 0));
#Override
public Dimension getSizeAtTime(int time) {
return new Dimension(15,15);
}
#Override
public void paintAtTime(Graphics2D g, int time) {
Area toPaint = star;
if ((time / 150) % 2 == 1) {
Dimension size = getSizeAtTime(0);
toPaint = new Area(toPaint);
PaintUtils.rotateArea(toPaint, Math.PI / 6);
}
g.setColor(Color.YELLOW);
g.fill(toPaint);
g.setColor(Color.RED);
g.draw(toPaint);
}
#Override
public int getTotalAnimationTime() {
return 10000;
}
};
System.out.println(myAnimation.getLocationAtTime(0));
System.out.println(myAnimation.getLocationAtTime(2500));
System.out.println(myAnimation.getLocationAtTime(4000));
System.out.println(myAnimation.getLocationAtTime(5000));
System.out.println(myAnimation.getLocationAtTime(7000));
System.out.println(myAnimation.getLocationAtTime(7500));
System.out.println(myAnimation.getLocationAtTime(9000));
System.out.println(myAnimation.getLocationAtTime(10000));
final JPanel jp = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int time = ((int) System.currentTimeMillis()) % myAnimation.getTotalAnimationTime();
int time2 = (time + myAnimation.getTotalAnimationTime() / 2) % myAnimation.getTotalAnimationTime();
Point2D pt = myAnimation.getLocationAtTime(time);
Point2D pt2 = myAnimation.getLocationAtTime(time2);
Dimension size = myAnimation.getSizeAtTime(time);
g2.translate(pt.getX() - size.width / 2, pt.getY() - size.height / 2);
myAnimation.paintAtTime(g2, time);
g2.translate(- (pt.getX() - size.width / 2), - (pt.getY() - size.height / 2));
g2.translate(pt2.getX() - size.width / 2, pt2.getY() - size.height / 2);
myAnimation.paintAtTime(g2, time2);
g2.translate(- (pt2.getX() - size.width / 2), - (pt2.getY() - size.height / 2));
g2.setColor(Color.BLACK);
g2.draw(myPath);
}
};
WindowUtilities.visualize(jp);
AbstractAction action = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
jp.repaint();
}
};
javax.swing.Timer t = new javax.swing.Timer(30, action);
t.start();
}
}

why Timer() will delay in java?

I have coded a clock using Java,
it will display the clock includes seconds,
this is how I set the Timer
Timer timer = new Timer(1000, new TimerListener());
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
repaint() will work on the Clock panel.
the program will display clock normally,
the only problem is that the time on the clock is 1 or 2 seconds slower than real time.
then i change the response time to 100,
Timer timer = new Timer(100, new TimerListener());
this time there is no delay compare to real time.
I just don't understand why. Can somebody explain to me how Timer works, thank you.
ps.
this is my Clock code
public class Clock extends JPanel {
public double hour;
public double minute;
public double second;
public Clock() {
setFont(Common.SetFont.font);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
FontMetrics fm = g.getFontMetrics(Common.SetFont.font);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(xCenter, yCenter) * 0.7);
int radius1 = (int)(radius * 0.8);
g.drawOval(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);
for (int i = 1; i <= 12; i++) {
g.drawString(i + "", (int)(xCenter + radius1 * Math.sin(i * Math.PI / 6)) - (int)(fm.stringWidth(i + "") / 2), (int)(yCenter - radius1 * Math.cos(i * Math.PI / 6)) + (int)(fm.getAscent() / 2));
}
Calendar calendar = new GregorianCalendar();
hour = calendar.get(calendar.HOUR);
minute = calendar.get(calendar.MINUTE);
second = calendar.get(calendar.SECOND);
// draw hour line
g.drawLine(xCenter, yCenter, xCenter + (int)(radius1 * 2 / 5 * Math.sin(2 * Math.PI * (hour + minute / 60) / 12)), yCenter - (int)(radius1 * 2 / 5 * Math.cos(2 * Math.PI * (hour + minute / 60) / 12)));
//draw minute line
g.drawLine(xCenter, yCenter, xCenter + (int)(radius1 * 3 / 5 * Math.sin(2 * Math.PI * (minute + second / 60) / 60)), yCenter - (int)(radius1 * 3 / 5 * Math.cos(2 * Math.PI * (minute + second / 60) / 60)));
//draw second line
g.drawLine(xCenter, yCenter, xCenter + (int)(radius1 * 5 / 6 * Math.sin(2 * Math.PI * (second) / 60)), yCenter - (int)(radius1 * 5 / 6 * Math.cos(2 * Math.PI * (second) / 60)));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(2000, 1000);
}
}
and this is the code of Clock Display
public class jClockAnimation extends JFrame {
Common.Clock clock = new Common.Clock();
public jClockAnimation() {
Timer timer = new Timer(100, new TimerListener());
add(clock, BorderLayout.NORTH);
add(new MessagePanel(), BorderLayout.SOUTH);
timer.start();
}
public static void main(String[] args) {
new Common.SetFrame(new jClockAnimation(), "Clock");
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
class MessagePanel extends JPanel {
public MessagePanel() {
setFont(Common.SetFont.font);
}
#Override
protected void paintComponent(Graphics g) {
String string = new String((int)clock.hour + ": " + (int)clock.minute + ": " + (int)clock.second);
FontMetrics fm = g.getFontMetrics();
g.drawString(string, getWidth() / 2 - fm.stringWidth(string) / 2, getHeight() / 2 + fm.getAscent() / 2);
}
#Override
public Dimension preferredSize() {
return new Dimension(400, 200);
}
}
}

How do i align this text correctly?

I wrote this polar clock today and i am almost finished exept i want to align my text inside the line similar to this. Does anyone know how to do this? Ive tried to use FontRenderContext and font metrics but i cant seem to get it to work. Here is the whole source code so you can compile it and see for yourselves.
import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.Calendar;
import java.util.TimeZone;
public class Clock extends Applet implements Runnable {
int[][] colorsInt = {{20,20,20},{100,100,50},{50,100,100},{10,170,50},{79,29,245},{24,69,234},{253,24,103}};
Color[] colors;
int size;
int radius;
boolean anitalias = false;
static final float HPI = (float)(Math.PI / 180f);
public void start() {
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
new Thread(this).start();
}
public void run() {
setSize(500, 500); // For AppletViewer, remove later.
// Set up the graphics stuff, double-buffering.
BufferedImage screen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)screen.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
WritableRaster wr = screen.getRaster();
Graphics appletGraphics = getGraphics();
// Some variables to use for the fps.
long fpstn = 1000000000 / 600;
int tick = 0, fps = 0, acc = 0;
long lastTime = System.nanoTime();
// Vars
Calendar c;
size = 500;
radius = size / 2;
Arc2D.Float arch;
float scale, radians;
long miliSecond;
int second, minute, hour, month, year, dayOfWeek, dayOfMonth, dayOfYear, daysInMonth, daysInYear;
float[] tvars = new float[6];
float[] vars = new float[6];
String[] names = new String[6];
FontMetrics fm = g.getFontMetrics();
Font font = g.getFont();
FontRenderContext frc = g.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "Hello world");
int length = gv.getNumGlyphs();
// Init
initColors();
for (int i = 0; i < vars.length; i++)
vars[i] = 0;
// Game loop.
while (true) {
long now = System.nanoTime();
acc += now - lastTime;
tick++;
if (acc >= 1000000000L) {
acc -= 1000000000L;
fps = tick;
tick = 0;
}
// Update
c = Calendar.getInstance();
miliSecond = c.get(Calendar.MILLISECOND);
second = c.get(Calendar.SECOND);
minute = c.get(Calendar.MINUTE);
hour = c.get(Calendar.HOUR_OF_DAY);
dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
dayOfYear = c.get(Calendar.DAY_OF_YEAR);
dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
month = c.get(Calendar.MONTH);
daysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
daysInYear = c.getActualMaximum(Calendar.DAY_OF_YEAR);
tvars[0] = (second * 1000 + miliSecond) / 60000f * 360f;
tvars[1] = (minute * 60f + second) / 3600f * 360f;
tvars[2] = (hour * 60f + minute) / 1440f * 360f;
tvars[3] = ((dayOfWeek - 2) * 24f + hour) / 168f * 360f;
tvars[4] = ((dayOfMonth - 1) * 24f + hour) / (daysInMonth * 24f) * 360f;
tvars[5] = dayOfYear / (float)daysInYear * 360f;
for (int i = 0; i < vars.length; i++) {
if (tvars[i] - vars[i] > 1) {
vars[i] += (tvars[i] - vars[i]) / 15;
} else if(tvars[i] - vars[i] < -1) {
vars[i] -= (vars[i] - tvars[i]) / 15;
} else {
vars[i] = tvars[i];
}
}
names[0] = second + " Second" + (second > 1 ? "s" : "");
lastTime = now;
// Render
g.setColor(colors[0]);
g.fillRect(0, 0, size, size);
for (int i = 0; i < vars.length; i++) {
scale = i / (float)vars.length * radius * 1.7f;
g.setColor(colors[0]);
g.fillOval((int)(scale / 2), (int)(scale / 2), (int)(size - scale), (int)(size - scale));
g.setColor(colors[i + 1]);
scale += 15;
arch = new Arc2D.Float(scale / 2, scale / 2, size - scale, size - scale, 450 - vars[i], vars[i], Arc2D.PIE);
g.fill(arch);
g.setColor(Color.WHITE);
radians = (vars[i]) * HPI;// vars[i] - 90
scale = ((float)(vars.length - i) / (float)vars.length * (float)radius / 2f * 1.7f) + 15f;
g.translate(radius, radius);
System.out.println(i + ": " + ((1 - scale / radius) * 2));
for (int j = 0; j < names[0].length(); j++) {
char ch = names[0].charAt(j);
radians = ((vars[i] - (names[0].length() - j) * 2) * (1 + (1 - scale / radius) * 2)) * HPI;
g.rotate(radians);
g.drawString(ch + "", 0, -scale);
g.rotate(-radians);
}
g.translate(-radius, -radius);
/*float x = (float)Math.cos(radians) * scale;
float y = (float)Math.sin(radians) * (vars.length - i) / vars.length * radius / 2 * 1.7f;
g.drawRect((int)x + size / 2, (int)y + size / 2, 10, 10);*/
}
scale = vars.length / (float)vars.length * radius * 1.7f;
g.setColor(colors[0]);
g.fillOval((int)(scale / 2), (int)(scale / 2), (int)(size - scale), (int)(size - scale));
g.setColor(Color.WHITE);
g.drawString("FPS " + String.valueOf(fps), 20, 30);
// Draw the entire results on the screen.
appletGraphics.drawImage(screen, 0, 0, null);
do {
Thread.yield();
} while (System.nanoTime() - lastTime < 0);
if (!isActive()) {
return;
}
}
}
public void initColors() {
colors = new Color[colorsInt.length];
for (int i = 0; i < colors.length; i++) {
colors[i] = new Color(colorsInt[i][0], colorsInt[i][1], colorsInt[i][2]);
}
}
}
Here's a simple example of rotating text.
Addendum: You'll want to adjust the the text's radial starting point by stringWidth(name[n]). Your program appears to be rotating individual characters in a effort to follow the arc, while the example appears to be drawing the text in a straight line tangent to the arc. The latter approach may prove simpler. For example, this variation centers the labels across the arc's getStartPoint():
for (int i = 0; i < vars.length; i++) {
...
String s = names[0];
int w = fm.stringWidth(s);
int h = fm.getHeight() + fm.getMaxDescent();
Point2D p = arch.getStartPoint();
int x = (int) p.getX();
int y = (int) p.getY();
radians = (vars[i]) * HPI;
g.rotate(radians, x, y);
g.drawString(s, x - w / 2, y + h);
g.rotate(-radians, x, y);
}
For convenience the code above does rotate() to and fro; for comparison, here's the original example showing repeated concatenations of rotate():
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
/** #see http://stackoverflow.com/questions/6238037 */
public class RotateText extends JPanel {
private static final Font f = new Font("Serif", Font.BOLD, 32);
private static final String s = "Hello World!";
private static final Color[] colors = {
Color.red, Color.green, Color.blue, Color.cyan
};
private Graphics2D g2d;
private AffineTransform at;
public RotateText() {
setPreferredSize(new Dimension(400, 400));
}
#Override
public void paintComponent(Graphics g) {
g2d = (Graphics2D) g;
g2d.setFont(f);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, getWidth(), getHeight());
at = g2d.getTransform();
int w = this.getWidth();
int h = this.getHeight();
int w2 = g2d.getFontMetrics().stringWidth(s) / 2;
int h2 = 2 * g2d.getFontMetrics().getHeight() / 3;
render(0, w / 2 - w2, h - h2);
render(1, h2, h / 2 - w2);
render(2, w / 2 + w2, h2);
render(3, w - h2, h / 2 + w2);
g2d.setTransform(at);
g2d.setColor(Color.yellow);
g2d.fillRect(w / 3, h / 3, w / 3, h / 3);
}
private void render(int n, int x, int y) {
g2d.setColor(colors[n]);
g2d.setTransform(at);
g2d.rotate(n * Math.PI / 2, x, y);
g2d.drawString(s, x, y);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
//#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new RotateText(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
You have to be able to draw text along the curves. There are several ways to do it, but the simplest one is to use Stroke API. You can find an example at http://www.jhlabs.com/java/java2d/strokes/
The other way is using affine transforms. The example is at http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawtextalongacurve.htm

Categories