Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: xxMrPHDxx (posted 2017-12-31 17:15:51, viewed 734 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.mrphd.board.Tile;

public class Sudoku extends JPanel implements KeyListener {
   
   private static final long serialVersionUID = 1L;

   public static final int WIDTH = 640,HEIGHT=640;
   
   private final JFrame frame = new JFrame("Sudoku");
   
   private final List<TilePanel> tiles = new ArrayList<>();
   
   private boolean running = false;
   
   private Tile selectedTile;
   
   public Sudoku() {
      setPreferredSize(new Dimension(WIDTH,HEIGHT));
      setLayout(new GridLayout(9,9));

      frame.addKeyListener(this);
      
      IntStream.range(0, 81).forEach(i -> {
         final int row = (int)(i / 9);
         final int col = i % 9;
         final TilePanel tile = new TilePanel(row,col);
         add(tile);
      });
      
      frame.add(this);
      frame.pack();
      frame.setResizable(false);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   
   public void run() {
      running = true;
      while(running) {
         tiles.forEach(tile -> {
            tile.repaint();
         });
      }
   }
   
   public static void main(String[] args) {
      new Sudoku().run();
   }
   
   private class TilePanel extends JPanel implements MouseListener {
      
      private static final long serialVersionUID = 1L;
      
      private final BufferedImage image;
      
      private final Tile tile;
      
      public TilePanel(final int row,final int col) {
         this.tile = new Tile(row,col);
         addMouseListener(this);

         final int width = Sudoku.WIDTH / 9;
         final int height = Sudoku.HEIGHT / 9;
         
         image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
         final Graphics g = image.getGraphics();
         
         g.setColor(Color.BLACK);
         g.fillRect(0, 0, width, height);
         g.setColor(Color.WHITE);
         if((row % 3 == 2 && row < 8) || (col % 3 == 2 && col < 8)) {
            if((row % 3 == 2 && row < 8) && (col % 3 == 2 && col < 8))
               g.fillRect(2, 2, width-4, height-4);
            else if(col % 3 == 2 && col < 8)
               g.fillRect(2, 2, width-4, height-2);
            else
               g.fillRect(2, 2, width-2, height-4);
         } else 
            g.fillRect(2, 2, width-2, height-2);
         
         validate();
      }
      
      public void paint(final Graphics g) {
         g.drawImage(image, 0, 0, null);
         g.setColor(Color.BLACK);
         g.setFont(new Font("Arial",Font.BOLD,30));
         System.out.println(tile.getValue());
         g.drawString(tile.getValue(), image.getWidth() / 2 - 5, image.getHeight() * 2 / 3);
      }
      
      public void mouseClicked(final MouseEvent e) {
         selectedTile = this.tile;
      }

      public void mouseEntered(final MouseEvent e) {}

      public void mouseExited(final MouseEvent e) {}

      public void mousePressed(final MouseEvent e) {}

      public void mouseReleased(final MouseEvent e) {}
      
   }

   public void keyPressed(final KeyEvent e) {      
      //System.out.println("Working!! " + selectedTile);
      
      if(selectedTile != null) //e.getKeyCode() >= 49 && e.getKeyCode() <= 57 && 
         selectedTile.setValue(String.valueOf(e.getKeyChar())); // 1 -> 49 , 9 -> 57 ,BS -> 8
   }

   public void keyReleased(final KeyEvent e) {}

   public void keyTyped(final KeyEvent e) {}
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
ivj94 (584 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (382 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (109 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (703 views)
2018-02-28 16:59:18

buddyBro (92 views)
2018-02-28 16:45:17

xxMrPHDxx (493 views)
2017-12-31 17:17:51

xxMrPHDxx (733 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!