Why is this error showing up? — Processing - java

Could anyone share with me why I am getting this error? Basically it's a program where I want to simulate basic basic plant growth. I want to do it in such a way that the petals are all stored in an array of circles.
Stem myStem;
Circle circles;
float scaleFactor=0.5;
void setup() {
size(floor(400*scaleFactor), floor(800*scaleFactor));
myStem = new Stem(200,800);
}
void draw() {
background(150);
smooth();
Circle circles[];
circles = new Circle[5];
circles[0] = new Circle(0, -40, 50, 50);
circles[1] = new Circle(0, -40, 50, 50);
circles[2] = new Circle(0, -40, 50, 50);
circles[3] = new Circle(0, -40, 50, 50);
circles[4] = new Circle(0, -40, 50, 50);
for (int i = 0; i < circles.length; i++) {
circles = ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
rotate(radians(72));
circles[i] = Circle;
}
myStem.drawStem();
}
class Stem {
int initalloX=200;
int initalloY=800;
Stem(int tempInitalloX, int tempInitalloY) {
initalloX = tempInitalloX;
initalloY = tempInitalloY;
}
void drawStem() {
background(#0DBADB);
scale(scaleFactor, scaleFactor);
stroke (12, 149, 11);
fill (12, 149, 11);
strokeWeight(10);
line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
//stem1
if (frameCount>101) {
noStroke();
translate(initalloX, initalloY-200);
scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
beginShape();
vertex(0, 0);
bezierVertex(-40, -5, -30, -40, -80, -20);
bezierVertex(-47, -16, -52, 8, 0, 0);
endShape(CLOSE);
scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
translate(-initalloX, -(initalloY-200));
}
//stem2
if (frameCount>151) {
noStroke();
translate(initalloX, initalloY-300);
scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
beginShape();
vertex(0, 0);
bezierVertex(-40, -5, -30, -40, -80, -20);
bezierVertex(-47, -16, -52, 8, 0, 0);
endShape(CLOSE);
scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
translate(-initalloX, -(initalloY-300));
}
}
}
class Circle {
int c1 = 0;
int c2 = -40;
int c3 = 50;
int c4 = 50;
Circle(int tc1, int tc2, int tc3, int tc4) {
c1 = tc1;
c2 = tc2;
c3 = tc3;
c4 = tc4;
}
}
Thanks in advance... All help is much appreciated.

Besides all thing already pointed, note that ellipse() is a void method, and so, it won't return anything. Thus a line like
circle = ellipse(x,y,z,z)
has no meaning. You probably wan to use the values stored in ciclcle[i] to draw ellipses, so just call
ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
no need for assigning it. Also i don't see why create 5 equal circles. If your circle object is just storing data, why store the same data five times? The call:
for (int i = 0; i < circles.length; i++) {
ellipse(0, -40, 50, 50);
rotate(radians(72));
}
Will have the same effect.
Besides that calling background() at the end of draw (trough myStem.drawStem()) will hide all things previously drawn.
And yet there is no need to recreate the array and reassign the values 60 times per second, you can move it to setup.
I made those changes to your code. It will compile now. Still the "petals" is beeing drawn at origin, and the fill/stroke of them needs to be handled, but at least it is running :)
You may want to make a display method in your circle class... More like i pointed in the other post you made. cheers!
Stem myStem;
//Circle circles; // double declaration
Circle circles[]; // keeping the array one only
float scaleFactor=0.5;
void setup() {
size(floor(400*scaleFactor), floor(800*scaleFactor));
myStem = new Stem(200,800);
//mpoved this to setup, no need to recreate each frame
circles = new Circle[5];
circles[0] = new Circle(0, -40, 50, 50);
circles[1] = new Circle(0, -40, 50, 50);
circles[2] = new Circle(0, -40, 50, 50);
circles[3] = new Circle(0, -40, 50, 50);
circles[4] = new Circle(0, -40, 50, 50);
// also smooth only needs to be called once
// unless ther is a noSmooth() somewhere
smooth();
}
void draw() {
// moved this here
background(#0DBADB);
for (int i = 0; i < circles.length; i++) {
ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
// note you may use this instead
//ellipse(0, -40, 50, 50);
rotate(radians(72));
}
myStem.drawStem();
}
class Stem {
int initalloX=200;
int initalloY=800;
Stem(int tempInitalloX, int tempInitalloY) {
initalloX = tempInitalloX;
initalloY = tempInitalloY;
}
void drawStem() {
//background(#0DBADB); // this was hiding all other draws
scale(scaleFactor, scaleFactor);
stroke (12, 149, 11);
fill (12, 149, 11);
strokeWeight(10);
line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
//stem1
if (frameCount>101) {
noStroke();
translate(initalloX, initalloY-200);
scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
beginShape();
vertex(0, 0);
bezierVertex(-40, -5, -30, -40, -80, -20);
bezierVertex(-47, -16, -52, 8, 0, 0);
endShape(CLOSE);
scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
translate(-initalloX, -(initalloY-200));
}
//stem2
if (frameCount>151) {
noStroke();
translate(initalloX, initalloY-300);
scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
beginShape();
vertex(0, 0);
bezierVertex(-40, -5, -30, -40, -80, -20);
bezierVertex(-47, -16, -52, 8, 0, 0);
endShape(CLOSE);
scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
translate(-initalloX, -(initalloY-300));
}
}
}
class Circle {
int c1 = 0;
int c2 = -40;
int c3 = 50;
int c4 = 50;
Circle(int tc1, int tc2, int tc3, int tc4) {
c1 = tc1;
c2 = tc2;
c3 = tc3;
c4 = tc4;
}
}

Learned something new I guess for declaring an array.
As for what is going wrong, it looks like you're using a Circle variable called "circle" and confusing it with an array of Circles by also calling it circle which probably is leading to all sorts of problems. That's probably what you should focus on fixing.

Guessing...
There are two definitions of circles in the class
Circle circles
Circle[] circles

I think this circles[i] = Circle; is the error. You cannot asign a Type (the class Circle) to a variable (i.e. an Object or an instance of a class)

Related

Variables not functioning properly when called in different functions

I am making a code of a cute animated pig using java:
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public static void main(String[] args) {
File Oink = new File("C:/.../oink.wav");
//sound definition -- you can specify your file location in '...'
//if you would like to test my code :)
}
static void PlaySound(File Sound) {
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(Sound));
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}catch (Exception e) {
System.out.println(e);
}
}
int pigX ;
int pigY ;
int count ;
int snoutPos ;
int eyeCol ;
int legY ;
int earS ;
void setup () {
;
System.out.println("Pig assemled!");
System.out.println("Pig free to roam!");
System.out.println(Math.random());
size (900, 900);
pigX = width/2;
pigY = height/2;
count = -1;
count = -1;
}
void draw () {
if (keyPressed) {
if (key == CODED) {
if (keyCode==RIGHT) {
pigX += 10;
}
if (keyCode==LEFT) {
pigX -= 10;
}
if (keyCode==UP) {
pigY -= 10;
}
if (keyCode==DOWN) {
pigY += 10;
}
if (key==' ') {
PlaySound(Oink); // Oink is apparently not defined here
}
}
}
if (pigX < -180) {
pigX = 1080;
}
if (pigX > 1080) {
pigX = -180;
}
if (pigY < -180) {
pigY = 1080;
}
if (pigY > 1080) {
pigY = -170;
}
background (50, 130, 50);
count += 1;
if ((count % 36) == 0) {
snoutPos += 1;
}
if (0 < (count % 90) && (count % 90) < 10) {
eyeCol = 0;
} else {
eyeCol = 255;
}
if (0 < (count % 40) && (count % 40) < 20) {
legY = 20;
} else {
legY = 0;
}
if (count % 60 < 30) {
earS = 10;
} else {
earS = 7;
}
//shadow
fill (45, 95, 45);
stroke (45, 95, 45);
rect (pigX, pigY+160, 200, 100);
rect (pigX+109, pigY+160, 20, 80);
rect (pigX-109, pigY+160, 20, 80);
//body
fill (239, 154, 154);
stroke (0, 0, 0);
rectMode (CENTER);
rect (pigX, pigY, 180, 180);
//eyes
fill (0, 0, 0);
rect (pigX-70, pigY, 40, 20);
rect (pigX+70, pigY, 40, 20);
stroke (eyeCol, eyeCol, eyeCol);
fill (eyeCol, eyeCol, eyeCol);
rect (pigX-60, pigY, 20, 20);
rect (pigX+60, pigY, 20, 20);
//snout
fill (255, 205, 210);
stroke (255, 205, 210);
rect (pigX, pigY+40-(snoutPos % 2)*9, 80, 50);
fill (229, 115, 115);
stroke (229, 115, 115);
rect (pigX-30, pigY+40-(snoutPos % 2)*9, 20, 20);
rect (pigX+30, pigY+40-(snoutPos % 2)*9, 20, 20);
//hair i guess
rect (pigX-40, pigY-69, 20, 40);
//legs
fill (190, 100, 100);
stroke (100, 65, 65);
rect (pigX-45, pigY+120+legY/2, 60, 60+legY);
fill (190, 100, 100);
stroke (100, 65, 65);
rect (pigX+45, pigY+130-legY/2, 60, 80-legY);
//hooves
fill (140, 50, 50);
stroke (140, 50, 50);
rect (pigX-65, pigY+140+legY, 20, 20);
rect (pigX-25, pigY+140+legY, 20, 20);
rect (pigX+65, pigY+160-legY, 20, 20);
rect (pigX+25, pigY+160-legY, 20, 20);
//ears
fill (219, 134, 134);
stroke (219, 134, 134);
rect (pigX-85, pigY-95, 50, 15);
rect (pigX-100, pigY-70, 20, 40);
rect (pigX+85, pigY-95, 50, 15);
rect (pigX+100, pigY-70, 20, 40);
}
However, the program says that 'Oink' is not defined!
I thought maybe I should do something like making Oink global, but I couldn't just stick global File in main() !
I've browsed on the internet for solutions, but none of the results really answered my question!
I guess I could specify the filename in draw (), but then it would be running at 60 times per second and if I want a faster program in the future (probably with a longer, harder project), I feel it will be necessary to cut down on that.
How shall I fix this?
NOTE: even when I put File Oink = new File("C:/.../oink.wav"); in draw(), the sound still doesn't work!

Trying to draw a vertical linear gradient using a map of colors, but only the first 9 colors are used: why?

(nota: a Minimal, Complete, and Verifiable example is provided at the end of this question)
Summary
Context, Aim & Problem
What I have already tried
Pertinent sources explained
Expected results, Actual results & Question
Minimal, Complete, and Verifiable example
Context, Aim & Problem
I am trying to animate some pixels to generate a fire animation in Java. Each pixel is colored so that a vertical linear gradient, from white to yellow, yellow to red, and red to black, is drawn. This gradient goes from bottom to top of the canvas.
At the beginning of the execution, all pixels are black, except the white line which is defined by the coordinate y = height - 1, height being the height of the canvas. This white line is used to init the gradient ("white to yellow, yellow to ..., etc.").
The problem is that the gradient begins correctly, but it stops when the 9th color is used. Then only this color is used to fill my gradient and I don't know why.
What I have already tried
I have a map of RGB values which defines a gradient.
The idea to know which color to apply to a pixel called "A" is to retrieve the RGB of the pixel just below it, then get the ID of this RGB among all the RGB of my map. Then, I get the RGB under this ID + 1 in this same map and apply it to the pixel A.
So:
I checked the function that returns the ID of the RGB, given this RGB: it seems to be OK since I don't have thrown any exception
I checked if the buffered image I use is correctly updated. In other words: if the fact that a pixel has been colored really has the consequence to determine the color of the pixel above : it's OK too
Pertinent sources explained
Calling methods to draw the gradient
The idea is to set all pixels in black, except the bottom line which is white. Then, I iterate on each canvas' pixel and give it the color of its below direct vertical neighbor. More precisely, I give it the color whose ID = the ID of this neighbor pixel's color + 1, within my map of colors.
Colors colors = new FireColors(new ArrayList<>());
gui.colorize(colors.getColorAtIndex(34), -1, -1); // Setting black anywhere
gui.colorize(colors.getColorAtIndex(0), -1, height - 1); // Setting white, in a lower line
try {
for(int y = height - 2; y >= 0; y--) {
for(int x = 0; x < width; x++) {
int below_pixel_rgb = gui.getRGBAtCoordinates(x, y + 1);
int index_of_found_color = colors.getIndexOfColor(below_pixel_rgb);
int index_of_color_to_apply = (index_of_found_color + 1) % colors.getSize();
gui.colorize(colors.getColorAtIndex(index_of_color_to_apply), x, y);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
How I color pixels
I'm just iterating over the canvas.
void colorize(Color color, int x_parameter, int y_parameter) {
for(int y = (y_parameter == -1 ? 0 : y_parameter); y <= (y_parameter == -1 ? this.getHeight() - 1 : y_parameter); y++) {
for(int x = (x_parameter == -1 ? 0 : x_parameter); x <= (x_parameter== -1 ? this.getWidth() - 1 : x_parameter); x++) {
buffered_image.setRGB(x, y, color.getRGB());
}
}
panel.repaint();
}
How do I find the index of the color of a pixel, within the list of colors?
int getIndexOfColor(int rgb) throws Exception {
for (int x = 0; x < colors.size(); x++) {
if(colors.get(x).getRGB() == rgb) {
return x;
}
}
throw new Exception("Color not found in the list!");
}
Expected results, Actual results & Question
I expect to have several vertical gradients (each from bottom to top). "Several" because my canvas' height is greater than the number of the colors of my gradients and because I use a modulo to choose the color to apply.
The actual results are: I get a gradient that begins from white to yellow, there are only 9 colors and that's all. No orange, no red, no black. Indeed: https://imgur.com/oQFJ52k
My question is: since the good ID is retrieved, and the good neighbor chosen for a given pixel, why is my gradient blocked to the 9th color? In other words: why, from a precise moment, aren't the good colors chosen?
Minimal, Complete, and Verifiable example
Launcher.java
import java.util.ArrayList;
public class Launcher {
public static void main(String args[]) {
int width = 150, height = 150;
Gui gui = new Gui(width, height);
gui.setUp("DOOM-like fire");
gui.setVisible(true);
Colors colors = new FireColors(new ArrayList<>());
gui.colorize(colors.getColorAtIndex(34), -1, -1); // Setting black anywhere
gui.colorize(colors.getColorAtIndex(0), -1, height - 1); // Setting white, in a lower line
try {
for(int y = height - 2; y >= 0; y--) {
for(int x = 0; x < width; x++) {
int below_pixel_rgb = gui.getRGBAtCoordinates(x, y + 1);
int index_of_found_color = colors.getIndexOfColor(below_pixel_rgb);
int index_of_color_to_apply = (index_of_found_color + 1) % colors.getSize();
gui.colorize(colors.getColorAtIndex(index_of_color_to_apply), x, y);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Gui.java
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
class Gui extends JFrame {
private JPanel panel;
private BufferedImage buffered_image;
Gui(int width, int height) {
buffered_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
panel = new JPanel() {
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
graphics.drawImage(buffered_image, 0, 0, null);
}
};
}
void setUp(String title) {
setTitle(title);
setLayout(null);
setSize(buffered_image.getWidth(), buffered_image.getHeight());
setContentPane(panel);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
void colorize(Color color, int x_parameter, int y_parameter) {
for(int y = (y_parameter == -1 ? 0 : y_parameter); y <= (y_parameter == -1 ? this.getHeight() - 1 : y_parameter); y++) {
for(int x = (x_parameter == -1 ? 0 : x_parameter); x <= (x_parameter== -1 ? this.getWidth() - 1 : x_parameter); x++) {
buffered_image.setRGB(x, y, color.getRGB());
}
}
panel.repaint();
}
int getRGBAtCoordinates(int x, int y) {
return buffered_image.getRGB(x, y);
}
}
Colors.java
import java.awt.Color;
import java.util.List;
abstract class Colors {
List<Color> colors;
Color getColorAtIndex(int index) {
return colors.get(index);
}
int getIndexOfColor(int rgb) throws Exception {
for (int x = 0; x < colors.size(); x++) {
if(colors.get(x).getRGB() == rgb) {
return x;
}
}
throw new Exception("Color not found in the list!");
}
int getSize() {
return colors.size();
}
}
FireColors.java
import java.awt.Color;
import java.util.List;
class FireColors extends Colors {
FireColors(List<Color> colors) {
this.colors = colors;
this.colors.add(new Color(255, 255, 255));
this.colors.add(new Color(239, 239, 199));
this.colors.add(new Color(223, 223, 159));
this.colors.add(new Color(207, 207, 111));
this.colors.add(new Color(183, 183, 55));
this.colors.add(new Color(183, 183, 47));
this.colors.add(new Color(183, 175, 47));
this.colors.add(new Color(191, 175, 47));
this.colors.add(new Color(191, 167, 39));
this.colors.add(new Color(191, 167, 39));
this.colors.add(new Color(191, 159, 31));
this.colors.add(new Color(191, 159, 31));
this.colors.add(new Color(199, 151, 31));
this.colors.add(new Color(199, 143, 23));
this.colors.add(new Color(199, 135, 23));
this.colors.add(new Color(207, 135, 23));
this.colors.add(new Color(207, 127, 15));
this.colors.add(new Color(207, 119, 15));
this.colors.add(new Color(207, 111, 15));
this.colors.add(new Color(215, 103, 15));
this.colors.add(new Color(215, 95, 7));
this.colors.add(new Color(223, 87, 7));
this.colors.add(new Color(223, 87, 7));
this.colors.add(new Color(223, 79, 7));
this.colors.add(new Color(199, 71, 7));
this.colors.add(new Color(191, 71, 7));
this.colors.add(new Color(175, 63, 7));
this.colors.add(new Color(159, 47, 7));
this.colors.add(new Color(143, 39, 7));
this.colors.add(new Color(119, 31, 7));
this.colors.add(new Color(103, 31, 7));
this.colors.add(new Color(87, 23, 7));
this.colors.add(new Color(71, 15, 7));
this.colors.add(new Color(47, 15, 7));
this.colors.add(new Color(7, 7, 7));
}
}
Your problem is that FireColors contains duplicate colors:
// FireColors, lines 20 and 21:
this.colors.add(new Color(191, 167, 39));
this.colors.add(new Color(191, 167, 39));
// more duplicate colors found later on!
The problem is together with your color selection algorithm:
// Launcher lines 20 to 22:
int below_pixel_rgb = gui.getRGBAtCoordinates(x, y + 1);
int index_of_found_color = colors.getIndexOfColor(below_pixel_rgb);
int index_of_color_to_apply = (index_of_found_color + 1) % colors.getSize();
For the 8th line, it reads the color from the line below it, finds its index (7), adds one and colors that line with the color #8.
For the 9th line, it reads the color from the line below it, finds its index (8), adds one and colors that line with the color #9 (which is the same as color #8)
For the 10th line, it reads the color from the line below it, finds its index (8, because getIndexOfColor() returns the first found index, which is 8, not 9!), adds one and colors that line with the color #9 (which is the same as color #8)
To fix it, you should either redesign your color choosing algorithm or make your FireColor colors unique.

Processing - Make an object disappear and appear in certain frames of time

This is my current code:
int doorCounter = 0;
void setup()
{
size(512, 348); //width and height of screen
doorCounter = (int)random(180,300);
}
void draw()
{
display();
doorCounter = doorCounter - 1; // Decrease count by 1
if (doorCounter <= 0)
{
fill(255);
rect(420, 190, 55, 100); //house door outline
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
doorCounter = (int)random(180,480);
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0,0,0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}
However what this code does is just makes the object disappear for a fraction of a second and just makes it reappear instantly. How do I make it so that the object stays disappeared for 3-8 seconds on a random interval just like how the object disappears every 3-8 seconds given that its still on the screen?
P.s I don't know if what I'm trying to achieve makes sense so please feel free to question.
An idea is to use a timestamp and check the time elapsed from it, something like this:
int min_time = 3000; // in ms
int max_time = 8000; // in ms
int time_frame = (int)random(min_time, max_time);
int time_stamp = 0;
boolean show_door = true;
void setup()
{
size(512, 348); //width and height of screen
}
void draw()
{
background(200);
int time_passed = millis() - time_stamp;
if (time_passed < time_frame && show_door) {
display();
} else if (time_passed >= time_frame) {
time_stamp = millis();
time_frame = (int)random(min_time, max_time);
show_door = !show_door;
}
}
void display()
{
fill(255);
rect(420, 190, 55, 100); //house door outline
fill(0, 0, 0); // fill the following polygons in black
rect(435, 210, 25, 25, 7); // house door window
ellipse(435, 255, 8, 8); // house door handle
}

How to get a graphics-method, such as paint, to activate on a button-click

For a school-project, we've recieved a set amount of figures we're supposed to paint on a canvas with the normal Java-graphics method .paint(). This is supposed to be controlled with the help of a few buttons, each painting a different mix of colors. The paint-method is quite easy and works but when we try to connect the buttons, nothing happens but the buttons appearing on the screen. The code reads as following:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class MarcusErikaGrf2 extends Applet implements ActionListener {
Button knapp, knapp2, knapp3, knapp4, knapp5;
boolean svart=false, rod=false, gul=false, gron=false, rensa=false;
int tx[],ty[],pox[],poy[],pgx[],pgy[],pgxf[],pgyf[],bx[],by[],bxf[],byf[],hf[],hy[];
Polygon txy, po, pg, pgf, b, bf, hxy;
public void init (){
this.setSize(800,900);
this.setBackground(Color.white);
this.setLocation(200,1);
knapp= new Button ("Svart");
knapp2= new Button ("Röd & Rosa");
knapp3= new Button ("Gul & Orange");
knapp4 = new Button ("Grön & Blå");
knapp5 = new Button ("Rensa fönstret");
knapp.addActionListener(this);
knapp2.addActionListener(this);
knapp3.addActionListener(this);
knapp4.addActionListener(this);
knapp5.addActionListener(this);
add(knapp);
add(knapp2);
add(knapp3);
add(knapp4);
add(knapp5);
int tx[] = {375,475,425};
int ty[] = {110,110,250};
int pox[] = {65,120,120,65,50};
int poy[] = {350,350,450,450,400};
int pgx[] = {35,88,98,88,35,25};
int pgy[] = {210,210,270,330,330,270};
int pgxf[] = {36,87,97,87,36,26};
int pgyf[] = {211,211,270,329,329,270};
int bx[] = {372, 400, 395, 420, 415, 440/*TP*/, 415, 400, 405, 382, 387 };
int by[] = {60 , 67 , 70 , 77 , 80 , 87 /*TP*/, 95, 88 , 85 , 75 , 70};
int bxf[] = {373,399,394,419,414,439,416,401,406,383,388};
int byf[] = {61,67,70,77,80,86,94,88,85,75,70};
int hx[] = {150,185,225};
int hy[] = {176,120,176};
Polygon txy = new Polygon(tx,ty,tx.length);
Polygon po = new Polygon(pox,poy,pox.length);
Polygon pg = new Polygon(pgx,pgy,pgx.length);
Polygon pgf = new Polygon(pgxf,pgyf,pgxf.length);
Polygon b = new Polygon(bx,by,bx.length);
Polygon bf = new Polygon(bxf,byf,bxf.length);
Polygon hxy = new Polygon(hx,hy,hx.length);
}
#Override
public void paint(Graphics g){
if(svart == true){
g.setColor(Color.black);
g.drawRect(50, 50, 30, 150); //Rektangel 1
g.drawRect(90,50,30,150); //Rektangel 2
g.drawOval(140, 60, 40,30); //Öga 1
g.drawOval(200, 62, 40, 30); //Öga 2
g.fillOval(147,67,15,15); //Pupill 1
g.fillOval(218,69,15,15); //Pupill 2
g.fillOval(310,100,50,120); //Svart oval, Stor
g.fillOval(310, 50, 22, 30); //Svart oval, Medium
g.fillOval(270, 220, 20, 28); //Svart oval, Liten
g.drawOval(100,230,140,40); //Rosa oval, outline
g.fillOval(370,250,100,50); //Svart oval, horisontell
g.drawRect(200,300,180,150); //Grön rektangel, outline
g.drawRect(170,369,10,130);
g.drawRect(140,369,10,130);
g.fillOval(250,480,28,40);
g.fillOval(400,480,14,20);
g.drawOval(280,520,122,200);
g.drawOval(160, 570, 80, 100);
g.drawOval(50,680,140,40);
g.drawRect(65,500,65,150);
g.drawPolygon(pg);
g.drawPolygon(b);
g.setColor(Color.white);
g.fillOval(317, 65, 8, 8); //Vit prick i svart oval, Medium
g.fillOval(277, 235, 6, 6); //Vit prick i svart oval, Liten
}
else if(rod == true){
g.setColor(Color.red);
g.fillOval(161,571,79,99);
g.fillOval(255, 150, 50, 70);
g.setColor(Color.pink);
g.fillOval(101, 231, 138, 38); //Rosa oval
g.fillPolygon(bf);
g.setColor(Color.white);
g.fillOval(255, 170, 50, 50);
}
else if(gron == true){
g.setColor(Color.blue);
g.fillRect(51, 51, 29, 149); //Rektangel 1
g.fillRect(91, 51, 29, 149); //Rektangel 2
g.fillRect(66,501,64,149);
g.fillPolygon(txy);
g.setColor(Color.green);
g.fillRect(201, 301, 179, 149); //Grön rektangel
g.fillOval(51,681,139,38);
g.fillPolygon(pgf);
}
else if(gul == true){
g.setColor(Color.yellow);
g.fillRect(171,370,9,129);
g.fillRect(141,370,9,129);
g.fillOval(281,521,120,198);
g.setColor(Color.orange);
g.fillPolygon(po);
g.fillPolygon(hxy);
g.fillOval(150, 160, 45, 45);
g.fillOval(180, 160, 45, 45);
}
else if(rensa == true){
repaint();
}
}
public void actionPerformed (ActionEvent e){
if(e.getSource() == knapp){
svart = true;
}
else if(e.getSource() == knapp2){
rod = true;
}
else if(e.getSource() == knapp3){
gul = true;
}
else if(e.getSource() == knapp4){
gron = true;
}
else if(e.getSource() == knapp5){
rensa = true;
}
}
}
What are we missing?
In your actionPerformed(...) method you need to invoke:
repaint();
This tells the component to repaint itself.
else if(rensa == true){
repaint();
}
Never invoke repaint() from within a painting method. A painting method is for doing the painting, not scheduling the painting.

Refresh text in Applet

I am doing this small race between two cars, in a java applet.
Just two pictures moving at random speed. I am calculating the distance between current position and the finish line, and you are suppose to be able to see the distance in the upper corner.
The thing is I am not able to refresh the text field, instead it just applies a new layer on top of the old number so it is almost impossible to read.
Here are pictures to demonstrate my problem.
I thought I would be able to solve it by creating the blue rectangle at the start of each loop but that does not seem to solve it.
public void action(){
Random rand = new Random();
boolean race = true;
int x1 =500, y1 = 233;
int x2 = 500, y2 = 333;
int speed1 = rand.nextInt(15) + -16;
int speed2 = rand.nextInt(15) + -16;
int finishline = 30;
Text winnerBlue = new Text("Winner: BLUE",new Font("SansSerif",Font.BOLD,20), Color.blue,Color.white);
Text winnerRed = new Text("Winner: RED",new Font("SansSerif",Font.BOLD,20), Color.red,Color.white);
//background
Text text =null;
Text text2 = null;
window.fillRect(0, 0, 600, 400, Color.GREEN);
//track 1
window.fillRect(20, 330, 550, 39, Color.gray);
//track2
window.fillRect(20, 230, 550, 39, Color.gray);
//Finish line
window.fillRect(40, 210, 10, 180, Color.BLACK);
while(race){
text = new Text(Integer.toString(x1),new Font("Courier",Font.BOLD,20), Color.WHITE);
text2 = new Text(Integer.toString(x2),new Font("SansSerif",Font.BOLD,20), Color.WHITE);
window.fillRect(0, 0, 70, 50, Color.blue);
window.fillRect(70, 0, 70, 50, Color.red);
window.showImage(text, 0, 0);
window.showImage(text2, 70, 0);
window.showImage(car1.getImage(), x1, y1);
window.showImage(car2.getImage(), x2, y2);
car1.moveTo(x1 += speed1, y1);
car2.moveTo(x2 += speed2, y2);
window.pause(50);
if(x1 <= (finishline ) ){
speed1 = 0;
speed2 = 0;
window.showImage(winnerBlue, 200, 200);
race = false;
}
if(x2 <= (finishline)){
speed2 = 0;
speed1 = 0;
window.showImage(winnerRed, 200, 200);
race = false;
}
}
}
}
For the two screen shots and supplied code snippt, it's clear that you don't understand how painting works in Swing/AWT.
Do NOT ever, maintain any kind of refernce to the Graphics context out side of the paintXxx methods.
The paint methods perform a number of very important steps to prepare the Graphics context for painting
Start by taking a look through Performing Custom Painting

Categories

Resources