Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: painting to a JPanel in a JFrame

  1. #1
    Parkournerd is offline Member
    Join Date
    Jan 2012
    Posts
    20
    Rep Power
    0

    Default painting to a JPanel in a JFrame

    It's me again. The JPanel displays and the mouselistener works, but I don't know how to get the application to redraw.

    Java Code:
    /*name:  date: Dec 16 2011
      purpose: proChess*/
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Point2D;
    
    import javax.swing.*;
    
    //import JPanel
    import javax.swing.JPanel;
    //import swing
    import java.awt.*;
    import java.awt.event.*;
    //import java2d
    import java.awt.geom.Point2D;
    import java.awt.geom.AffineTransform;
    //import sound
    import java.applet.Applet;
    import java.applet.AudioClip;
    //import IO
    import java.io.File;
    import java.net.MalformedURLException; 
    
    //w = 1 = first player
    /*  java.io.File file = new java.io.File("bark.aiff");
     AudioClip sound = Applet.newAudioClip(file.toURL());
     sound.play();*/
    
    //p2 = green = black
    
    public class proChess extends JFrame implements ActionListener, MouseListener, MouseMotionListener
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 8828135172836094084L;
    	
    	JFrame frameProChess = new JFrame("ProChess");
    		// all items in window
    		Panel panelProChess = new Panel();
    			JLabel iconTitle = new JLabel(createImageIcon("iconLogo.png")); 
    			JPanel panelBoard;
    			Panel panelDisplay = new Panel(new GridLayout(3,1));
    				JLabel labelPlayer1;
    				JLabel labelPlayer2;
    				JLabel labelTurn;
    			JLabel labelPromotionTitle;
    			Panel panelChoice = new Panel(new GridLayout(1,4));
    				JButton buttonChoice1 = new JButton();
    				JButton buttonChoice2 = new JButton();
    				JButton buttonChoice3 = new JButton();
    				JButton buttonChoice4 = new JButton();
    	
    	final JFrame frameHelp = new JFrame("Help");
    	static boolean bolHelp = false;
    	final JFrame frameSetting = new JFrame("Settings");
    	static boolean bolSetting = false;
    
    	// board variables
    	Color colorOne = Color.white;
    	Color colorTwo = Color.black;
    	
    	Color colorPieceOne = Color.blue;
    	Color colorPieceTwo = Color.green;
    	Color colorAlpha = new Color(0,0,0,0);
    	
    	String[][] position = new String[8][8];
    	Boolean[][] positionChosen = new Boolean[8][8];
    	Boolean[][] positionMove = new Boolean[8][8];
    	
    	int turn;
    	int[] time = new int[2];
    	int[] gold = new int[2];
    	int[] score = new int[2];
    	
    	int timeSet = 600;
    
        Graphics bufferGraphics = null; 
        Image offscreen = null; 
        
        Boolean bolChosen = false;
        int curX; 
        int curY;
        
    	public static void main(String[] args) 
    	{
    		@SuppressWarnings("unused")
    		proChess proChess = new proChess();
    	}
    	
    	@SuppressWarnings("deprecation")
    	public proChess()
    	{	
    		
    		// Menu
    		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    		JMenuBar JMenuBar = new JMenuBar();
    		frameProChess.setJMenuBar(JMenuBar);
    			JMenu menuMenu = new JMenu("Menu");
    				JMenuItem itemNewGame = new JMenuItem("New Game");
    				menuMenu.add(itemNewGame);
    				itemNewGame.setActionCommand ("New Game");
    				itemNewGame.addActionListener((ActionListener)this);
    				JMenuItem itemSaveGame = new JMenuItem("Save Game");
    				menuMenu.add(itemSaveGame);
    				itemSaveGame.setActionCommand ("Save Game");
    				itemSaveGame.addActionListener((ActionListener)this);
    				JMenuItem itemLoadGame = new JMenuItem("Load Game");
    				menuMenu.add(itemLoadGame);
    				itemLoadGame.setActionCommand ("Load Game");
    				itemLoadGame.addActionListener((ActionListener)this);
    				JMenuItem itemSetting = new JMenuItem("Settings");
    				itemSetting.setActionCommand ("Settings");
    				itemSetting.addActionListener((ActionListener)this);
    				menuMenu.add(itemSetting);
    				JMenuBar.add(menuMenu);
    			JMenu menuHelp = new JMenu("Help");
    				JMenuItem itemHelp = new JMenuItem("Help");
    				menuHelp.add(itemHelp);
    				itemHelp.setActionCommand ("Help");
    				itemHelp.addActionListener((ActionListener)this);
    				JMenuBar.add(menuHelp);
    		
    		// disable auto layout
    		panelProChess.setLayout(null);
    		
    			/* Not as good on XP
    			 *try 
    			 *{
    			 *	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			 *}catch(Exception e){}
    			 */
    			 
    		iconTitle = new JLabel(createImageIcon("iconLogo.png"));
    		
    		newGame();
    		// canvasBoard.addMouseListener(this);
    		// canvasBoard.addMouseMotionListener(this);
    		
    			panelBoard = new JPanel()
    			{
    				private static final long serialVersionUID = -4529456824050911554L;
    				
    			    public void paintComponent(Graphics g)  
    			    {
    			    	 super.paintComponent(g);
    			    	 offscreen = createImage(480,480); 
    					 bufferGraphics = offscreen.getGraphics();
    			    	 bufferGraphics.clearRect(0,0,768,768); 
    			    	 
    						for(int y = 0; y < 8; y++)
    						{
    								for(int x = 0; x < 8; x++)
    								{
    
    									Graphics2D g2 = (Graphics2D) bufferGraphics;
    									g2.setStroke(new BasicStroke(4));
    									g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    									
    										if(x % 2 == 1 && y % 2 == 1 || x % 2 == 0 && y % 2 == 0)
    										{
    											bufferGraphics.setColor(colorOne);
    											bufferGraphics.fillRect(x*60,y*60,60,60);
    										}
    										else
    										{
    											bufferGraphics.setColor(colorTwo);
    											bufferGraphics.fillRect(x*60,y*60,60,60);
    										}
    										
    										// draw circles
    										if(position[x][y].charAt(0) == 'b')
    										{
    											((Graphics2D) bufferGraphics).setPaint(generateGradient(colorPieceTwo,x,y));
    											bufferGraphics.fillOval(x*60+5,y*60+5,50,50);
    										}
    										else if(position[x][y].charAt(0) == 'w')
    										{
    											((Graphics2D) bufferGraphics).setPaint(generateGradient(colorPieceOne,x,y));
    											bufferGraphics.fillOval(x*60+5,y*60+5,50,50);
    										}
    										
    										bufferGraphics.setColor(Color.black);
    									
    										// draw pawns
    										if(position[x][y].charAt(1) == 'p')
    										{
    												if(position[x][y].charAt(0) == 'b')
    												{
    													bufferGraphics.setColor(Color.white);
    													bufferGraphics.fillRect(x*60+29,y*60+30,4,20);
    													bufferGraphics.setColor(Color.black);
    													bufferGraphics.drawLine(x*60+30,y*60+30,x*60+16,y*60+44);
    													bufferGraphics.drawLine(x*60+31,y*60+30,x*60+44,y*60+44);
    												}
    												else
    												{
    													bufferGraphics.setColor(Color.white);
    													bufferGraphics.fillRect(x*60+29,y*60+10,4,20);
    													bufferGraphics.setColor(Color.black);
    													bufferGraphics.drawLine(x*60+30,y*60+30,x*60+16,y*60+16);
    													bufferGraphics.drawLine(x*60+31,y*60+30,x*60+44,y*60+16);
    												}
    												
    												if(position[x][y].charAt(2) == '1')
    												{
    													bufferGraphics.fillRect(x*60+28,y*60+10,4,40);
    													bufferGraphics.fillRect(x*60+10,y*60+28,40,4);
    												}
    												if(position[x][y].charAt(3) == '1')
    												{
    													bufferGraphics.drawLine(x*60+15,y*60+15,x*60+45,y*60+45);
    													bufferGraphics.drawLine(x*60+45,y*60+15,x*60+15,y*60+45);
    												}
    												if(position[x][y].charAt(4) == '1')
    												{
    													bufferGraphics.fillRect(x*60+28,y*60+20,4,20);
    													bufferGraphics.fillRect(x*60+20,y*60+28,20,4);
    													bufferGraphics.fillRect(x*60+16,y*60+10,4,40);
    													bufferGraphics.fillRect(x*60+40,y*60+10,4,40);
    													bufferGraphics.fillRect(x*60+10,y*60+16,40,4);
    													bufferGraphics.fillRect(x*60+10,y*60+40,40,4);
    												}
    												if(position[x][y].charAt(5) == '1')
    												{
    													bufferGraphics.fillRect(x*60+28,y*60+20,4,20);
    													bufferGraphics.fillRect(x*60+20,y*60+28,20,4);
    													bufferGraphics.drawLine(x*60+22,y*60+22,x*60+38,y*60+38);
    													bufferGraphics.drawLine(x*60+38,y*60+22,x*60+22,y*60+38);
    													
    													/* TOO BIG
    													 * g.drawLine(x*60+20,y*60+20,x*60+40,y*60+40);
    													 * g.drawLine(x*60+40,y*60+20,x*60+20,y*60+40);*/
    												}	
    										}
    										
    										// draw rooks
    										if(position[x][y].charAt(1) == 'r')
    										{
    											bufferGraphics.fillRect(x*60+28,y*60+10,4,40);
    											bufferGraphics.fillRect(x*60+10,y*60+28,40,4);
    										}
    										// draw knights
    										if(position[x][y].charAt(1) == 'k')
    										{
    											bufferGraphics.fillRect(x*60+28,y*60+20,4,20);
    											bufferGraphics.fillRect(x*60+20,y*60+28,20,4);
    											bufferGraphics.fillRect(x*60+16,y*60+10,4,40);
    											bufferGraphics.fillRect(x*60+40,y*60+10,4,40);
    											bufferGraphics.fillRect(x*60+10,y*60+16,40,4);
    											bufferGraphics.fillRect(x*60+10,y*60+40,40,4);
    										}
    										// draw bishops
    										if(position[x][y].charAt(1) == 'b')
    										{
    											bufferGraphics.drawLine(x*60+15,y*60+15,x*60+45,y*60+45);
    											bufferGraphics.drawLine(x*60+45,y*60+15,x*60+15,y*60+45);
    										}
    										// draw queens
    										if(position[x][y].charAt(1) == 'q')
    										{
    											bufferGraphics.fillRect(x*60+28,y*60+10,4,40);
    											bufferGraphics.fillRect(x*60+10,y*60+28,40,4);
    											bufferGraphics.drawLine(x*60+15,y*60+15,x*60+45,y*60+45);
    											bufferGraphics.drawLine(x*60+45,y*60+15,x*60+15,y*60+45);
    										}
    										// draw kings
    										if(position[x][y].charAt(1) == 'w')
    										{
    											bufferGraphics.fillRect(x*60+28,y*60+20,4,20);
    											bufferGraphics.fillRect(x*60+20,y*60+28,20,4);
    											bufferGraphics.drawLine(x*60+22,y*60+22,x*60+38,y*60+38);
    											bufferGraphics.drawLine(x*60+38,y*60+22,x*60+22,y*60+38);
    										}
    										
    										// highlight chosen square
    										if(positionChosen[x][y] == true)
    										{
    											((Graphics2D) bufferGraphics).setPaint(generateRadialGradient(Color.orange,x,y));
    											bufferGraphics.fillOval(x*60-10,y*60-10,90,90);
    										}
    										// highlight available moves
    										if(positionMove[x][y] == true)
    										{
    											((Graphics2D) bufferGraphics).setPaint(generateRadialGradient(Color.red,x,y));
    											bufferGraphics.fillOval(x*60,y*60,60,60);
    										}
    								}		
    						}
    						if(turn==2)
    						{
    							// flip board
    						}
    
    					g.drawImage(offscreen,0,0,this);		
    			    }
    			    public void update(Graphics g) 
    			    { 
    			         paint(g); 
    			    } 
    			};
    		
    			panelBoard.addMouseListener(this);
    			addMouseListener(this);
    			panelBoard.addMouseMotionListener(this);
    			addMouseMotionListener(this);
    			
    		labelPlayer2 = new JLabel("<html><u>Player 2</u><br><br>Time:<br>Gold:<br>Score:</html>");
    		panelDisplay.add(labelPlayer2);			
    		labelPlayer1 = new JLabel("<html><u>Player 1</u><br><br>Time:<br>Gold:<br>Score:</html>");
    		panelDisplay.add(labelPlayer1);
    		labelTurn = new JLabel("It is Player 1's turn.");
    		panelDisplay.add(labelTurn);
    
    		// Segment Three
    		labelPromotionTitle = new JLabel("<html><u>Promotion</u></html>");
    
    		buttonChoice1 = new JButton();
    		panelChoice.add(buttonChoice1);
    		buttonChoice2 = new JButton();
    		panelChoice.add(buttonChoice2);
    		buttonChoice3 = new JButton();
    		panelChoice.add(buttonChoice3);
    		buttonChoice4 = new JButton();
    		panelChoice.add(buttonChoice4);
    
    		panelProChess.add(iconTitle);
    		iconTitle.setBounds(15,15,484,107);
    		panelProChess.add(panelBoard);
    		panelBoard.setBounds(15,122,480,480);
    		panelProChess.add(panelDisplay);
    		panelDisplay.setBounds(514,0,254,768);
    		panelProChess.add(labelPromotionTitle);
    		labelPromotionTitle.setBounds(225,621,63,18);
    		panelProChess.add(panelChoice);
    		panelChoice.setBounds(135,653,242,60);
    
    		frameProChess.add(panelProChess);
    		
    		frameProChess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frameProChess.pack();
    		frameProChess.resize(768,768);
    		frameProChess.setLocationRelativeTo(null);
    		frameProChess.setResizable(false);
    		frameProChess.setVisible(true);
    	}
    	public void newGame()
    	{
    			// reset board
    			for(int y = 0; y < 8; y++)
    			{
    					for(int x = 0; x < 8; x++)
    					{
    						position[x][y] = "      ";
    						positionChosen[x][y] = false;
    						positionMove[x][y] = false;
    					}
    			}
    			
    		// positions pawns for black
    		int y = 1;
    		
    			for(int x = 0; x < 8; x++)
    			{
    				position[x][y] = "bp0000";
    			}
    			
    		// positions pawns for white
    		y = 6;
    			
    			for(int x = 0; x < 8; x++)
    			{
    				position[x][y] = "wp0000";
    			}
    			
    		// positions pieces for black
    		position[0][0] = "br";
    		position[1][0] = "bk";
    		position[2][0] = "bb";
    		position[3][0] = "bq";
    		position[4][0] = "bw";
    		position[5][0] = "bb";
    		position[6][0] = "bk";
    		position[7][0] = "br";
    		
    		// positions pieces for white	
    		position[0][7] = "wr";
    		position[1][7] = "wk";
    		position[2][7] = "wb";
    		position[3][7] = "wq";
    		position[4][7] = "ww";
    		position[5][7] = "wb";
    		position[6][7] = "wk";
    		position[7][7] = "wr";
    		
    		position[4][4] = "wb";
    		position[6][4] = "wr";
    		
    		turn = 2;
    		
    			for(int x = 0; x < 2; x++)
    			{
    				time[x] = timeSet;
    				gold[x] = 0;
    				score[x] = 0;
    			}
    	}
    
        public GradientPaint generateGradient(Color c, int x,int y)
        {
        	GradientPaint gradient = new GradientPaint(x*60+5,y*60+5,c,x*60+30,y*60+30,Color.gray,true);
    		return gradient;
        }
        public RadialGradientPaint generateRadialGradient(Color c, int x,int y)
        {
        	Point2D center = new Point2D.Float(x*60+30, y*60+30);
            float radius = 30;
            float[] dist = {0.0f, 1f};
            Color[] colors = {c, colorAlpha};
        	RadialGradientPaint gradient = new RadialGradientPaint(center, radius, dist, colors);
    		return gradient;
        }
    	public void generateHighlightMoves(int x1, int y1, String p)
    	{
    		positionChosen[x1][y1] = true;
    		
    			for(int y = 0; y < 8; y++)
    			{
    					for(int x = 0; x < 8; x++)
    					{
    						positionMove[x][y] = false;			
    					}
    			}
    			
    			if(position[x1][y1].charAt(1) == 'p')
    			{
    					if(position[x1][y1].charAt(0) == 'b' && y1+1<8)
    					{
    							if(position[x1][y1+1].equals("      "))
    							{
    								positionMove[x1][y1+1] = true;
    							}
    							if(y1+2<8)
    							{
    									if(position[x1][y1+2].equals("      ") && y1==1)
    									{
    										positionMove[x1][y1+2] = true;	
    									}
    							}
    							// diagonals
    							if(y1+1<8 && x1+1<8 && !position[x1+1][y1+1].equals("      "))
    							{
    								positionMove[x1+1][y1+1] = true;
    							}
    							if(y1+1<8 && x1-1>-1 && !position[x1-1][y1+1].equals("      "))
    							{
    								positionMove[x1-1][y1+1] = true;
    							}
    					}
    					if(position[x1][y1].charAt(0) == 'w' && y1-1>-1)
    					{
    							if(position[x1][y1-1].equals("      "))
    							{
    								positionMove[x1][y1-1] = true;
    							}
    							// 2
    							if(y1-2>-1)
    							{
    									if(position[x1][y1-2].equals("      ") && y1==6)
    									{
    										positionMove[x1][y1-2] = true;	
    									}				
    							}
    							// diagonals
    							if(y1+1>-1 && x1+1<8 && !position[x1+1][y1-1].equals("      "))
    							{
    								positionMove[x1+1][y1-1] = true;
    							}
    							if(y1+1>-1 && x1-1>-1 && !position[x1-1][y1-1].equals("      "))
    							{
    								positionMove[x1-1][y1-1] = true;
    							}
    					}
    			}
    			if(position[x1][y1].charAt(1) == 'r' || position[x1][y1].charAt(1) == 'p' && position[x1][y1].charAt(2) == '1' || position[x1][y1].charAt(1) == 'q')
    			{
    				// right
    				int rightLim[] = {x1,x1};
    				
    					if(x1+1<8)
    					{
    						rightLim[0] = x1+1;
    					}
    				
    				rightLim[1] = rightLim[0];
    					
    					while(rightLim[1]<7 && position[rightLim[1]][y1].equals("      "))
    					{
    						rightLim[1]++;
    					}
    					for(int num = rightLim[0];num<rightLim[1]+1;num++)
    					{
    						positionMove[num][y1] = true;
    					}
    					
    				// left
    				int leftLim[] = {x1,x1};
    					
    					if(x1-1>-1)
    					{
    						leftLim[0] = x1-1;
    					}
    				
    				leftLim[1] = leftLim[0];
    					
    					while(leftLim[1]>0 && position[leftLim[1]][y1].equals("      "))
    					{
    						leftLim[1]--;
    					}
    					for(int num = leftLim[0];num>leftLim[1]-1;num--)
    					{
    						positionMove[num][y1] = true;
    					}	
    					
    				// down
    				int downLim[] = {y1,y1};
    					
    					if(y1+1<8)
    					{
    						downLim[0] = y1+1;
    					}
    				
    				downLim[1] = downLim[0];
    					
    					while(downLim[1]<7 && position[x1][downLim[1]].equals("      "))
    					{
    						downLim[1]++;
    					}
    					for(int num = downLim[0];num<downLim[1]+1;num++)
    					{
    						positionMove[x1][num] = true;
    					}
    					
    				// up
    				int upLim[] = {y1,y1};
    					
    					if(y1-1>-1)
    					{
    						upLim[0] = y1-1;
    					}
    				
    				upLim[1] = upLim[0];
    					
    					while(upLim[1]>0 && position[x1][upLim[1]].equals("      "))
    					{
    						upLim[1]--;
    					}
    					for(int num = upLim[0];num>upLim[1]-1;num--)
    					{
    						positionMove[x1][num] = true;
    					}
    			}
    			if(position[x1][y1].charAt(1) == 'k' || position[x1][y1].charAt(1) == 'p' && position[x1][y1].charAt(4) == '1')
    			{
    					if(x1-2>-1 && y1+1<8)
    					{
    						positionMove[x1-2][y1+1] = true;
    					}
    					if(x1-1>-1 && y1+2<8)
    					{
    						positionMove[x1-1][y1+2] = true;
    					}
    					if(x1+1<8 && y1+2<8)
    					{
    						positionMove[x1+1][y1+2] = true;
    					}	
    					if(x1+2<8 && y1+1<8)
    					{
    						positionMove[x1+2][y1+1] = true;
    					}
    					if(x1+2<8 && y1-1>-1)
    					{
    						positionMove[x1+2][y1-1] = true;
    					}
    					if(x1+1<8 && y1-2>-1)
    					{
    						positionMove[x1+1][y1-2] = true;
    					}
    					if(x1-1>-1 && y1-2>-1)
    					{
    						positionMove[x1-1][y1-2] = true;
    					}
    					if(x1-2>-1 && y1-1>-1)
    					{
    						positionMove[x1-2][y1-1] = true;
    					}	
    			}
    			if(position[x1][y1].charAt(1) == 'b' || position[x1][y1].charAt(1) == 'p' && position[x1][y1].charAt(3) == '1' || position[x1][y1].charAt(1) == 'q')
    			{
    				// up-left
    				int stepUpLeft[] = {1,1};
    				
    					if(x1-stepUpLeft[0]>-1 && y1-stepUpLeft[0]>-1)
    					{
    							while(position[x1-stepUpLeft[0]][y1-stepUpLeft[0]].equals("      ") && x1-stepUpLeft[0]>-1 && y1-stepUpLeft[0]>-1)
    							{
    								stepUpLeft[0]++;
    							}
    							
    							for(int num = stepUpLeft[0];num>stepUpLeft[1]-1;num--)
    							{
    								positionMove[x1-num][y1-num] = true;
    							}
    					}
    					
    				// up-right
    				int stepUpRight[] = {1,1};
    					
    					if(x1+stepUpRight[0]+1<8 && y1-stepUpRight[0]-1>-1)
    					{
    							while(position[x1+stepUpRight[0]][y1-stepUpRight[0]].equals("      ") && x1+stepUpRight[0]<8 && y1-stepUpRight[0]>-1)
    							{
    								stepUpRight[0]++;
    							}
    						for(int num = stepUpRight[0];num>stepUpRight[1]-1;num--)
    						{
    							positionMove[x1+num][y1-num] = true;
    						}	
    					}
    
    				// down-right
    				int stepDownRight[] = {1,1};
    					
    					if(x1+stepDownRight[0]+1<8 && y1+stepDownRight[0]+1<8)
    					{
    							while(position[x1+stepDownRight[0]][y1+stepDownRight[0]].equals("      ") && x1+stepDownRight[0]<8 && y1+stepDownRight[0]<8)
    							{
    								stepDownRight[0]++;
    							}
    							for(int num = stepDownRight[0];num>stepDownRight[1]-1;num--)
    							{
    								positionMove[x1+num][y1+num] = true;
    							}
    					}
    		
    				// down-left
    				int stepDownLeft[] = {1,1};
    					
    					if(x1-stepDownLeft[0]+1>-1 && y1+stepDownLeft[0]+1<8)
    					{
    							while(position[x1-stepDownLeft[0]][y1+stepDownLeft[0]].equals("      ") && x1-stepDownLeft[0]<8 && y1+stepDownLeft[0]<8)
    							{
    								stepDownLeft[0]++;
    							}
    							for(int num = stepDownLeft[0];num>stepDownLeft[1]-1;num--)
    							{
    								positionMove[x1-num][y1+num] = true;
    							}
    					}
    			}
    			if(position[x1][y1].charAt(1) == 'w' || position[x1][y1].charAt(1) == 'p' && position[x1][y1].charAt(5) == '1')
    			{
    					if(y1-1>-1)
    					{
    						positionMove[x1][y1-1] = true;
    					}
    					if(y1+1<8)
    					{
    						positionMove[x1][y1+1] = true;
    					}
    					if(x1-1>-1)
    					{
    						positionMove[x1-1][y1] = true;
    					}	
    					if(x1+1<8)
    					{
    						positionMove[x1+1][y1] = true;
    					}
    					if(x1-1>-1 && y1-1>-1)
    					{
    						positionMove[x1-1][y1-1] = true;
    					}
    					if(x1+1<8 && y1-1>-1)
    					{
    						positionMove[x1+1][y1-1] = true;
    					}
    					if(x1-1>-1 && y1+1<8)
    					{
    						positionMove[x1-1][y1+1] = true;
    					}
    					if(x1+1<8 && y1+1<8)
    					{
    						positionMove[x1+1][y1+1] = true;
    					}	
    			}
    	}
    	public void saveGame()
    	{
    
    	}
    	public void loadGame()
    	{
    		
    	}
    	public void actionPerformed(ActionEvent e) 
    	{
    			if(e.getActionCommand().equals ("New Game"))
    			{
    				newGame();
    			}
    			if(e.getActionCommand().equals ("Save Game"))
    			{
    				saveGame();
    			}
    			if(e.getActionCommand().equals ("Load Game"))
    			{
    				loadGame();
    			}			
    			if(e.getActionCommand().equals ("Help"))
    			{
    					if(frameHelp.isVisible() == false && bolHelp == false)
    					{
    						bolHelp = true;						
    						@SuppressWarnings("unused")
    						JFrame FrameHelp = new classFrameHelp();
    					}
    					else
    						frameHelp.setVisible(true);
    			}
    			if(e.getActionCommand().equals ("Settings"))
    			{
    					if(frameSetting.isVisible() == false && bolSetting == false)
    					{
    						bolSetting = true;
    						@SuppressWarnings("unused")
    						JFrame FrameSettings = new classFrameSettings();
    					}
    					else 
    						frameSetting.setVisible(true);
    			}
    	}
    	public void win(String string)
    	{
    		
    	}
    	protected static ImageIcon createImageIcon (String path)
    	{
    		java.net.URL imgURL = proChess.class.getResource (path);
    		
    			if (imgURL != null)
    			{
    				return new ImageIcon (imgURL);
    			}
    			else
    			{
    				System.err.println ("Couldn't find file: " + path);
    				return null;
    			}
    	}
    	public void mouseClicked(MouseEvent e) 
    	{
    			if(bolChosen == false)
    			{
    					for(int y = 0; y < 8; y++)
    					{
    							for(int x = 0; x < 8; x++)
    							{
    								positionChosen[x][y] = false;
    							}
    					}
    					
    				curX = (int)(e.getX()/60);
    				curY = (int)(e.getY()/60);
    				
    					if(turn==1 && position[curX][curY].charAt(0) == 'w')
    					{
    						generateHighlightMoves(curX, curY, position[curX][curY]);
    					}
    					if(turn==2 && position[curX][curY].charAt(0) == 'b')
    					{
    						generateHighlightMoves(curX, curY, position[curX][curY]);
    					}
    					
    				bolChosen = true;
    			}
    			else 
    			{
    					if(positionMove[(int)(e.getX()/60)][(int)(e.getY()/60)] == true)
    					{
    							if(position[(int)(e.getX()/60)][(int)(e.getY()/60)].charAt(0) == 'w')
    							{
    									switch(position[(int)(e.getX()/60)][(int)(e.getY()/60)].charAt(1))
    									{
    										case 'p': gold[0] = gold[0] + 1;
    											break;
    										case 'r': gold[0] = gold[0] + 5;
    											break;
    										case 'b': gold[0] = gold[0] + 3;
    											break;
    										case 'k': gold[0] = gold[0] + 3;
    											break;
    										case 'q': gold[0] = gold[0] + 9;
    											break;
    										case 'w': win("Player One");
    									}
    							}
    							else
    							{
    									switch(position[(int)(e.getX()/60)][(int)(e.getY()/60)].charAt(1))
    									{
    										case 'p': gold[1] = gold[1] + 1;
    											break;
    										case 'r': gold[1] = gold[1] + 5;
    											break;
    										case 'b': gold[1] = gold[1] + 3;
    											break;
    										case 'k': gold[1] = gold[1] + 3;
    											break;
    										case 'q': gold[1] = gold[1] + 9;
    											break;
    										case 'w': win("Player Two");
    									}
    							}	
    							
    						position[(int)(e.getX()/60)][(int)(e.getY()/60)] = position[curX][curY];
    						position[curX][curY] = "      ";
    						
    							for(int y = 0; y < 8; y++)
    							{
    									for(int x = 0; x < 8; x++)
    									{
    										positionChosen[x][y] = false;
    										positionMove[x][y] = false;
    									}
    							}
    						
    						bolChosen = false;
    					}
    					else if((int)(e.getX()/60)!=curX || (int)(e.getY()/60)!= curY)
    					{
    							for(int y = 0; y < 8; y++)
    							{
    									for(int x = 0; x < 8; x++)
    									{
    									positionChosen[x][y] = false;
    									}
    							}
    						
    						curX = (int)(e.getX()/60);
    						curY = (int)(e.getY()/60);
    			
    						generateHighlightMoves(curX, curY, position[curX][curY]);
    					}
    			}
    			
    		repaint();
    	}		
    	public void mousePressed(MouseEvent e) 
    	{
    		// TODO Auto-generated method stub
    	}	
    	public void mouseReleased(MouseEvent e) 
    	{
    		// TODO Auto-generated method stub
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: painting to a JPanel in a JFrame

    Too much code! Consider constructing a SSCCE.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 2
    Last Post: 10-17-2011, 09:04 PM
  2. JFrame and JPanel
    By khiat in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 08:32 PM
  3. Problem painting on JPanel
    By Boatski in forum AWT / Swing
    Replies: 0
    Last Post: 12-01-2010, 05:36 AM
  4. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 12:24 PM
  5. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 11:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •