Skip to content

Instantly share code, notes, and snippets.

@sh0umik
Created July 31, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh0umik/44f117169cf71a8cab4d92f81c2a6248 to your computer and use it in GitHub Desktop.
Save sh0umik/44f117169cf71a8cab4d92f81c2a6248 to your computer and use it in GitHub Desktop.
java-mistake
package gametry;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Faiaz
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import gametry.Player;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Board extends JPanel {
private Game game;
private ImageIcon icon;
public Board(Game game) {
setPreferredSize(new Dimension(268, 268));
this.game = game;
}
// Mistake 1 : name is paintComponent not Paintcomponent , this is case sensetive
public void paintComponent(Graphics g) {
// Mistake 2 : super class call is the same as the function name , you wrote paint(g) previously !!
super.paintComponent(g);
BufferedImage background = null;
try {
// Mistake 3 : You have to include the full path of the image ... or it wont load ...
background = ImageIO.read(new File("/home/fahim/NetBeansProjects/gametry/src/gametry/board.jpg"));
} catch (Exception ex) {
//Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex);
}
try {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(background, 0, 0, null);
for (Player player : game.getPlayers()) {
g2d.setColor(player.getColor());
g2d.fillOval(player.getPosition().getX(), player.getPosition().getY(), 26, 26);
}
} catch(Exception ex) {
System.out.println(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment