Results 1 to 4 of 4

Thread: Flickering

  1. #1
    slum is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Flickering

    Hi I have tried image buffering as well as overriding the update() method. But when the sun rises the buildings still flicker. Anyone please help. Thanks.

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package building2;

    import java.awt.*;
    import java.util.Random;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;


    /**
    *
    * @author slum
    */
    public class Skyline2 extends JPanel {

    private int l = 0;
    private Random r = new Random ();
    private int[] x1 = {rand(), rand(), rand(), rand(), rand(), rand(),
    rand(), rand(), rand(), rand(), rand(), rand(),
    rand(), rand(), rand(), rand()} ;
    private int[] y1 = {rando(), rando(), rando(), rando(), rando(), rando(),
    rando(), rando(), rando(), rando(), rando(), rando(),
    rando(), rando(), rando(), rando()};
    private int[] sizeW1 = {ranW1(), ranW1(), ranW1(), ranW1(), ranW1(), ranW1(),
    ranW1(), ranW1(), ranW1(), ranW1(), ranW1(), ranW1(),
    ranW1(), ranW1(), ranW1(), ranW1()};
    private int[] sizeW2 = {ranW2(), ranW2(), ranW2(), ranW2(), ranW2(), ranW2(),
    ranW2(), ranW2(), ranW2(), ranW2(), ranW2(), ranW2(),
    ranW2(), ranW2(), ranW2(), ranW2()};
    private int[] posW1 = {r.nextInt(x1[0]), r.nextInt(x1[1]), r.nextInt(x1[2]), r.nextInt(x1[3]),
    r.nextInt(x1[4]), r.nextInt(x1[5]), r.nextInt(x1[6]), r.nextInt(x1[7]),
    r.nextInt(x1[8]), r.nextInt(x1[9]), r.nextInt(x1[10]), r.nextInt(x1[11]),
    r.nextInt(x1[12]), r.nextInt(x1[13]), r.nextInt(x1[14]), r.nextInt(x1[15])};
    private int[] posW2 = {r.nextInt(y1[0]), r.nextInt(y1[1]), r.nextInt(y1[2]), r.nextInt(y1[3]),
    r.nextInt(y1[4]), r.nextInt(y1[5]), r.nextInt(y1[6]), r.nextInt(y1[7]),
    r.nextInt(y1[8]), r.nextInt(y1[9]), r.nextInt(y1[10]), r.nextInt(y1[11]),
    r.nextInt(y1[12]), r.nextInt(y1[13]), r.nextInt(y1[14]), r.nextInt(y1[15])};


    private int left = 0;

    private int moonY = 5;
    private int sunY = 200;

    private int sunMove = r.nextInt(435);
    private int moonMove = r.nextInt(435);

    private int moonInc = 0;
    private int sunInc = 0;

    private int p = 0;

    private int pos0 = 0;
    private int pos450 = 450;

    private int a = 0, b = 450;

    private int q = 20;

    private int bright = 0;
    private int brightEnd = 100;
    private int brighter = 0;

    private boolean sunUp = false;
    private boolean sunset = true;

    private int s = r.nextInt(400);
    private int t = r.nextInt(100)+50;
    private int g = r.nextInt(50);
    private int gs = r.nextInt(50)+30;
    private int count = 0;

    private int[] cloudSizeX = {r.nextInt(100)+50, r.nextInt(100)+50, r.nextInt(100)+50, r.nextInt(100)+50};
    private int[] cloudSizeY = {r.nextInt(50)+30, r.nextInt(50)+30, r.nextInt(50)+30, r.nextInt(50)+30};
    private int[] cloudX = {s, s-cloudSizeX[1], s-(cloudSizeX[2]/2), s-(cloudSizeX[3]/2)};
    private int[] cloudY = {g, g, g-(cloudSizeY[2]/2), g+(cloudSizeY[3]/2)};

    private Color color = new Color (r.nextInt(255),r.nextInt(255),r.nextInt(255));
    private Color color2 = new Color (r.nextInt(255),r.nextInt(255),r.nextInt(255));
    private Color background = new Color (0, 0, 100);
    private Color colour = Color.yellow;
    private Color buildingC = Color.black;
    private Color c = new Color (0, 50, 0);
    private Color road = Color.darkGray;
    private Color lights = Color.yellow;
    private int[] starX = {r.nextInt(445), r.nextInt(445), r.nextInt(445), r.nextInt(445), r.nextInt(445),
    r.nextInt(445), r.nextInt(445), r.nextInt(445), r.nextInt(445), r.nextInt(445)};
    private int[] starY = {r.nextInt(90), r.nextInt(90), r.nextInt(90), r.nextInt(90), r.nextInt(90),
    r.nextInt(90), r.nextInt(90), r.nextInt(90), r.nextInt(90), r.nextInt(90)};

    private Timer timer = new Timer (r.nextInt(50)+1, new moveCar());
    private Timer timer2 = new Timer (r.nextInt(50)+1, new moveCar1());
    private Timer moonTimer = new Timer (50, new moonRS());

    public Skyline2() { // constructor

    //setBackground(background);
    setPreferredSize(new Dimension(450, 350));
    timer.start();
    timer2.start();
    moonTimer.start();
    makeCarX(pos0);
    makeCarX(pos450);

    }

    public int rand () { //random building width
    return r.nextInt(25) + 25;
    }

    public int rando () { //random building height
    return r.nextInt(70) + 40;
    }

    public int ranW1 () { //random window width
    return r.nextInt(5) + 5;
    }

    public int ranW2 () {
    return r.nextInt(10) + 5; //random window height
    }

    public int[] makeCarX (int pos) { // create polygon dimension x values
    pos = pos*getWidth()/450;
    int[] carX = {pos, pos, pos+(4*getWidth()/450), pos+(6*getWidth()/450), pos+(10*getWidth()/450),
    pos+(12*getWidth()/450), pos+(16*getWidth()/450), pos+(16*getWidth()/450)};
    return carX;
    }

    public int[] makeCarY (int pos) { // create polygon dimension y values
    pos = pos*getHeight()/350;
    int[] carY = {pos, pos-(5*getHeight()/350), pos-(5*getHeight()/350), pos-(11*getHeight()/350),
    pos-(11*getHeight()/350), pos-(5*getHeight()/350), pos-(5*getHeight()/350), pos};
    return carY;
    }

    public void paint (Graphics page)

    { super.paintComponent (page);
    Image img = createImage();
    page.drawImage(img, 0, 0, this);

    }

    public void update(Graphics g) {
    paint(g);
    }

    private Image createImage() {

    BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
    Graphics page = image.getGraphics();

    setBackground(new Color(bright, brighter, brightEnd));

    page.setColor(Color.white); // moon
    page.fillOval(moonMove, (moonY*getHeight()/350), (20*getWidth()/450), (20*getWidth()/450));

    page.setColor(Color.yellow); // sun
    page.fillOval(sunMove, (sunY*getHeight()/350), (35*getWidth()/450), (35*getWidth()/450));

    page.setColor(Color.white);
    if (sunUp == true)
    for (int cCount = 0; cCount < 4; cCount++) { // clouds
    page.fillOval(cloudX[cCount], cloudY[cCount], cloudSizeX[cCount], cloudSizeY[cCount]);

    }
    if (sunset == true) // stars
    for (int sCount = 0; sCount < 10; sCount++) {
    page.drawLine(starX[sCount]*getWidth()/450, starY[sCount]*getHeight()/350, starX[sCount]*getWidth()/450, (starY[sCount]+6)*getHeight()/350);
    page.drawLine((starX[sCount]-3)*getWidth()/450, (starY[sCount]+3)*getHeight()/350, (starX[sCount]+3)*getWidth()/450, (starY[sCount]+3)*getHeight()/350);
    }

    page.setColor(buildingC);
    for (int left = 0; p < 14; left = left + (int)(x1[p] * getWidth()/450)) //how do you use a global variable within a loop
    {
    p++;

    page.setColor(buildingC);
    page.fillRect(left, ((200-y1[p])*getHeight()/350), (x1[p]*getWidth()/450), (y1[p]*getHeight()/350)+5); //buildings

    page.setColor(colour);

    if (posW1[p] + sizeW1[p] >= x1[p]) // window width greater (special)
    if (posW2[p]+200-y1[p] >= 200-sizeW2[p]) // window height greater
    page.fillRect(left + ((x1[p]-posW1[p])*getWidth()/450), (200-posW2[p])*getHeight()/350, sizeW1[p]*getWidth()/450, sizeW2[p]*getHeight()/350);
    else // window height less than
    page.fillRect(left + ((x1[p]-posW1[p])*getWidth()/450), ((posW2[p]+200-y1[p])*getHeight()/350), sizeW1[p]*getWidth()/450, sizeW2[p]*getHeight()/350);
    else // window width less than (normal)
    if (posW2[p]+200-y1[p] >= 200-sizeW2[p]) //window height greater
    page.fillRect(left + ((posW1[p])*getWidth()/450), (200-posW2[p])*getHeight()/350, sizeW1[p]*getWidth()/450, sizeW2[p]*getHeight()/350);
    else //window height less than
    page.fillRect(left + ((posW1[p])*getWidth()/450), ((posW2[p]+200-y1[p])*getHeight()/350), sizeW1[p]*getWidth()/450, sizeW2[p]*getHeight()/350);
    }

    page.setColor(road); // road
    page.fillRect(0, (200*getHeight()/350), (500*getWidth()/450), (32*getHeight()/350));

    page.setColor(c); // grass
    page.fillRect(0, (230*getHeight()/350), (500*getWidth()/450), (200*getHeight()/450));

    page.setColor(Color.white);
    for (int l = 0; l < (500*getWidth()/450); l = l + (q*getWidth()/450) + (20*getWidth()/450)) //white lines
    page.drawLine(l, (215*getHeight()/350), l+(q*getWidth()/450), (215*getHeight()/350));

    if (timer.getDelay() <= 15) {
    Color newCol = new Color (r.nextInt(255), r.nextInt(255), r.nextInt(255));
    page.setColor(newCol);
    }
    else
    page.setColor(color);

    page.fillPolygon(makeCarX(a), makeCarY(225), 8);
    page.setColor(Color.black); // car a
    page.fillOval((a*getWidth()/450)+(2*getWidth()/450), (223*getHeight()/350), (4*getWidth()/450), (4*getHeight()/350));
    page.fillOval((a*getWidth()/450)+(10*getWidth()/450), (223*getHeight()/350), (4*getWidth()/450), (4*getHeight()/350));
    a++;

    if (timer2.getDelay() <= 15) { // police car
    Color newCol = new Color (r.nextInt(255), r.nextInt(255), r.nextInt(255));
    page.setColor(newCol);
    }
    else
    page.setColor(color2);

    page.fillPolygon(makeCarX(b), makeCarY(210), 8); // car b
    page.setColor(Color.black);
    page.fillOval((b*getWidth()/450)+(2*getWidth()/450), (208*getHeight()/350), (4*getWidth()/450), (4*getHeight()/350)); // car a
    page.fillOval((b*getWidth()/450)+(10*getWidth()/450), (208*getHeight()/350), (4*getWidth()/450), (4*getHeight()/350));
    b--;

    for (int l = 0; l <= (450*getWidth()/450); l = l + (50*getWidth()/450)) // lamp posts
    {
    page.setColor(Color.black);
    page.drawLine(l, (230*getHeight()/350), l, (200*getHeight()/350));
    page.setColor(lights);
    page.drawOval(l-(2*getWidth()/450), (196*getHeight()/350), (4*getWidth()/450), (4*getHeight()/350));
    }
    return image;
    }


    public void makeClouds(int x, int y) {

    cloudX[0] = x;
    cloudX[1] = x-cloudSizeX[1];
    cloudX[2] = x-(cloudSizeX[2]/2);
    cloudX[3] = x-(cloudSizeX[3]/2);
    cloudY[0] = y;
    cloudY[1] = y;
    cloudY[2] = y-(cloudSizeY[2]/2);
    cloudY[3] = y+(cloudSizeY[3]/2);
    }

    private class moonRS implements ActionListener
    { public void actionPerformed(ActionEvent ae)
    {
    if ((moonY*getHeight()/350) > (200*getHeight()/350)) { //moon at bottom
    if (sunInc == 0) {

    sunY--;
    }
    moonInc = 0;
    }

    if (sunY == 5) { // sun at top
    sunInc++;
    }
    if (sunInc == 30)
    sunY++;


    if ((sunY*getHeight()/350) > (200*getHeight()/350)) { // sun at bottom
    if (moonInc == 0)
    moonY--;
    sunInc = 0;
    }

    if (moonY == 5) { // moon at top
    moonInc++;
    }
    if (moonInc == 30) {
    moonY++;
    }

    if ((sunY*getHeight()/350) < (150*getHeight()/350)) { // sun above (200, 230, 255)
    if (brightEnd < 252) {
    bright = bright + 10;
    brighter = brighter + 11;
    brightEnd = brightEnd + 8;
    sunset = false;
    }
    else // sunrise complete
    buildingC = new Color (100, 100, 100);
    colour = new Color (200, 230, 255);
    c = new Color (0, 100, 0);
    road = Color.gray;
    lights = Color.black;

    sunUp = true;
    count++;
    if (count == 1) {
    s = r.nextInt(400);
    g = r.nextInt(50);
    sunMove = r.nextInt(435);
    moonMove = r.nextInt(435);
    makeClouds(s, g);
    }
    }
    else if ((sunY*getHeight()/350) >= (150*getHeight()/350)) // sun below (0, 0, 100)
    if (brightEnd > 100) {
    bright = bright - 10;
    brighter = brighter - 11;
    brightEnd = brightEnd - 8;
    sunUp = false;
    }
    else { // sunset complete
    colour = Color.yellow;
    buildingC = Color.black;
    c = new Color (0, 50, 0);
    road = Color.darkGray;
    lights = Color.yellow;
    sunset = true;
    count = 0;
    }

    }

    }



    private class moveCar1 implements ActionListener
    {
    public void actionPerformed(ActionEvent ae)
    {

    if (b > -16)
    b--;
    else {
    b = 450;
    color2 = new Color (r.nextInt(255),r.nextInt(255),r.nextInt(255));
    timer2.setDelay(r.nextInt(50)+1);
    }

    }
    }

    private class moveCar implements ActionListener
    {
    @Override
    public void actionPerformed(ActionEvent ae) {

    if (p == 14)
    p=0;
    if (a < 450)
    a++;
    else {
    a = 0;
    color = new Color (r.nextInt(255),r.nextInt(255),r.nextInt(255));
    timer.setDelay(r.nextInt(50)+1);
    }
    repaint();
    //update();
    }
    }

    }

  2. #2
    slum is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Flickering

    Main method.

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package building2;

    import javax.swing.*;
    import java.awt.*;

    /**
    *
    * @author slum
    */
    public class Building2 {//implements MouseListener {
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    JFrame frame = new JFrame ("Skyline");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Skyline2());
    frame.setVisible(true);
    frame.pack();

    }
    }

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Flickering

    Its a big unformatted mess. But skimming through it I do spot this:

    Java Code:
    public void paint (Graphics page)   // <---- wrong, you should be override paintComponent(), not paint().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Flickering

    And I suggest you create the image before calling repaint, not in the paintComponent method itself. Which means you should also pack the frame before setting visible to true and ensure img != null before using it.

    And it is usually a good thing to set local values from getters instead of calling getters all over the place.
    e.g.
    Java Code:
    int w = getWidth();
    This avoids repetitive method calls. It also makes the code less cluttered.

    Finally, not only should you format your code but always (read always) use {} to delineate if/else blocks. Even for
    single statements. Then there is no ambiguity in what you are trying to do.

    Regards,
    Jim
    Last edited by jim829; 09-12-2014 at 04:11 PM. Reason: missing preposition - I would be better off using pig latin!!
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. My BufferedImage Flickering
    By ShadowWalker in forum AWT / Swing
    Replies: 6
    Last Post: 07-25-2013, 12:34 PM
  2. How to animate without flickering?
    By Farscience in forum Java 2D
    Replies: 3
    Last Post: 05-16-2013, 05:13 AM
  3. Flickering on a 2D Sprite
    By driiper in forum Java 2D
    Replies: 5
    Last Post: 05-06-2012, 05:30 PM
  4. Replies: 3
    Last Post: 02-29-2012, 09:38 PM
  5. Applet flickering
    By samson in forum Java 2D
    Replies: 3
    Last Post: 09-21-2007, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •