samedi 29 novembre 2014

Getting paintComponent elements of a JPanel to work with another JPanel inside of it


Vote count:

0




I have a JPanel with a simple animated snow particle effect inside of a JFrame, and it works from on its own. But when I try and add another JPanel to it, it makes the effect of the snow stop. Can anyone help me with this? Is it a problem with layering, or something? Or is it an issue with my Layout Managers?


My Code: package christmasfinal;



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

public class ChristmasFinal extends JApplet {

public static void main(String[] args) {
JFrame frame = new JFrame();
TreePanel treePanel = new TreePanel();
frame.setSize(550, 420);
SnowBackgroundPanel snowPanel = new SnowBackgroundPanel(frame.getWidth(), frame.getHeight());
treePanel.setSize(200, 320);
snowPanel.setSize(frame.getWidth(), frame.getHeight());
snowPanel.setLayout(new BorderLayout());
frame.setLayout(new BorderLayout());
frame.add(snowPanel, BorderLayout.CENTER);
snowPanel.add(treePanel, BorderLayout.CENTER);
System.out.println(treePanel.getWidth() + " " + treePanel.getHeight());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Christmas Final");
}

}


package christmasfinal;

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

public class TreePanel extends JPanel{

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int[] treeX = {getWidth()/2, getWidth()/2-20, getWidth()/2-60, getWidth()/2-20,
getWidth()/2-40, getWidth()/2-80, getWidth()/2-20, getWidth()/2-60, getWidth()/2-100,
getWidth()/2+100,getWidth()/2+60, getWidth()/2+20, getWidth()/2+80,
getWidth()/2+40, getWidth()/2+20, getWidth()/2+60, getWidth()/2+20, getWidth()/2};

int[] treeY = {getHeight()/2-120, getHeight()/2-80, getHeight()/2-40, getHeight()/2-40,
getHeight()/2-20, getHeight()/2, getHeight()/2, getHeight()/2+40, getHeight()/2+60,
getHeight()/2+60, getHeight()/2+40, getHeight()/2, getHeight()/2, getHeight()/2-20,
getHeight()/2-40, getHeight()/2-40, getHeight()/2-80, getHeight()/2-120};

g.setColor(Color.GREEN);
g.fillPolygon(treeX, treeY, treeX.length);
g.setColor(new Color(102, 51, 0));
g.fillRect(getWidth()/2-20, getHeight()/2+60, 40, 40);
}
}

package christmasfinal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.math.*;

public class SnowBackgroundPanel extends JPanel{
Random rand = new Random();

//Create ArrayList for SnowParticles
ArrayList<SnowParticle> snow = new ArrayList();

//Create animation timer
Timer timer = new Timer(7, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});



public SnowBackgroundPanel(int width, int height){
setSize(width, height);
System.out.println(getHeight() + " " + getWidth());
for(int i=0; i<50; i++){
snow.add(new SnowParticle());
snow.get(i).x = rand.nextInt(getWidth()+1);
snow.get(i).y = 0-rand.nextInt(getHeight()+1);
}
timer.start();

}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);

//Set, paint, and move snow particles
for(int i=0; i<50; i++){
g.fillOval(snow.get(i).x, snow.get(i).y, 10, 10);
snow.get(i).y += 1;
if(snow.get(i).y > getHeight()){
snow.get(i).x = rand.nextInt(getWidth()+1);
snow.get(i).y = 0-rand.nextInt(getHeight()+1);
}
}
}
}


asked 53 secs ago







Getting paintComponent elements of a JPanel to work with another JPanel inside of it

Aucun commentaire:

Enregistrer un commentaire