Advertisement
Guest User

StackTest.java

a guest
Mar 2nd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. import java.net.URL;
  7. import java.util.Random;
  8. import java.awt.image.BufferedImage;
  9.  
  10.  
  11. public class StackTest {
  12.     private static Position p;
  13.     public static void main(String[] v){
  14.         Field f = new Field(2, 100);
  15.         p = f.getRandomFieldPosition();
  16.        
  17.         JPanel panel = new FieldPanel(p);
  18.         JPanel player = new LeftPlayerPanel();// add player later
  19.         JFrame f1 = new TestFrame("TESTFRAME");
  20.  
  21.         panel.setPreferredSize(new Dimension(326,309));
  22.         player.setPreferredSize(new Dimension(135,309));
  23.  
  24.         JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,player,panel);
  25.  
  26.         JPanel content = new JPanel();
  27.         content.setLayout(new BorderLayout());
  28.         content.add(splitpane,BorderLayout.CENTER);
  29.  
  30.         f1.setContentPane(content);
  31.         f1.setPreferredSize(new Dimension(326+135,309));
  32.         f1.pack();
  33.         f1.show();
  34.     }
  35. }
  36.  
  37. class LeftPlayerPanel extends JPanel{
  38.     private JPanel PlayerPicturePanel = new ImagePanel("http://i.stack.imgur.com/sjfvC.jpg",Color.GRAY);
  39.     public LeftPlayerPanel(){
  40.         this.add(PlayerPicturePanel);
  41.         //TODO!!:Display Player Stats
  42.     }
  43.  
  44. }
  45.  
  46. class ImagePanel extends JPanel{
  47.     private BufferedImage image;
  48.     public ImagePanel(String path,Color background){ //TODO!!:set background color
  49.         try{
  50.             URL url = new URL(path);
  51.             image = ImageIO.read(url);
  52.         }catch(IOException ex){
  53.             System.out.println("Image Not Found");
  54.         }
  55.     }
  56.     @Override
  57.     protected void paintComponent(Graphics g){
  58.         super.paintComponents(g);
  59.         g.drawImage(image,0,0,null);
  60.     }
  61. }
  62.  
  63.  
  64. @SuppressWarnings("serial")
  65. class TestFrame extends JFrame {
  66.     public TestFrame(){
  67.         super();
  68.         setExtendedState(JFrame.MAXIMIZED_BOTH);
  69.         setVisible(true);
  70.     }
  71.     public TestFrame(String titolo){
  72.         super(titolo);
  73.         setExtendedState(JFrame.MAXIMIZED_BOTH);
  74.         setVisible(true);
  75.     }
  76. }
  77.  
  78.  
  79.  
  80. class Field {
  81.     private int x;
  82.     private int y;
  83.     private int factor;
  84.     private int[][] field;
  85.  
  86.     public Field(int lvldif, int spread){
  87.         this.x=10;
  88.         this.y=10;
  89.         field = new int[x][y];
  90.         this.factor = initfield(lvldif,spread);
  91.     }
  92.  
  93.     private int initfield(int lvldif, int spread) {
  94.         int factor=(int)((lvldif*0.75)*(spread*1.3333)+1);
  95.         for(int i=0;i<this.x;i++)
  96.             for(int j=0;j<this.y;j++){
  97.                 field[i][j]=generateFieldValue(factor);
  98.             }
  99.         return factor;
  100.     }
  101.  
  102.     private int generateFieldValue(int factor) {
  103.         Random random = new Random();
  104.         return random.nextInt(10*factor)+(factor/2)+1;
  105.     }
  106.    
  107.     public void printfield(){
  108.         for(int i=0;i<this.x;i++){
  109.             for(int j=0;j<this.y;j++)
  110.                 System.out.print(field[i][j]+" ");
  111.             System.out.println(" ");
  112.         }
  113.     }
  114.    
  115.     public Position getRandomFieldPosition(){
  116.         float[][] relativeField = new float[this.x][this.y];
  117.         double sum=0.0;
  118.         Random random = new Random();
  119.         float limit = random.nextFloat();
  120.         double current = (float) 0.0;
  121.         for(int i=0;i<this.x;i++)
  122.             for(int j=0;j<this.y;j++)
  123.                 sum+=field[i][j];
  124.         for(int i=0;i<this.x;i++)
  125.             for(int j=0;j<this.y;j++){
  126.                 relativeField[i][j]=(float) (field[i][j]/sum);
  127.                 current+=relativeField[i][j];
  128.                 if(current>limit)
  129.                     return new Position(i,j);
  130.             }
  131.         return null;
  132.     }
  133. }
  134.  
  135. class Position {
  136.     private int x;
  137.     private int y;
  138.  
  139.     public Position(int x,int y){
  140.         this.x=x;
  141.         this.y=y;
  142.     }
  143. }
  144.  
  145. class FieldPanel extends JPanel{
  146.     private Field f;
  147.     private JPanel imagePanels[][] = new JPanel[10][10];
  148.     public FieldPanel(Position p){
  149.         this.setLayout(new GridLayout(10,10));
  150.         LoadSquare(p);
  151.        
  152.  
  153.     }
  154.     private void LoadSquare(Position p){
  155.         for(int i=0;i<10;i++)
  156.             for(int j=0;j<10;j++){
  157.                     imagePanels[i][j] = new ImagePanel("http://i.stack.imgur.com/W3RMa.jpg",Color.WHITE);
  158.                     this.add(imagePanels[i][j]);
  159.                 }
  160.            
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement