How can I rearrange my code for the rain and poster to appear

110 views Asked by At

So here I have a set of code to display generated polygons with a background:

public class Drawing extends JFrame{
  public static void main(String []args)
  {
        Drawing b = new Drawing();
  }

public Drawing()
{
  //JFrame frame = new JFrame("Poster");
  setSize(1180,750);
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  getContentPane().add(new MyComponent());
  setVisible(true);

  //background
  setLayout(new BorderLayout());
  JLabel background=new JLabel(new ImageIcon("J:\\1. Current Works\\CSC533\\1. project 1.0\\bg.jpg"));
  add(background);
  background.setLayout(new FlowLayout());
}

public class MyComponent extends JComponent
{
    public void paint(Graphics g)
    {            
        /*Iron Man Part Start*/

        //Iron Man
        Graphics2D g2 = (Graphics2D)g;
        g.setColor(Color.red);//face
        g.fillOval(200,60,190,200);

        //MaskKiri
        Polygon mask1 = new Polygon();
        mask1.addPoint(230,85);//1st
        mask1.addPoint(230,200);//2nd    
        mask1.addPoint(260,255);//3rd
        mask1.addPoint(290,255);//4rd
        mask1.addPoint(290,100);//5rd
        mask1.addPoint(250,100);//6rd
        mask1.addPoint(250,85);//7rd
        g.setColor(Color.yellow);
        g.fillPolygon(mask1);

        //MaskKanan
        Polygon mask2 = new Polygon();
        mask2.addPoint(280,100);//1st
        mask2.addPoint(280,255);//2nd    
        mask2.addPoint(330,255);//3rd
        mask2.addPoint(360,200);//4rd
        mask2.addPoint(360,85);//5rd
        mask2.addPoint(340,85);//6rd
        mask2.addPoint(340,100);//7rd
        g.setColor(Color.yellow);
        g.fillPolygon(mask2);

        //Iron Man Left Eye
        g.setColor(Color.red);
        g.fillRect(240,150,40,10);

        //Iron Man Right Eye
        g.setColor(Color.red);
        g.fillRect(310,150,40,10);

        //Iron Man Fucking Mouth
        g.setColor(Color.red);
        g.fillRect(255,220,80,10);

        /*Iron Man Part End*/

        /*Flash Part Start*/

        //Flash code
        Graphics2D g3 = (Graphics2D)g;
        g.setColor(Color.red);//flash
        g.fillOval(800,50,200,200);

        //Flash eye (lebar,bawah)
        Polygon eye2 = new Polygon();
        eye2.addPoint(830,100);//1st
        eye2.addPoint(830,140);//2nd    
        eye2.addPoint(870,150);//3rd
        eye2.addPoint(870,126);//4rd
        g.setColor(Color.white);
        g.fillPolygon(eye2);

        //Flash eye (lebar,bawah)
        Polygon eye3 = new Polygon();
        eye3.addPoint(970,100);//1st
        eye3.addPoint(970,140);//2nd    
        eye3.addPoint(930,150);//3rd
        eye3.addPoint(930,126);//4rd
        g.setColor(Color.white);
        g.fillPolygon(eye3);

        //Flash mouth (lebar,bawah)
        Polygon mouth = new Polygon();
        mouth.addPoint(830,160);//1st
        mouth.addPoint(830,210);//2nd    
        mouth.addPoint(900,230);//3rd
        mouth.addPoint(900,186);//4rd
        g.setColor(Color.white);
        g.fillPolygon(mouth);

        //Flash mouth (lebar,bawah)
        Polygon mouth2 = new Polygon();
        mouth2.addPoint(970,160);//1st
        mouth2.addPoint(970,210);//2nd    
        mouth2.addPoint(900,230);//3rd
        mouth2.addPoint(900,186);//4rd
        g.setColor(Color.white);
        g.fillPolygon(mouth2);

        //Flash ear kanan (lebar,bawah)
        Polygon ear = new Polygon();
        ear.addPoint(1050,85);//1st    
        ear.addPoint(995,180);//2nd
        ear.addPoint(995,136);//3rd
        g.setColor(Color.yellow);
        g.fillPolygon(ear);

        //Flash ear2 kiri (lebar,bawah)
        Polygon ear2 = new Polygon();
        ear2.addPoint(770,85);//1st
        ear2.addPoint(805,180);//2nd
        ear2.addPoint(805,136);//3rd
        g.setColor(Color.yellow);
        g.fillPolygon(ear2);

        /* Flash Part End */

        /*Spiderman Part Start*/

        Graphics2D g4 = (Graphics2D)g;
        g.setColor(Color.red);
        g.fillOval(800,400,200,200);

        Polygon eye4 = new Polygon();
        eye4.addPoint(820,500);//1st
        eye4.addPoint(820,540);//2nd    
        eye4.addPoint(870,550);//3rd
        eye4.addPoint(870,526);//4rd
        g.setColor(Color.white);
        g.fillPolygon(eye4);

        Polygon eye5 = new Polygon();
        eye5.addPoint(980,500);//1st
        eye5.addPoint(980,540);//2nd    
        eye5.addPoint(930,550);//3rd
        eye5.addPoint(930,526);//4rd
        g.setColor(Color.white);
        g.fillPolygon(eye5);

        //String
        g.setColor(Color.black);
        g.drawLine(900,600,900,400);//tegak

        /* Spiderman Part End */

        /* Spawn Part Start */ 

        Graphics2D g5 = (Graphics2D)g;
        g.setColor(Color.black);
        g.fillOval(200,350,200,200);
        g.fillArc(200,300,200,325,170,200); 

        //left face decoration
        Polygon leftDeco = new Polygon();
        leftDeco.addPoint(295,465);//1st
        leftDeco.addPoint(280,450);//2nd    
        leftDeco.addPoint(283,465);//3rd
        leftDeco.addPoint(281,471);//4th
        leftDeco.addPoint(240,460);//5th
        leftDeco.addPoint(230,430);//6th
        leftDeco.addPoint(235,410);//7th
        leftDeco.addPoint(247,385);//8th
        leftDeco.addPoint(235,395);//9th
        leftDeco.addPoint(210,430);//10th
        leftDeco.addPoint(225,470);//11th
        leftDeco.addPoint(210,485);//12th
        leftDeco.addPoint(245,510);//13th
        leftDeco.addPoint(220,500);//14th
        leftDeco.addPoint(219,510);//15th
        leftDeco.addPoint(216,515);//16th
        leftDeco.addPoint(260,580);//17th
        leftDeco.addPoint(240,525);//18th
        leftDeco.addPoint(280,500);//19th
        leftDeco.addPoint(275,510);//20th
        leftDeco.addPoint(295,490);//21th
        leftDeco.addPoint(290,485);//22th
        g.setColor(Color.white);
        g.fillPolygon(leftDeco);           

        //right face decoration 
        Polygon rightDeco = new Polygon();
        rightDeco.addPoint(305,465);//1st
        rightDeco.addPoint(320,450);//2nd    
        rightDeco.addPoint(317,465);//3rd
        rightDeco.addPoint(319,471);//4th
        rightDeco.addPoint(360,460);//5th
        rightDeco.addPoint(370,430);//6th
        rightDeco.addPoint(365,410);//7th
        rightDeco.addPoint(357,385);//8th
        rightDeco.addPoint(365,395);//9th
        rightDeco.addPoint(390,430);//10th
        rightDeco.addPoint(375,470);//11th
        rightDeco.addPoint(390,485);//12th
        rightDeco.addPoint(355,510);//13th
        rightDeco.addPoint(380,500);//14th
        rightDeco.addPoint(381,510);//15th
        rightDeco.addPoint(384,515);//16th
        rightDeco.addPoint(340,580);//17th
        rightDeco.addPoint(360,525);//18th
        rightDeco.addPoint(320,500);//19th
        rightDeco.addPoint(325,510);//20th
        rightDeco.addPoint(305,490);//21th
        rightDeco.addPoint(310,485);//22th
        g.setColor(Color.white);
        g.fillPolygon(rightDeco); 

        //left eye
        Polygon leftEye = new Polygon(); 
        leftEye.addPoint(321,487); 
        leftEye.addPoint(367,468); 
        leftEye.addPoint(363,472); 
        leftEye.addPoint(361,485); 
        leftEye.addPoint(359,487); 
        g.setColor(Color.green); 
        g.fillPolygon(leftEye); 

        //right eye
        Polygon rightEye = new Polygon(); 
        rightEye.addPoint(279,487); 
        rightEye.addPoint(233,468); 
        rightEye.addPoint(237,472); 
        rightEye.addPoint(239,485); 
        rightEye.addPoint(241,487); 
        g.setColor(Color.green); 
        g.fillPolygon(rightEye); 

    }
  }
}

Here is the result of the code when ran: So I have an image in the background and also polygons on the front

This is the code after I've added the rain effect:

public class Drawing extends JFrame
{
      public static void main(String []args)
      {
            Drawing b = new Drawing();
      }

    public Drawing()
    {
      final RPanel rPanel=new RPanel();
      //JFrame frame = new JFrame("Poster");
      setSize(1180,750);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      getContentPane().add(new MyComponent());
      add(rPanel); 
      setVisible(true);

      //background
      setLayout(new BorderLayout());
      JLabel background=new JLabel(new ImageIcon("J:\\1. Current Works\\CSC533\\2. project 2.0\\bg.jpg"));
      add(background);
      background.setLayout(new FlowLayout());

      //rain 
      addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                  super.windowClosing(e);
                  rPanel.stop();
                  System.exit(0);
            }
      });
    }



    public class MyComponent extends JComponent
    {
        public void paint(Graphics g)
        {            
            /*Iron Man Part Start*/

            //Iron Man
            Graphics2D g2 = (Graphics2D)g;
            g.setColor(Color.red);//face
            g.fillOval(200,60,190,200);

            //MaskKiri
            Polygon mask1 = new Polygon();
            mask1.addPoint(230,85);//1st
            mask1.addPoint(230,200);//2nd    
            mask1.addPoint(260,255);//3rd
            mask1.addPoint(290,255);//4rd
            mask1.addPoint(290,100);//5rd
            mask1.addPoint(250,100);//6rd
            mask1.addPoint(250,85);//7rd
            g.setColor(Color.yellow);
            g.fillPolygon(mask1);

            //MaskKanan
            Polygon mask2 = new Polygon();
            mask2.addPoint(280,100);//1st
            mask2.addPoint(280,255);//2nd    
            mask2.addPoint(330,255);//3rd
            mask2.addPoint(360,200);//4rd
            mask2.addPoint(360,85);//5rd
            mask2.addPoint(340,85);//6rd
            mask2.addPoint(340,100);//7rd
            g.setColor(Color.yellow);
            g.fillPolygon(mask2);

            //Iron Man Left Eye
            g.setColor(Color.red);
            g.fillRect(240,150,40,10);

            //Iron Man Right Eye
            g.setColor(Color.red);
            g.fillRect(310,150,40,10);

            //Iron Man Fucking Mouth
            g.setColor(Color.red);
            g.fillRect(255,220,80,10);

            /*Iron Man Part End*/

            /*Flash Part Start*/

            //Flash code
            Graphics2D g3 = (Graphics2D)g;
            g.setColor(Color.red);//flash
            g.fillOval(800,50,200,200);

            //Flash eye (lebar,bawah)
            Polygon eye2 = new Polygon();
            eye2.addPoint(830,100);//1st
            eye2.addPoint(830,140);//2nd    
            eye2.addPoint(870,150);//3rd
            eye2.addPoint(870,126);//4rd
            g.setColor(Color.white);
            g.fillPolygon(eye2);

            //Flash eye (lebar,bawah)
            Polygon eye3 = new Polygon();
            eye3.addPoint(970,100);//1st
            eye3.addPoint(970,140);//2nd    
            eye3.addPoint(930,150);//3rd
            eye3.addPoint(930,126);//4rd
            g.setColor(Color.white);
            g.fillPolygon(eye3);

            //Flash mouth (lebar,bawah)
            Polygon mouth = new Polygon();
            mouth.addPoint(830,160);//1st
            mouth.addPoint(830,210);//2nd    
            mouth.addPoint(900,230);//3rd
            mouth.addPoint(900,186);//4rd
            g.setColor(Color.white);
            g.fillPolygon(mouth);

            //Flash mouth (lebar,bawah)
            Polygon mouth2 = new Polygon();
            mouth2.addPoint(970,160);//1st
            mouth2.addPoint(970,210);//2nd    
            mouth2.addPoint(900,230);//3rd
            mouth2.addPoint(900,186);//4rd
            g.setColor(Color.white);
            g.fillPolygon(mouth2);

            //Flash ear kanan (lebar,bawah)
            Polygon ear = new Polygon();
            ear.addPoint(1050,85);//1st    
            ear.addPoint(995,180);//2nd
            ear.addPoint(995,136);//3rd
            g.setColor(Color.yellow);
            g.fillPolygon(ear);

            //Flash ear2 kiri (lebar,bawah)
            Polygon ear2 = new Polygon();
            ear2.addPoint(770,85);//1st
            ear2.addPoint(805,180);//2nd
            ear2.addPoint(805,136);//3rd
            g.setColor(Color.yellow);
            g.fillPolygon(ear2);

            /* Flash Part End */

            /*Spiderman Part Start*/

            Graphics2D g4 = (Graphics2D)g;
            g.setColor(Color.red);
            g.fillOval(800,400,200,200);

            Polygon eye4 = new Polygon();
            eye4.addPoint(820,500);//1st
            eye4.addPoint(820,540);//2nd    
            eye4.addPoint(870,550);//3rd
            eye4.addPoint(870,526);//4rd
            g.setColor(Color.white);
            g.fillPolygon(eye4);

            Polygon eye5 = new Polygon();
            eye5.addPoint(980,500);//1st
            eye5.addPoint(980,540);//2nd    
            eye5.addPoint(930,550);//3rd
            eye5.addPoint(930,526);//4rd
            g.setColor(Color.white);
            g.fillPolygon(eye5);

            //String
            g.setColor(Color.black);
            g.drawLine(900,600,900,400);//tegak

            /* Spiderman Part End */

            /* Spawn Part Start */ 

            Graphics2D g5 = (Graphics2D)g;
            g.setColor(Color.black);
            g.fillOval(200,350,200,200);
            g.fillArc(200,300,200,325,170,200); 

            //left face decoration
            Polygon leftDeco = new Polygon();
            leftDeco.addPoint(295,465);//1st
            leftDeco.addPoint(280,450);//2nd    
            leftDeco.addPoint(283,465);//3rd
            leftDeco.addPoint(281,471);//4th
            leftDeco.addPoint(240,460);//5th
            leftDeco.addPoint(230,430);//6th
            leftDeco.addPoint(235,410);//7th
            leftDeco.addPoint(247,385);//8th
            leftDeco.addPoint(235,395);//9th
            leftDeco.addPoint(210,430);//10th
            leftDeco.addPoint(225,470);//11th
            leftDeco.addPoint(210,485);//12th
            leftDeco.addPoint(245,510);//13th
            leftDeco.addPoint(220,500);//14th
            leftDeco.addPoint(219,510);//15th
            leftDeco.addPoint(216,515);//16th
            leftDeco.addPoint(260,580);//17th
            leftDeco.addPoint(240,525);//18th
            leftDeco.addPoint(280,500);//19th
            leftDeco.addPoint(275,510);//20th
            leftDeco.addPoint(295,490);//21th
            leftDeco.addPoint(290,485);//22th
            g.setColor(Color.white);
            g.fillPolygon(leftDeco);           

            //right face decoration 
            Polygon rightDeco = new Polygon();
            rightDeco.addPoint(305,465);//1st
            rightDeco.addPoint(320,450);//2nd    
            rightDeco.addPoint(317,465);//3rd
            rightDeco.addPoint(319,471);//4th
            rightDeco.addPoint(360,460);//5th
            rightDeco.addPoint(370,430);//6th
            rightDeco.addPoint(365,410);//7th
            rightDeco.addPoint(357,385);//8th
            rightDeco.addPoint(365,395);//9th
            rightDeco.addPoint(390,430);//10th
            rightDeco.addPoint(375,470);//11th
            rightDeco.addPoint(390,485);//12th
            rightDeco.addPoint(355,510);//13th
            rightDeco.addPoint(380,500);//14th
            rightDeco.addPoint(381,510);//15th
            rightDeco.addPoint(384,515);//16th
            rightDeco.addPoint(340,580);//17th
            rightDeco.addPoint(360,525);//18th
            rightDeco.addPoint(320,500);//19th
            rightDeco.addPoint(325,510);//20th
            rightDeco.addPoint(305,490);//21th
            rightDeco.addPoint(310,485);//22th
            g.setColor(Color.white);
            g.fillPolygon(rightDeco); 

            //left eye
            Polygon leftEye = new Polygon(); 
            leftEye.addPoint(321,487); 
            leftEye.addPoint(367,468); 
            leftEye.addPoint(363,472); 
            leftEye.addPoint(361,485); 
            leftEye.addPoint(359,487); 
            g.setColor(Color.green); 
            g.fillPolygon(leftEye); 

            //right eye
            Polygon rightEye = new Polygon(); 
            rightEye.addPoint(279,487); 
            rightEye.addPoint(233,468); 
            rightEye.addPoint(237,472); 
            rightEye.addPoint(239,485); 
            rightEye.addPoint(241,487); 
            g.setColor(Color.green); 
            g.fillPolygon(rightEye); 

        }
      }
}

class RPanel extends JPanel {
//*********SETTINGS****************************
//private float mWind = 2.05f;
//private float mGravity = 9.8f;
private float mWind = 0.55f;
private float mGravity = 1.4f;
private double mRainChance = 0.99; // from 0 to 1

private int mRepaintTimeMS = 16;
private float mRainWidth=1;
//private double mDdropInitialVelocity = 20;
private double mDdropInitialVelocity = 10;
private double mDropDiam = 2;
private Color mColor=new Color(255, 100, 50);
//*********************************************

private ArrayList<Rain> rainV;
private ArrayList<Drop> dropV;
private UpdateThread mUpdateThread;

public RPanel() {
    rainV = new ArrayList<Rain>();
    dropV = new ArrayList<Drop>();

    mUpdateThread=new UpdateThread();
    mUpdateThread.start();
}

public void stop() {
    mUpdateThread.stopped=true;
}

public int getHeight() {
    return this.getSize().height;
}

public int getWidth() {
    return this.getSize().width;
}

private class UpdateThread extends Thread {
    public volatile boolean stopped=false;
    @Override
    public void run() {
        while (!stopped) {
            RPanel.this.repaint();
            try {
                Thread.sleep(mRepaintTimeMS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(new BasicStroke(mRainWidth));
    g2.setColor(mColor);

    //DRAW DROPS
    Iterator<Drop> iterator2 = dropV.iterator();
    while (iterator2.hasNext()) {
        Drop drop = iterator2.next();
        drop.update();
        drop.draw(g2);

        if (drop.y >= getHeight()) {
            iterator2.remove();
        }
    }

    //DRAW RAIN
    Iterator<Rain> iterator = rainV.iterator();
    while (iterator.hasNext()) {
        Rain rain = iterator.next();
        rain.update();
        rain.draw(g2);

        if (rain.y >= getHeight()) {
            //create new drops (2-8)
            long dropCount = 1 + Math.round(Math.random() * 4);
            for (int i = 0; i < dropCount; i++) {
                dropV.add(new Drop(rain.x, getHeight()));
            }
            iterator.remove();

        }
    }

    //CREATE NEW RAIN
    if (Math.random() < mRainChance) {
        rainV.add(new Rain());
    } 
}

//*****************************************
class Rain {
    float x;
    float y;
    float prevX;
    float prevY;

    public Rain() {
        Random r = new Random();
        x = r.nextInt(getWidth());
        y = 0;
    }

    public void update() {
        prevX = x;
        prevY = y;

        x += mWind;
        y += mGravity;
    }

    public void draw(Graphics2D g2) {
        Line2D line = new Line2D.Double(x, y, prevX, prevY);
        g2.draw(line);
    }
}

//*****************************************
private class Drop {

    double x0;  
    double y0;  
    double v0; //initial velocity
    double t;  //time
    double angle;
    double x;
    double y;

    public Drop(double x0, double y0) {
        super();
        this.x0 = x0;
        this.y0 = y0;

        v0 = mDdropInitialVelocity;
        angle = Math.toRadians(Math.round(Math.random() * 180)); //from 0 - 180 degrees
    }

    private void update() {
        // double g=10;
        t += mRepaintTimeMS / 100f;
        x = x0 + v0 * t * Math.cos(angle);
        y = y0 - (v0 * t * Math.sin(angle) - mGravity * t * t / 2);
    }

    public void draw(Graphics2D g2) {
        Ellipse2D.Double circle = new Ellipse2D.Double(x, y, mDropDiam, mDropDiam);
        g2.fill(circle);
    }
}
}

The output I got form this:

After extending the frame, you could see the background that was covered by the rain's panel

The problem that I am having after adding the rain effect is that the panel that I call from the rain covers my background. I implemented the rain code directly and didn't understand much. I'm pretty sure I have to change something somewhere inside there.

The question is how can I make the rain appear with the picture as the background. The components/superheroes could be behind the rain or front of it, whichever is easy.

I hope I've prepared enough even though it is not precise because I don't know the problem specifically. Thank you very much!

2

There are 2 answers

6
teppic On BEST ANSWER

Firstly, make your rain panel extend JComponent. JPanel has an opaque background which will obscure lower components.

class RPanel extends JComponent {

Then add your rain panel to the Glass Pane of your frame:

public Drawing() {
    final RPanel rPanel = new RPanel();
    setSize(1180, 750);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new MyComponent());

    JPanel glassPane = (JPanel) getGlassPane();
    glassPane.setLayout(new BorderLayout());
    glassPane.add(rPanel, BorderLayout.CENTER);
    glassPane.setVisible(true); 

    setVisible(true);

    // ...
0
camickr On

First of all:

  1. When doing custom painting you should be overriding paintComponent(), not paint() and then invoke super.paintComponent(...) as the first statement.

  2. All components should be added to the frame BEFORE invoking setVisible(true).

Then you need to understand the structure of a JFrame. The JFrame has a parent/child relationship. The content pane of the frame is a child. Then the content pane can have child panels/components. And then these panels can also contain more panels/components.

Read the Swing tutorial on Using Top Level Containers to better understand this structure.

  getContentPane().add(new MyComponent());
  add(rPanel); 

The above code is basically the same. That is both the component and the panel are added to the content pane. The getContentPane() is not necessary.

setLayout(new BorderLayout());

Don't change the layout after you add the components. Basically this will cause the two components added previously to be replaced by the next component you add to the frame.

So what you want is a structure like this:

- frame
    - content pane
        - background
            - painting

So the basic logic is:

JLabel background = new JLabel( new BorderLayout() );
background.setLayout( ... ):
add( background );

JComponent painting = new MyComponent();
background.add( painting );

setVisible( true );

Keep a link to the Swing tutorial handy. For example it also has sections on:

  1. Custom painting - it shows you how to override the getPreferredSize() method.
  2. Layout Managers - it explains why the preferred size is important for some layout managers.