I've got 8 ovals all set to the same colour. After a second I want the first oval to change colours and then after another second i want the first oval to go back to its original colour and then change the second ovals colour. I've drawn the circles and I've tried implementing a thread but i think it's not executing...
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Circle extends JPanel implements Runnable
{
Graphics g;
Thread t = new Thread();
int[][] fillCircles = new int[8][4];
#Override
public void paintComponent(Graphics g)
{
this.g = g;
super.paintComponent(this.g);
this.g.setColor(new java.awt.Color(237, 54, 26));
this.g.drawOval(300, 50, 100, 100);
this.g.drawOval(450, 125, 100, 100);
this.g.drawOval(500, 250, 100, 100);
this.g.drawOval(425, 375, 100, 100);
this.g.drawOval(300, 400, 100, 100);
this.g.drawOval(175, 350, 100, 100);
this.g.drawOval(125, 225, 100, 100);
this.g.drawOval(175, 100, 100, 100);
this.g.fillOval(300, 50, 100, 100);
this.g.fillOval(450, 125, 100, 100);
this.g.fillOval(500, 250, 100, 100);
this.g.fillOval(425, 375, 100, 100);
this.g.fillOval(300, 400, 100, 100);
this.g.fillOval(175, 350, 100, 100);
this.g.fillOval(125, 225, 100, 100);
this.g.fillOval(175, 100, 100, 100);
fillCircles[0][0] = 300;
fillCircles[0][1] = 50;
fillCircles[0][2] = 100;
fillCircles[0][3] = 100;
fillCircles[1][0] = 450;
fillCircles[1][1] = 125;
fillCircles[1][2] = 100;
fillCircles[1][3] = 100;
fillCircles[2][0] = 500;
fillCircles[2][1] = 250;
fillCircles[2][2] = 100;
fillCircles[2][3] = 100;
fillCircles[3][0] = 425;
fillCircles[3][1] = 375;
fillCircles[3][2] = 100;
fillCircles[3][3] = 100;
fillCircles[4][0] = 300;
fillCircles[4][1] = 400;
fillCircles[4][2] = 100;
fillCircles[4][3] = 100;
fillCircles[5][0] = 175;
fillCircles[5][1] = 350;
fillCircles[5][2] = 100;
fillCircles[5][3] = 100;
fillCircles[6][0] = 125;
fillCircles[6][1] = 225;
fillCircles[6][2] = 100;
fillCircles[6][3] = 100;
fillCircles[7][0] = 175;
fillCircles[7][1] = 100;
fillCircles[7][2] = 100;
fillCircles[7][3] = 100;
}
Circle () {
t.start();
}
public void run () {
int circle = 0;
try {
for (;;) {
Thread.sleep(1000);
if (circle > 0) {
this.g.setColor(new java.awt.Color(237, 54, 26));
circle--;
this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]);
circle++;
}
this.g.setColor(Color.red);
this.g.fillOval(fillCircles[circle][0], fillCircles[circle][1], fillCircles[circle][2], fillCircles[circle][2]);
circle++;
if (circle == 8) {
circle = 0;
}
}
} catch (InterruptedException e) {
System.out.println ("Thread Interrupted");
}
}
public static void main(String[] args) {
Circle c;
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(c=new Circle());
application.setSize(1200, 900);
application.setVisible(true);
}
}
You're just instantiating a Thread without giving it a Runnable, so it's not running what you're intending.
Thread t = new Thread();
should be
Thread t = new Thread(this);
this because the Circle is Runnable and the run() method is in it.
Regarding color changing, I would give you an idea.
When drawing the circles, check the current circle number/index against another variable which changes every second colorChangeIndex
if(circleIndex == colorChangeIndex){
// another color
else
//the default color
colorChangeIndex++; (modulo number of cricles to reset it)
The basic structure of your code is wrong. Painting should only be done in the paintComponent() method. You should never reference the Graphics object in your thread.
So, instead what you might want to do is keep an ArrayList of "circle" objects. This custom object will contain the circle to be painted and its color. Then in the paintComponent() method you just iterate through the ArrayList and paint all the circles in the ArrayList. Check out the DrawOnComponent example found in Custom Painting Approches.
After a second I want the first oval to change colours and then after another second i want the first oval to go back to its original colour and then change the second ovals colour.
So now you need to use a Swing Timer to animate the painting. When the Timer fires you reset the Color of the appropriate circle and then just repaint the panel.
Related
I have a project in which I have several components, . The problem is that when the repaint () command is executed, the JLabels are not painted. I want to make it clear that it is NOT a problem that there are graphics that are being painted on or anything. Apparently what happens is that within repaint (), when I force the JLabel to repaint, they do not paint unless you change the JLabel chain.
Initialization of the JLabel and define its parameters:
puntuacionL.setFont(new Font("Marker Felt", Font.PLAIN, 20));
puntuacionL.setBounds(710, 212, 150, 30);
puntuacionL.setOpaque(true);
puntuacionL.setBackground(Color.CYAN);
puntuacionL.setForeground(Color.white);
puntuacionL.setVisible(true);
comoJugar.setFont(new Font("Marker Felt", Font.PLAIN, 20));
comoJugar.setBounds(710, 245, 150, 30);
comoJugar.setOpaque(true);
comoJugar.setBackground(Color.CYAN);
comoJugar.setForeground(Color.white);
comoJugar.setVisible(true);
I have tried two options to force the redefinition of JLabel:
1. mietiqueta.setText("whatever").
In this option what I do is to redefine in the repaint () label string.
2.
Repaint the component: mietiqueta.paintComponent(g)
I would have uploaded a capture of the result, which would have been very useful, but you need at least 10 reputation points
Repaint() method:
#Override
public void update(Graphics g){
paint(g);
}
#Override
public void paint(Graphics g) {
if (offGraphics == null) {
offImage = createImage(900,900);
offGraphics = (Graphics2D) offImage.getGraphics();
}
puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
comoJugar.setText(" Pulsa H para ayuda");
Graphics2D g2d = (Graphics2D) g;
puntoIncialBala();
Image fondo = new ImageIcon(getClass().getResource("/imagenes/fondo.jpeg")).getImage();
Image img2 = new ImageIcon(getClass().getResource("/imagenes/fondoMapa.png")).getImage();
offGraphics.drawImage(img2, 30, 30, this);
int posVidaX = 710;
offGraphics.setColor(Color.CYAN);
offGraphics.fillRoundRect(710, 30, 150, 100, 10, 10);
for (int k = 0; k < vidas; k++) {
Image vida = new ImageIcon(getClass().getResource("/imagenes/vida.png")).getImage();
offGraphics.drawImage(vida, posVidaX, 50, this);
posVidaX = posVidaX + vida.getWidth(this);
}
timeLabel.paintComponents(g);
for (int k = 0; k < arrayCasilla.size(); k++) {
offGraphics.drawImage(arrayCasilla.get(k).getImg(), (int) arrayCasilla.get(k).getY(), (int) arrayCasilla.get(k).getX(), this);
}
Image img = new ImageIcon(getClass().getResource("/imagenes/BalaCanon.png")).getImage();
if (flagBala == true) {
//puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
if (pintarCasilla == true) {
puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
comoJugar.setText(" Pulsa H para ayuda");
queHayCasilla();
if (casilla[x][y].getTipo().compareTo("cangrejo") == 0) {
puntuacion = puntuacion + 1;
} else if (casilla[x][y].getTipo().compareTo("ron") == 0) {
posVidaX = 710;
//puntuacion = puntuacion + 1;
vidas--;
if (vidas == 0) {
timer.stop();
etiquetaFin.setVisible(true);
finalizar.setVisible(true);
contenedorFinal.setVisible(true);
}
offGraphics.setColor(Color.CYAN);
offGraphics.fillRoundRect(710, 30, 150, 100, 10, 10);
for (int k = 0; k < vidas; k++) {
Image vida = new ImageIcon(getClass().getResource("/imagenes/vida.png")).getImage();
offGraphics.drawImage(vida, posVidaX, 50, this);
posVidaX = posVidaX + vida.getWidth(this);
}
} else if (casilla[x][y].getTipo().compareTo("cofre") == 0) {
puntuacion = puntuacion + 2;
}
if (posY[y] < 441) {
arrayCasilla.add(casilla[x][y]);
}
for (int k = 0; k < arrayCasilla.size(); k++) {
if (posY[y] < 441) {
offGraphics.drawImage(arrayCasilla.get(k).getImg(), (int) arrayCasilla.get(k).getY(), (int) arrayCasilla.get(k).getX(), this);
}
}
animacion.stop();
if (!animacion.isRunning()) {
i = 0;
}
pintarCasilla = false;
flagBala = false;
} else {
offGraphics.drawImage(img, (int) x1, (int) x2, this);
}
}
try {
imgB = ImageIO.read(getClass().getResource("/imagenes/canon.png"));
} catch (IOException ex) {
Logger.getLogger(PanelCanon.class.getName()).log(Level.SEVERE, null, ex);
}
offGraphics.setColor(Color.gray);
offGraphics.fillRect(30, 720, 646, 122);
AffineTransform tx = AffineTransform.getRotateInstance(Math.PI / 2 - anguloRotacion, imgB.getWidth(this) / 2, imgB.getHeight(this) / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
tx.rotate(Math.PI / 2 - anguloRotacion);
System.out.println(tx.toString());
offGraphics.drawImage(op.filter(imgB, null), 360, 740, null);
g2d.drawImage(offImage, 0, 0, this);
puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
comoJugar.setText(" Pulsa H para ayuda");
System.out.println("Posicion bala x" + x1);
System.out.println("Posicion bala y: " + x2);
}
I have a timer that may be interfering in some way, this timer is responsible for controlling the countdown.
Timer code:
timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (seconds == 0 && minutes == 0) {
timer.stop();
etiquetaFin.setVisible(true);
finalizar.setVisible(true);
contenedorFinal.setVisible(true);
} else if (seconds > 0) {
seconds--;
} else if (minutes > 0) {
minutes--;
seconds = 59;
}
revalidate();
//puntuacionL.paintImmediately(710, 212, 150, 30);
//puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
//comoJugar.setText(" Pulsa H para ayuda");
timeLabel.setText(" "+timeFormatter.format(minutes) + ":" + timeFormatter.format(seconds));
}
});
timer.start();
And this is where the magic happens and I do not understand anything anymore. As you can see inside the event I have commented the lines:
puntuacionL.setText(" Puntuación: "+Integer.toString(puntuacion));
comoJugar.setText(" Pulsa H para ayuda");
If within the event of the Timer I redefine the Labels, with a different string from the paint () they are painted, but of course it does not work for me, since I want the string not to be modified.
I have not put the whole class because it is quite large, but if someone still needs the complete context, happy to edit the question and put all the code.
I am really not sure what you are trying to do, but you appear not to understand the basic concepts of using components and of custom painting.
Here are the problems I see:
Don't override update(...). There is no reason to do this.
Don't override paint(). Custom painting is done by overriding the paintComponent(...) method of the component.
Don't read images in the painting method. A component is repainted when you specifically invoke repaint() on the component or when Swing determines the components needs to be painted. Images should be read in the constructor of your class to make the painting code as efficient as possible.
Don't change the properties of your class in the painting method. This would especially apply to the changing the text of an external component like a JLabel.
Don't invoke paintComponents() on any component from within the painting method.
Don't access the Timer in the painting method. Again, the whole purpose of the painting code is to paint the current state of the component, not alter the state. The purpose of the Timer is the change the state of the component every time it fires. So it is the Timer logic that should be changing the text on the JLabel. The JLabel will then automatically repaint itself. See: Program freezes during Thread.sleep() and with Timer for a basic example of undateing a JLabel from withing a Timer.
Read the section from the Swing tutorial on Custom Painting for more information on the basic of painting.
I have a graphic which consists of a horizontal line of circles of different sizes at regular intervals. Here is the picture:
I am trying to recreate this graphic using recursion as opposed to the if statements used in my code but after trying, am unsure about how to do this. Would love some help, here is my code:
package weekFour;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;
#SuppressWarnings("serial")
public class Circle extends JPanel {
private int circX = 10;
private static int windowW = 1700;
private static int windowH = 1000;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes out edges
Color c5 = new Color(50, 50, 50);
Color c4 = new Color(100, 100, 100);
Color c3 = new Color(150, 150, 150);
Color c2 = new Color(200, 200, 200);
Color c1= new Color(250, 250, 250);
for (int i = 0; i < 1; i++) {
g2.setColor(c1);
g2.fillOval(522 + 75*i, 138, 666, 666);
g2.setColor(c1);
g2.drawOval(522 + 75*i, 138, 666, 666);
}
for (int i = 0; i < 3; i++) {
g2.setColor(c2);
g2.fillOval(244 + 522*i, 365, 180, 180);
g2.setColor(c2);
g2.drawOval(244 + 522*i, 365, 180, 180);
}
for (int i = 0; i < 10; i++) {
g2.setColor(c3);
g2.fillOval(130 + 174*i, 428, 60, 60);
g2.setColor(c3);
g2.drawOval(130 + 174*i, 428, 60, 60);
}
for (int i = 0; i < 25; i++) {
g2.setColor(c4);
g2.fillOval(60 + 87*i, 444, 25, 25);
g2.setColor(c4);
g2.drawOval(60 + 87*i, 444, 25, 25);
}
for (int i = 0; i < 120; i++) {
g2.setColor(c5);
g2.fillOval(circX + 29*i, 450, 12, 12);
g2.setColor(c5);
g2.drawOval(circX + 29*i, 450, 12, 12);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("MyTaskToo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Circle());
frame.setSize(windowW, windowH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Thanks for your time.
This is how I went about this problem, although we had to do it with green circles as opposed to grey circles but it's not that different.
N.B: Sorry for the appealing comments for sometimes trivial things but we get marks for commenting and it is better to be safe than sorry. Maybe they change you some insight into the thought process.
Here is the main method that starts the programme and sets out the window information.
public class Q2Main {
public static void main(String[] args) {
// here we are just setting out the window end putting the circles drawin in Q2Circles into this window.
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(1000, 500);
window.getContentPane().add(new Q2Circles(5));
window.setVisible(true);
}}
This is where the magic happens:
public class Q2Circles extends JPanel {
// this allows the user to specify how many loops of recursion they want the programme to complete before finishing
int recursionsToDo;
public Q2Circles(int recursionMax){
super();
recursionsToDo = recursionMax;
}
/*
this method is automatically called when we run the constructor as it inherits from the JFram superclass. here
we are setting out the size of the circle by getting the size of the window to make it proportional to the rest
of the screen and circles.
we then pass these values into the drawCircle method to draw the circle
*/
public void paintComponent(Graphics g){
Rectangle rectangle = this.getBounds();
int diameter = rectangle.width/3;
int centerPoint = rectangle.width/2;
drawCircle(g, 1, centerPoint, diameter);
}
/*
This method is where the magic of the programme really takes place. first of all we make sure we haven't completed
the necessary recursions. we the set the color by dividing it by the amount of times we have recursed, this will
have the affect of getting darker the more times the method recurses. we then sset the color. finaly we fill the
oval (draw the circle). because we want to move depending on the times it has recursed and size of the previous
we do it based on the size of the elements from the previous call to this method. Getting the right numbers
though was just alot of trial and error.
we then increment the recursion counter so that we know how many times we have recursed and that can then be
used at different points where needed. e.g for setting the color.
each recursive call used the dimension of the other recursive calls to make the whole picture. Although the
first recursive call creates the circles on the right of the screen. the second call draws the circle on the
left of the screen and the last one does the circles in the middle, they all use eachothers values to make it
complete. without one recursive step, more is missing than just what is created by that recursive call on its own.
in all honesty though, there is alot of duplication, like the large middlecircle.
*/
public void drawCircle(Graphics g, int amountOfRecursions, int center, int diameter){
if (amountOfRecursions <= recursionsToDo){
int recursionsCount = amountOfRecursions;
int greenColor = Math.round(225 / (amountOfRecursions));
g.setColor(new Color(0, greenColor, 0));
g.fillOval(center - (diameter/2), 200 - (diameter/2), diameter, diameter);
recursionsCount++;
drawCircle(g, recursionsCount, Math.round(center + diameter), diameter/3);
drawCircle(g, recursionsCount, Math.round(center - diameter), diameter/3);
drawCircle(g, recursionsCount, Math.round(center), diameter/3);
}
}}
Id like multicolored changing text, I made a list with all the colors
I have 5 g.drawString(); functions running, each of them should be the next color in the list (one above each other.)
private Color[] colors = new Color[12];
Then in my constructor:
colors[0] = new Color(255, 0, 0);
colors[1] = new Color(255, 127, 0);
colors[2] = new Color(255, 255, 0);
colors[3] = new Color(127, 255, 0);
colors[4] = new Color(0, 255, 0);
colors[5] = new Color(0, 255, 127);
colors[6] = new Color(0, 255, 255);
colors[7] = new Color(0, 127, 255);
colors[8] = new Color(0, 0, 255);
colors[9] = new Color(127, 0, 255);
colors[10] = new Color(255, 0, 255);
colors[11] = new Color(255, 0, 127);
How would I make each each letter a different color?
Set The Color: g.setColor(Color object);
Example: g.setColor(colors[5]);
Write Text: g.drawString(String, x, y);
Example: g.drawString("S", 200, 300);
So, Id like S to be the color, colors[0], I made a table below:
Starting | First | Second | Fifth
S -- 0 11 10 7
N -- 1 0 11 8
A -- 2 1 0 9
K -- 3 2 1 10
E -- 4 3 2 11
So it would loop around though each color:
I tried making a function for this, I deleted the code because I'm an idiot -_-
In my main class, I have a game loop that calls the tick and render methods, tick first then render.
I have an enum called STATE which contains menu and game, then the variable gameState of the type state is set to STATE.menu
public enum state {
Menu,
Game,
}
public state gameState = state.Menu;
When gameState is equal to STATE.menu it will call menu.render(g ( <-- The variable im using for Graphics));
Each class has its own render and tick method.
-Tick method, for setting variables etc, if statements, yada yada yada
-Render method, anything to do with drawing pixels
Because the tick method is called every 0.0000000000000000001 seconds, the color changes every 9 millionth of a second and it looks very derpy.
So ill need a timer of some sorts that I can configure with a variable
I want each of the letters to be a different color, one after another in the list
you can refer to the table above if you don't understand but as an example,
the letter a should be colors[0]
and then b, colors[1]
and c, colors[2]
the colors should alter,
so a would be colors[2]
so b would be colors[0]
so c would be colors[1]
I've probably been unclear on 1001 things, so please shout at me in the comments ^-^
Thanks for reading!
If I understood correctly, there are mainly two issues:
Painting the letters in different colors, cycling through the given array
Updating the colors (but at a fixed time, not with every "tick")
Cycling through the colors can be achieved by introducing a "colorOffset". You can add this color offset to the index that you use to access the color in the array, and take this modulo the array length to obtain a valid array index:
int colorOffset = ... // Counted up or down all the time
// The index of the color for the i'th letter
int colorIndex = (i+colorOffset)%colors.length;
if (colorIndex < 0) colorIndex += colors.length;
g.setColor(colors[colorIndex]);
The second part, regarding the update: I assume that you have a game loop that is run in an own thread. Then, in thisTickMethodThatYouHaveBeenTalkingAbout, you can check the current system time with System.nanoTime(), and compute the time that has passed since the last update. If the time is larger than the desired interval, you perform an update, by increasing the colorOffset and triggering a repaint() (if necessary - you might cover this already with your render() method).
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MulticolorTextAnimation
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MulticolorTextAnimationPanel m = new MulticolorTextAnimationPanel();
f.getContentPane().add(m);
Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
while (true)
{
m.thisTickMethodThatYouHaveBeenTalkingAbout();
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
return;
}
}
}
});
thread.setDaemon(true);
thread.start();
f.setSize(500,200);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class MulticolorTextAnimationPanel extends JPanel
{
private String string;
private Color colors[];
private int colorOffset = 0;
private long lastUpdateNs = -1;
private final long updateIntervalMs = 250;
public MulticolorTextAnimationPanel()
{
setFont(new Font("Dialog", Font.BOLD, 45));
string = "I am a string!";
colors = new Color[12];
colors[0] = new Color(255, 0, 0);
colors[1] = new Color(255, 127, 0);
colors[2] = new Color(255, 255, 0);
colors[3] = new Color(127, 255, 0);
colors[4] = new Color(0, 255, 0);
colors[5] = new Color(0, 255, 127);
colors[6] = new Color(0, 255, 255);
colors[7] = new Color(0, 127, 255);
colors[8] = new Color(0, 0, 255);
colors[9] = new Color(127, 0, 255);
colors[10] = new Color(255, 0, 255);
colors[11] = new Color(255, 0, 127);
}
public void thisTickMethodThatYouHaveBeenTalkingAbout()
{
long ns = System.nanoTime();
if (lastUpdateNs < 0)
{
lastUpdateNs = ns;
}
long passedNs = (ns - lastUpdateNs);
long passedMs = passedNs / 1000000;
if (passedMs > updateIntervalMs)
{
// Increase or decrease the color offset,
// depending on whether the colors should
// cycle forward or backward
colorOffset--;
repaint();
lastUpdateNs = ns;
}
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
FontMetrics fontMetrics = g.getFontMetrics();
int x = 100;
int y = 100;
for (int i=0; i<string.length(); i++)
{
char c = string.charAt(i);
int colorIndex = (i+colorOffset)%colors.length;
if (colorIndex < 0)
{
colorIndex += colors.length;
}
g.setColor(colors[colorIndex]);
g.drawString(String.valueOf(c), x, y);
x += fontMetrics.charWidth(c);
}
}
}
My goal with this program is to have the bullseye colors switch back and forth. However the colors do not switch it makes new colors instead. The more pressing problem is when i try to repeat it in any way. The screen that comes up when the program is ran is blank and does nothing. When there is no loop the bullseye comes up.
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class BullSEye extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Random rand = new Random();
int top = 2;
int r = rand.nextInt(256);
int b = rand.nextInt(256);
int h = rand.nextInt(256);
int t = rand.nextInt(256);
int u = rand.nextInt(256);
int v = rand.nextInt(256);
Color randomColor = new Color(r, h, b);
Color randColor = new Color(t,u,v);
//sets colors for first bullseye
g.setColor(randomColor);
g.fillOval( 10, 10, 200, 200 );
g.setColor(randColor);
g.fillOval( 35, 35, 150, 150 );
g.setColor(randomColor);
g.fillOval(60, 60, 100, 100);
g.setColor(randColor);
g.fillOval( 85, 85, 50, 50 );
try
{
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
//sets colors for second bullseye
g.setColor(randColor);
g.fillOval( 10, 10, 200, 200 );
g.setColor(randomColor);
g.fillOval( 35, 35, 150, 150 );
g.setColor(randColor);
g.fillOval(60, 60, 100, 100);
g.setColor(randomColor);
g.fillOval( 85, 85, 50, 50 );
//recursive call to repeat the back and forth colors
paintComponent(g);
}
}
import javax.swing.JFrame;
public class BullSEyeTest
{
public static void main( String args[] )
{
BullSEye panel = new BullSEye();
JFrame application = new JFrame();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel );
application.setSize( 230, 250 );
application.setVisible( true );
}
}
There are a couple issues here that are probably contributing to your problem.
First, you are generating random numbers for your colors, and that is why you get different colors every time you run the program (without the recursive call).
If you want the same colors every time, you don't need the random number generator, they would just be constants.
Secondly, recursion is not the right way to re-render the UI in Swing. Swing provides a 'repaint' method for that on the JComponent, and typically you would call that repaint method from an action listener that is fired from a timer instead of doing that recursively. Also, you are getting a non-responsive UI because you are telling the Thread to sleep.
Hope that helps answer your question. Check out this post for some more information on how to implement this:
Java - repaint component every second?
I have an application that extends a Frame. Then, it'll display a few lines of text using:
Font f = new Font("Arial", Font.PLAIN, 10);
g.setFont(f);
g.drawString("Test|great Yes ^.", x, y + 10);
Now what happens is that the text doesn't fit in the box around. E.g. I'm expecting the text to fit in [x,y]-[x+width, y+10] (don't care about the width) but it falls somewhat below the y+10 line. Now for most characters ('T', 'e', etc.) this fits but '|' and 'g' don't! They go below the y+10-line. It seems you can't use: draw at y + characterHeight. But what does work?
To see what I mean, here's some sample code:
import java.awt.*;
public class test extends Frame
{
public test()
{
/* retrieve max window size */
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
GraphicsConfiguration [] gc = gs[0].getConfigurations();
Rectangle r = gc[0].getBounds();
setSize(r.width, r.height);
setVisible(true);
}
public void paint(Graphics g)
{
final int windowWidth = getSize().width;
final int windowHeight = getSize().height;
g.setColor(Color.BLUE);
g.fillRect(0, 0, windowWidth, windowHeight);
g.setColor(Color.WHITE);
g.fillRect(0, 100, windowWidth, 110);
int textHeight = 100;
Font f = new Font("Arial", Font.PLAIN, textHeight);
g.setFont(f);
g.setColor(Color.BLACK);
g.drawString("Test|great Yes ^.", 10, 100 + textHeight);
}
public void guiLoop()
{
for(;;) { try { Thread.sleep(1000); } catch(Exception e) { } }
}
public static void main(String [] args)
{
new test().guiLoop();
}
}
I tried the following code as well:
public void paint(Graphics g)
{
final int windowWidth = getSize().width;
final int windowHeight = getSize().height;
g.setColor(Color.BLUE);
g.fillRect(0, 0, windowWidth, windowHeight);
g.setColor(Color.WHITE);
g.fillRect(0, 100, windowWidth, 110);
int textHeight = 100;
String str = "Test|great Yes ^.";
Font f = new Font("Arial", Font.PLAIN, textHeight);
Rectangle2D boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false));
f = f.deriveFont((float)(textHeight * (textHeight / boundingRectangle.getHeight())));
boundingRectangle = f.getStringBounds(str, 0, str.length(), new FontRenderContext(null, false, false));
g.drawString(str, 10, 100 + (int)boundingRectangle.getHeight());
g.setFont(f);
g.setColor(Color.BLACK);
g.drawString(str, 10, 100 + textHeight);
}
This is somewhat better: the text is smaller so it might fit, but there's still the problem that the y-position is incorrect.
All help is appreciated!
What about using FontMetrics? You can obtain it from Graphics object with g.getFontMetrics().
Than you can retrieve max descent or ascent or directly height (using getHeight), so your implementation will be font-indipendent and it should work fine.. check documentation here!
EDIT (to explain comments):
there is no a direct way to tell to a string to draw itself in a manner that can fit a box. You have to do it by yourself.. like start from a max font size and check if width fits the box, otherwise decrement size and try again. For height you should FIRST decide (or obtain) max font height, then you can set how many pixel should the box be.
I think I solved it somewhat:
boundingBoxHeight: height of box in which the text should fit
yOffset where to start drawing the font
Font f = new Font("Arial", Font.PLAIN, boundingBoxHeight);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
double shrink = ((double)textHeight / (double)fm.getHeight());
double newSize = (double)textHeight * shrink;
double newAsc = (double)fm.getAscent() * shrink;
int yOffset = (int)newAsc - fm.getLeading();
f = f.deriveFont((float)newSize);
g.setFont(f);
g.drawString(str, 10, 100 + yOffset);
There's quite a bit of whitespace above the text though.