Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import java.net.URL;
- import java.util.Random;
- import java.awt.image.BufferedImage;
- public class StackTest {
- private static Position p;
- public static void main(String[] v){
- Field f = new Field(2, 100);
- p = f.getRandomFieldPosition();
- JPanel panel = new FieldPanel(p);
- JPanel player = new LeftPlayerPanel();// add player later
- JFrame f1 = new TestFrame("TESTFRAME");
- panel.setPreferredSize(new Dimension(326,309));
- player.setPreferredSize(new Dimension(135,309));
- JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,player,panel);
- JPanel content = new JPanel();
- content.setLayout(new BorderLayout());
- content.add(splitpane,BorderLayout.CENTER);
- f1.setContentPane(content);
- f1.setPreferredSize(new Dimension(326+135,309));
- f1.pack();
- f1.show();
- }
- }
- class LeftPlayerPanel extends JPanel{
- private JPanel PlayerPicturePanel = new ImagePanel("http://i.stack.imgur.com/sjfvC.jpg",Color.GRAY);
- public LeftPlayerPanel(){
- this.add(PlayerPicturePanel);
- //TODO!!:Display Player Stats
- }
- }
- class ImagePanel extends JPanel{
- private BufferedImage image;
- public ImagePanel(String path,Color background){ //TODO!!:set background color
- try{
- URL url = new URL(path);
- image = ImageIO.read(url);
- }catch(IOException ex){
- System.out.println("Image Not Found");
- }
- }
- @Override
- protected void paintComponent(Graphics g){
- super.paintComponents(g);
- g.drawImage(image,0,0,null);
- }
- }
- @SuppressWarnings("serial")
- class TestFrame extends JFrame {
- public TestFrame(){
- super();
- setExtendedState(JFrame.MAXIMIZED_BOTH);
- setVisible(true);
- }
- public TestFrame(String titolo){
- super(titolo);
- setExtendedState(JFrame.MAXIMIZED_BOTH);
- setVisible(true);
- }
- }
- class Field {
- private int x;
- private int y;
- private int factor;
- private int[][] field;
- public Field(int lvldif, int spread){
- this.x=10;
- this.y=10;
- field = new int[x][y];
- this.factor = initfield(lvldif,spread);
- }
- private int initfield(int lvldif, int spread) {
- int factor=(int)((lvldif*0.75)*(spread*1.3333)+1);
- for(int i=0;i<this.x;i++)
- for(int j=0;j<this.y;j++){
- field[i][j]=generateFieldValue(factor);
- }
- return factor;
- }
- private int generateFieldValue(int factor) {
- Random random = new Random();
- return random.nextInt(10*factor)+(factor/2)+1;
- }
- public void printfield(){
- for(int i=0;i<this.x;i++){
- for(int j=0;j<this.y;j++)
- System.out.print(field[i][j]+" ");
- System.out.println(" ");
- }
- }
- public Position getRandomFieldPosition(){
- float[][] relativeField = new float[this.x][this.y];
- double sum=0.0;
- Random random = new Random();
- float limit = random.nextFloat();
- double current = (float) 0.0;
- for(int i=0;i<this.x;i++)
- for(int j=0;j<this.y;j++)
- sum+=field[i][j];
- for(int i=0;i<this.x;i++)
- for(int j=0;j<this.y;j++){
- relativeField[i][j]=(float) (field[i][j]/sum);
- current+=relativeField[i][j];
- if(current>limit)
- return new Position(i,j);
- }
- return null;
- }
- }
- class Position {
- private int x;
- private int y;
- public Position(int x,int y){
- this.x=x;
- this.y=y;
- }
- }
- class FieldPanel extends JPanel{
- private Field f;
- private JPanel imagePanels[][] = new JPanel[10][10];
- public FieldPanel(Position p){
- this.setLayout(new GridLayout(10,10));
- LoadSquare(p);
- }
- private void LoadSquare(Position p){
- for(int i=0;i<10;i++)
- for(int j=0;j<10;j++){
- imagePanels[i][j] = new ImagePanel("http://i.stack.imgur.com/W3RMa.jpg",Color.WHITE);
- this.add(imagePanels[i][j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement