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!
Related
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
}
I am trying to make a little mini game, where u can move around. I have written a code, which did work. But when I tried to shorten my code and make it looks nicer, I stumble upon a error I could not solve by myself.
Main:
package eksamenstest;
import javax.swing.JFrame;
public class Eksamenstest extends JFrame {
public Eksamenstest() throws InterruptedException
{
JFrame SOJ = new JFrame("Sword Of Justice");
SOJ.pack();
SOJ.setSize(1000,700);
SOJ.setVisible(true);
SOJ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SOJ.add(new Grafik());
}
public static void main(String[] args) throws InterruptedException
{
new Eksamenstest();
new Musik();
new SpillerOne();
}
}
Graphics:
package eksamenstest;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Grafik extends JPanel
{
int Menu = 1;
int Player1Liv = 3;
int Player2Liv = 3;
int DødP1 = 1;
int DødP2 = 1;
int WeaponON2;
int WeaponON1;
int Skjold;
int Skjold1;
int x = 100;
int y = 360;
int x_1 = 820;
int y_1 = 360;
public Grafik(){}
public void paint(Graphics g)
{
//Grafikken af banen
Image Bane2 = Toolkit.getDefaultToolkit().getImage("Bane2.png");
g.drawImage(Bane2, 0, 0, 1000, 800, this);
Image Plank = Toolkit.getDefaultToolkit().getImage("Plank.jpg");
g.drawImage(Plank, 100, 500, 800, 10, this);
Image lava = Toolkit.getDefaultToolkit().getImage("lava.png");
g.drawImage(lava, 0, 520, 1000, 260, this);
if(Menu == 1)
{
Image Menu2 = Toolkit.getDefaultToolkit().getImage("Menu.png");
g.drawImage(Menu2, 100, -30, 900, 700, this);
}
//Player 1 - Grafikken der viser hvordan spilleren står.
if(WeaponON1 == 0){
Image PlayerStå = Toolkit.getDefaultToolkit().getImage("Stå.png");
g.drawImage(PlayerStå, x, y, 80, 140, this);
}
if(WeaponON1 == 1){
Image PlayerSværdOP = Toolkit.getDefaultToolkit().getImage("OP.png");
g.drawImage(PlayerSværdOP, x, y, 80, 140, this);
}
if(WeaponON1 == 2){
Image PlayerSværdFrem = Toolkit.getDefaultToolkit().getImage("Frem.png");
g.drawImage(PlayerSværdFrem, x, y, 80, 140, this);
}
if(Skjold == 1){
Image Player1Skjold = Toolkit.getDefaultToolkit().getImage("Player1Skjold.png");
g.drawImage(Player1Skjold, x, y, 80, 140, this);
}
if(Player1Liv == 0){
Image DødP = Toolkit.getDefaultToolkit().getImage("DødP.png");
g.drawImage(DødP, x, y, 80, 140, this);
}
if(Player2Liv == 0){
Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
g.drawImage(Jubel, x, y, 80, 140, this);
}
//Player 2 - Grafikken der viser hvordan spilleren står.
if(WeaponON2 == 0){
Image PlayerStå1 = Toolkit.getDefaultToolkit().getImage("Stå1.png");
g.drawImage(PlayerStå1, x_1, y_1, 80, 140, this);
}
if(WeaponON2 == 1){
Image PlayerSværdOP1 = Toolkit.getDefaultToolkit().getImage("OP1.png");
g.drawImage(PlayerSværdOP1, x_1, y_1, 80, 140, this);
}
if(WeaponON2 == 2){
Image PlayerSværdFrem1 = Toolkit.getDefaultToolkit().getImage("Frem1.png");
g.drawImage(PlayerSværdFrem1, x_1, y_1, 80, 140, this);
}
if(Skjold1 == 1){
Image Player2Skjold = Toolkit.getDefaultToolkit().getImage("Player2Skjold.png");
g.drawImage(Player2Skjold, x_1, y_1, 80, 140, this);
}
if(Player2Liv == 0){
Image DødPA = Toolkit.getDefaultToolkit().getImage("DødP.png");
g.drawImage(DødPA, x_1, y_1, 80, 140, this);
}
if(Player1Liv == 0){
Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
g.drawImage(Jubel, x_1, y_1, 80, 140, this);
}
//Health Bars / Stamina / Navne / Win
//Player 1
Image PlayerNavn = Toolkit.getDefaultToolkit().getImage("Player1Navn.png");
g.drawImage(PlayerNavn, 30, 50, 70, 30, this);
if(Player1Liv == 3){
Image Liv100B = Toolkit.getDefaultToolkit().getImage("Liv100B.png");
g.drawImage(Liv100B, 30, 80, 120, 40, this);
}
if(Player1Liv == 2){
Image Liv75B = Toolkit.getDefaultToolkit().getImage("Liv75B.png");
g.drawImage(Liv75B, 30, 80, 120, 40, this);
}
if(Player1Liv == 1){
Image Liv50B = Toolkit.getDefaultToolkit().getImage("Liv25B.png");
g.drawImage(Liv50B, 30, 80, 120, 40, this);
}
if(Player1Liv == 0){
Image Liv0B = Toolkit.getDefaultToolkit().getImage("Liv0B.png");
g.drawImage(Liv0B, 30, 80, 120, 40, this);
DødP1 = 0;
Image Player2Win = Toolkit.getDefaultToolkit().getImage("Player2Wins.png");
g.drawImage(Player2Win, 350, 80, 350, 110, this);
}
// Player 2
Image PlayerNavn1 = Toolkit.getDefaultToolkit().getImage("Player2Navn.png");
g.drawImage(PlayerNavn1, 900, 50, 70, 30,this);
if(Player2Liv == 3){
Image Liv100R = Toolkit.getDefaultToolkit().getImage("Liv100R.png");
g.drawImage(Liv100R, 850, 80, 120, 40, this);
}
if(Player2Liv == 2){
Image Liv75R = Toolkit.getDefaultToolkit().getImage("Liv75R.png");
g.drawImage(Liv75R, 850, 80, 120, 40, this);
}
if(Player2Liv == 1){
Image Liv50R = Toolkit.getDefaultToolkit().getImage("Liv25R.png");
g.drawImage(Liv50R, 850, 80, 120, 40, this);
}
if(Player2Liv == 0){
Image Liv0R = Toolkit.getDefaultToolkit().getImage("Liv0R.png");
g.drawImage(Liv0R, 850, 80, 120, 40, this);
DødP2 = 0;
Image Player1Win = Toolkit.getDefaultToolkit().getImage("Player1Wins.png");
g.drawImage(Player1Win, 350, 80, 350, 110, this);
}
}
}
Player Movement
package eksamenstest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class SpillerOne extends JFrame implements ActionListener, KeyListener {
int x = 100;
int y = 360;
int xHøjre;
int xVenstre;
int DødP1;
int Player1Liv;
int iLava;
int x_1;
int Player2Liv;
int Skjold1;
int WeaponON1;
int Menu = 1;
int SværdTid;
int Sværd;
int Skjold;
public SpillerOne()
{
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
#Override
public void actionPerformed(ActionEvent e)
{
PlayerOneMove();
repaint();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
//Menu
if(c == KeyEvent.VK_O){
Menu = 1;
}
if(c == KeyEvent.VK_P){
Menu = 0;
}
// Player
if(DødP1 == 1){
if(c == KeyEvent.VK_A){
xVenstre = 1;
WeaponON1 = 0;
Sværd = 0;
}
if(c == KeyEvent.VK_D){
xHøjre = 1;
WeaponON1 = 0;
}
if(WeaponON1 == 1){
if(SværdTid < 3){
if(c == KeyEvent.VK_W){
WeaponON1 = 2;
SværdTid++;
}
}
}
if(c == KeyEvent.VK_S){
WeaponON1 = 1;
Sværd = 1;
}
if(c == KeyEvent.VK_Q){
Skjold = 1;
SværdTid++;
}
}
if(c == KeyEvent.VK_E){
SværdTid--;
WeaponON1 = 0;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int c = e.getKeyCode();
// Spiller 1
if(c == KeyEvent.VK_A){
xVenstre = 0;
}
if(c == KeyEvent.VK_D){
xHøjre = 0;
}
if(c == KeyEvent.VK_Q){
Skjold = 0;
SværdTid = SværdTid;
}
if(c == KeyEvent.VK_W){
if(Sværd == 1)
WeaponON1 = 1;
}
if(c == KeyEvent.VK_E){
SværdTid = SværdTid;
WeaponON1 = 0;
}
}
public void PlayerOneMove()
{
// Spiller et Bevægelse
x = x + xHøjre; //Højre
x = x - xVenstre; //Venstre
if(WeaponON1 == 2){ //Tjekker om spilleren har våbnet fremme og hvis ja, så tjekker den om modspilleren har skjold på, hvis ikke, mister person 2 liv.
if(Skjold1 == 0){
if(x > x_1-80 && x < x_1 + 80){
x = 100;
x_1 = 820;
Player2Liv--;
}
}
if(WeaponON1 == 2){
if(Skjold1 == 1){
if(x > x_1-80 && x < x_1 + 80){
x_1 = x_1 + 60;
}
}
}
}
// Falder ned i lava
if(x < 100 && 100 > x + 80){ //Tjekker om player 1 er i lava ved venstre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}
if(x > 900){ //Tjekker om player 1 er i lava ved højre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}
}
}
I have a feeling that this error is caused because I have not repainted somewhere.
So, a list of issues...
First
There are two JFrames - which one is actually been used for what? As far as I can tell, SpillerOne is never displayed, so there is no possible way for it to be able to react to key events. Equally, your ActionListener doesn't seem to be attached to anything which can actually generate actions.
This also raises a bunch of questions about the relationship between SpillerOne and Grafik. They seem to be sharing variable names, but there is no way for them to be sharing state, so even if the KeyListener worked, Grafik would never reflect the changes in SpillerOnes state
Second
You've implemented your custom painting the wrong way. It's highly discouraged to override paint, instead, you should be overriding paintComponent AND calling super.paintComponent to ensure that requirements of the paint chain are upheld
See Performing Custom Painting and Painting in AWT and Swing for more details
You also seem to be putting a lot of state logic into your Grafik class, this is going to become exponentially more difficult to maintain as the complexity increases. Instead, each distinct operation should be it's own class, focused on performing as few dedicated operations as possible
Thirdly
KeyListener is a poor choice for monitoring for key events. While there are "hacks" that attempt to "solve" the focus related issues with KeyListener, none of them can achieve a reliable result.
If you do any research into KeyListener related issues, you will quickly find that the Key bindings API is often sighted as the most reliable solution to the problem
Overall
You code makes no sense. Why are there two frames? What is the ActionListener for and how is it attached to something that generates actions? Why doesn't the Grafik act as the key/action listener?
I think you have some significant thinking and redesigning to do
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.
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)
So I'm just starting with Applets and I am making a house with an open door and 2 open windows when the applet loads. When you click these windows or door then they will shut. My question though is what do I need to do to re-open these windows doors. I figure I need to set the Boolean variables to False somehow and repaint. Where would I do this. I don't need you guys to write the code for me, I just want to know what I should do.
Thanks ahead of time,
Rick
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
Rick Armstrong
Chapter 14 - House Applet
*/
public class HouseApplet extends Applet {
boolean leftWin, rightWin, door;
public void init()
{
leftWin = false;
rightWin = false;
door = false;
setBackground(Color.white);
addMouseListener(new MyMouseListener());
}
public void paint(Graphics g)
{
super.paint(g);
// Draw the house.
g.setColor(Color.black);
g.drawRect(100, 100, 200, 100);
// Draw the roof
g.drawLine(80, 100, 320, 100);
g.drawLine(80, 100, 200, 40);
g.drawLine(200, 40, 320, 100);
// Draw the left window open.
g.fillRect(120, 130, 40, 40);
// Draw the right window open.
g.fillRect(240, 130, 40, 40);
// Draw the door open.
g.fillRect(180, 130, 40, 70);
if (leftWin) {
// Draw the left window closed.
g.setColor(Color.white);
g.fillRect(120, 130, 40, 40);
g.setColor(Color.black);
g.drawRect(120, 130, 40, 40);
g.drawLine(140, 130, 140, 170);
g.drawLine(120, 150, 160, 150);
}
if (rightWin) {
// Draw the right window closed.
g.setColor(Color.white);
g.fillRect(240, 130, 40, 40);
g.setColor(Color.black);
g.drawRect(240, 130, 40, 40);
g.drawLine(260, 130, 260, 170);
g.drawLine(240, 150, 280, 150);
}
if (door) {
// Draw the door closed.
g.setColor(Color.white);
g.fillRect(180, 130, 40, 70);
g.setColor(Color.black);
g.drawRect(180, 130, 40, 70);
g.fillOval(210, 165, 07, 07);
}
}
private class MyMouseListener implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
int currentx = e.getX();
int currenty = e.getY();
boolean WindowLeft = (currentx >= 120 && currentx < 160 && currenty >= 130 && currenty <= 170);
if (WindowLeft)
{
leftWin = true;
repaint();
}
boolean WindowRight = (currentx >= 240 && currentx < 280 && currenty >= 130 && currenty <= 170);
if (WindowRight)
{
rightWin = true;
repaint();
}
boolean Door = (currentx >= 180 && currentx < 220 && currenty >= 40 && currenty <= 200);
if (Door)
{
door = true;
repaint();
}
else;
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
}
Maybe I'm misunderstanding the question, but why are you always setting the values to true in the event handler?
If you want a toggling behavior, you could simply write: value = !value and then repaint.
Since you initially set your value to false, the next click would set it to true, the next to false, etc. etc.
For example:
if (WindowLeft)
{
leftWin = !leftWin;
repaint();
}
Note that it is possible for you to cause sort of a "race condition" by clicking faster than the framework has a chance to update the view, but this is usually not a problem for initial problem.
BTW: In terms of readability, consider naming your variables in a way that conveys their meaning. For example, is the naming door evocative of a boolean? not really. but doorOpen is, and it helps interpret the meaning of the variable and its transitions.
if (WindowRight)
{
rightWin = false;
repaint();
}
else {
rightWin = true;
repaint();
}
try the above. When you click inside the window, it closes otherwise it opens