Results 1 to 6 of 6
Thread: need help with JPanel Images
- 04-21-2009, 02:12 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
need help with JPanel Images
I am working on creating a pacman game but i am having trouble putting an Image into JPanel....
any help is appreciated
Java Code:import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class gameBoard extends JPanel { SquareRx[][] squares; final int PAD = 20; public gameBoard() { int ROWS = 10; int COLS = 10; squares = new SquareRx[ROWS][COLS]; for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) { squares[i][j] = new SquareRx(i, j); } } } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); double xInc = (double)(w - 2*PAD)/squares[0].length; double yInc = (double)(h - 2*PAD)/squares.length; g2.setPaint(Color.black); for(int i = 0; i <= squares[0].length; i++) { double x = PAD + i*xInc; //g2.draw(new Line2D.Double(x, PAD, x, h-PAD)); } for(int i = 0; i <= squares.length; i++) { double y = PAD + i*yInc; //g2.draw(new Line2D.Double(PAD, y, w-PAD, y)); //left column g.fillRect(20,20 ,35,35); g.fillRect(20,55 ,35,35); g.fillRect(20,90 ,35,35); g.fillRect(20,120 ,35,35); g.fillRect(20,155 ,35,35); g.fillRect(20,190 ,35,35); g.fillRect(20,220 ,35,35); g.fillRect(20,255 ,35,35); g.fillRect(20,290 ,35,35); g.fillRect(20,320 ,35,35); g.fillRect(20,355 ,35,35); g.fillRect(20,390 ,35,35); g.fillRect(20,420 ,35,35); g.fillRect(20,455 ,35,35); g.fillRect(20,490 ,35,35); //top row g.fillRect(55,20 ,35,35); g.fillRect(90,20 ,35,35); g.fillRect(120,20 ,35,35); g.fillRect(155,20 ,35,35); g.fillRect(190,20 ,35,35); g.fillRect(220,20 ,35,35); g.fillRect(255,20 ,35,35); g.fillRect(290,20 ,35,35); g.fillRect(320,20 ,35,35); g.fillRect(355,20 ,35,35); g.fillRect(390,20 ,35,35); g.fillRect(420,20 ,35,35); g.fillRect(455,20 ,35,35); g.fillRect(490,20 ,35,35); //bottom row g.fillRect(55,490 ,35,35); g.fillRect(90,490 ,35,35); g.fillRect(120,490 ,35,35); g.fillRect(155,490 ,35,35); g.fillRect(190,490 ,35,35); g.fillRect(220,490 ,35,35); g.fillRect(255,490 ,35,35); g.fillRect(290,490 ,35,35); g.fillRect(320,490 ,35,35); g.fillRect(355,490 ,35,35); g.fillRect(390,490 ,35,35); g.fillRect(420,490 ,35,35); g.fillRect(455,490 ,35,35); g.fillRect(490,490 ,35,35); //right column g.fillRect(490,55 ,35,35); g.fillRect(490,90 ,35,35); g.fillRect(490,120 ,35,35); g.fillRect(490,155 ,35,35); g.fillRect(490,190 ,35,35); g.fillRect(490,220 ,35,35); g.fillRect(490,255 ,35,35); g.fillRect(490,290 ,35,35); g.fillRect(490,320 ,35,35); g.fillRect(490,355 ,35,35); g.fillRect(490,390 ,35,35); g.fillRect(490,420 ,35,35); g.fillRect(490,455 ,35,35); g.fillRect(490,490 ,35,35); //top right g.fillRect(90,90 ,35,35); g.fillRect(90,120 ,35,35); g.fillRect(90,155 ,35,35); g.fillRect(120,90 ,35,35); g.fillRect(155,90 ,35,35); g.fillRect(90, 420, 35, 35); g.fillRect(90, 390, 35, 35); g.fillRect(90, 355, 35, 35); g.fillRect(120, 420, 35, 35); g.fillRect(155, 420, 35, 35); g.fillRect(420, 90, 35, 35); g.fillRect(390, 90, 35, 35); g.fillRect(355, 90, 35, 35); g.fillRect(420, 120, 35, 35); g.fillRect(420, 155, 35, 35); g.fillRect(390, 420, 35, 35); g.fillRect(420, 390, 35, 35); g.fillRect(420, 355, 35, 35); g.fillRect(420, 420, 35, 35); g.fillRect(355, 420, 35, 35); } } public static void main(String[] args) { gameBoard test = new gameBoard(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(560,575); f.setLocation(200,200); f.setVisible(true); } } class SquareRx { private final int row; private final int col; private boolean occupied = false; public SquareRx(int r, int c) { row = r; col = c; } public void setOccupied(boolean occupied) { this.occupied = occupied; } public boolean isOccupied() { return occupied; } }
- 04-21-2009, 08:16 PM #2
There should be a g.drawImage(bla,bla,bla,bla) method... You can get the image with Image myImage=Toolkit.getDefaultToolkit().createImage(im ageName);...
Who Cares... As Long As It Works...
- 04-22-2009, 02:02 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
I used what you told me
everything looks good no errors
but still no image is coming up >.<
Java Code:import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class gameBoard extends JPanel { SquareRx[][] squares; final int PAD = 20; [B]Image myImage=Toolkit.getDefaultToolkit().createImage("pacy.jpg");[/B] public gameBoard() { int ROWS = 10; int COLS = 10; squares = new SquareRx[ROWS][COLS]; for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) { squares[i][j] = new SquareRx(i, j); } } } protected void paintComponent(Graphics g) { [B]g.drawImage(myImage,1,1,this);[/B] super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); double xInc = (double)(w - 2*PAD)/squares[0].length; double yInc = (double)(h - 2*PAD)/squares.length; g2.setPaint(Color.black); for(int i = 0; i <= squares[0].length; i++) { double x = PAD + i*xInc; //g2.draw(new Line2D.Double(x, PAD, x, h-PAD)); } for(int i = 0; i <= squares.length; i++) { double y = PAD + i*yInc; //g2.draw(new Line2D.Double(PAD, y, w-PAD, y)); //left column g.fillRect(20,20 ,35,35); g.fillRect(20,55 ,35,35); g.fillRect(20,90 ,35,35); g.fillRect(20,120 ,35,35); g.fillRect(20,155 ,35,35); g.fillRect(20,190 ,35,35); g.fillRect(20,220 ,35,35); g.fillRect(20,255 ,35,35); g.fillRect(20,290 ,35,35); g.fillRect(20,320 ,35,35); g.fillRect(20,355 ,35,35); g.fillRect(20,390 ,35,35); g.fillRect(20,420 ,35,35); g.fillRect(20,455 ,35,35); g.fillRect(20,490 ,35,35); //top row g.fillRect(55,20 ,35,35); g.fillRect(90,20 ,35,35); g.fillRect(120,20 ,35,35); g.fillRect(155,20 ,35,35); g.fillRect(190,20 ,35,35); g.fillRect(220,20 ,35,35); g.fillRect(255,20 ,35,35); g.fillRect(290,20 ,35,35); g.fillRect(320,20 ,35,35); g.fillRect(355,20 ,35,35); g.fillRect(390,20 ,35,35); g.fillRect(420,20 ,35,35); g.fillRect(455,20 ,35,35); g.fillRect(490,20 ,35,35); //bottom row g.fillRect(55,490 ,35,35); g.fillRect(90,490 ,35,35); g.fillRect(120,490 ,35,35); g.fillRect(155,490 ,35,35); g.fillRect(190,490 ,35,35); g.fillRect(220,490 ,35,35); g.fillRect(255,490 ,35,35); g.fillRect(290,490 ,35,35); g.fillRect(320,490 ,35,35); g.fillRect(355,490 ,35,35); g.fillRect(390,490 ,35,35); g.fillRect(420,490 ,35,35); g.fillRect(455,490 ,35,35); g.fillRect(490,490 ,35,35); //right column g.fillRect(490,55 ,35,35); g.fillRect(490,90 ,35,35); g.fillRect(490,120 ,35,35); g.fillRect(490,155 ,35,35); g.fillRect(490,190 ,35,35); g.fillRect(490,220 ,35,35); g.fillRect(490,255 ,35,35); g.fillRect(490,290 ,35,35); g.fillRect(490,320 ,35,35); g.fillRect(490,355 ,35,35); g.fillRect(490,390 ,35,35); g.fillRect(490,420 ,35,35); g.fillRect(490,455 ,35,35); g.fillRect(490,490 ,35,35); //top right g.fillRect(90,90 ,35,35); g.fillRect(90,120 ,35,35); g.fillRect(90,155 ,35,35); g.fillRect(120,90 ,35,35); g.fillRect(155,90 ,35,35); g.fillRect(90, 420, 35, 35); g.fillRect(90, 390, 35, 35); g.fillRect(90, 355, 35, 35); g.fillRect(120, 420, 35, 35); g.fillRect(155, 420, 35, 35); g.fillRect(420, 90, 35, 35); g.fillRect(390, 90, 35, 35); g.fillRect(355, 90, 35, 35); g.fillRect(420, 120, 35, 35); g.fillRect(420, 155, 35, 35); g.fillRect(390, 420, 35, 35); g.fillRect(420, 390, 35, 35); g.fillRect(420, 355, 35, 35); g.fillRect(420, 420, 35, 35); g.fillRect(355, 420, 35, 35); } } public static void main(String[] args) { gameBoard test = new gameBoard(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(560,575); f.setLocation(200,200); f.setVisible(true); } } class SquareRx { private final int row; private final int col; private boolean occupied = false; public SquareRx(int r, int c) { row = r; col = c; } public void setOccupied(boolean occupied) { this.occupied = occupied; } public boolean isOccupied() { return occupied; } }
- 04-22-2009, 03:29 PM #4
I ran you program and if you fix the following things you should be ok...
The first thing is that you pretty much always want to call any type of super first... So you will need to update that in your paintComponent
The second thing I am not really sure why this affects it but you want to take this out:
Other than that, your program should work...Java Code:g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Last edited by markw8500; 04-22-2009 at 07:44 PM.
Who Cares... As Long As It Works...
- 04-23-2009, 02:10 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 5
- Rep Power
- 0
I'm still getting nothing to come up
What are you refering to by calling the super first?
i tried a few different things too >.>
Java Code:import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class gameBoard extends JPanel { SquareRx[][] squares; final int PAD = 20; Image myImage=Toolkit.getDefaultToolkit().createImage("pacy.jpg"); public gameBoard() { int ROWS = 10; int COLS = 10; squares = new SquareRx[ROWS][COLS]; for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) { squares[i][j] = new SquareRx(i, j); } } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g.drawImage(myImage,1,1,this); int w = getWidth(); int h = getHeight(); double xInc = (double)(w - 2*PAD)/squares[0].length; double yInc = (double)(h - 2*PAD)/squares.length; g2.setPaint(Color.black); for(int i = 0; i <= squares[0].length; i++) { double x = PAD + i*xInc; //g2.draw(new Line2D.Double(x, PAD, x, h-PAD)); } for(int i = 0; i <= squares.length; i++) { double y = PAD + i*yInc; //g2.draw(new Line2D.Double(PAD, y, w-PAD, y)); } } public static void main(String[] args) { gameBoard test = new gameBoard(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(560,575); f.setLocation(200,200); f.setVisible(true); } } class SquareRx { private final int row; private final int col; private boolean occupied = false; public SquareRx(int r, int c) { row = r; col = c; } public void setOccupied(boolean occupied) { this.occupied = occupied; } public boolean isOccupied() { return occupied; } }
- 04-23-2009, 07:15 PM #6
Super referes to your super.paintComponent(g);... I was always tought to call super of any class first...
Everything that you have works for me... So double check your image name spelling and location... You can test it by creating a File object with your image and the ifFile() method...Who Cares... As Long As It Works...
Similar Threads
-
Resizing Images in JPanel??
By NoNickName in forum New To JavaReplies: 1Last Post: 04-09-2009, 10:19 PM -
Moving Images in JPanel
By killpoppop in forum AWT / SwingReplies: 7Last Post: 03-08-2009, 02:54 PM -
XML Images
By JavaWizz in forum XMLReplies: 1Last Post: 10-17-2008, 10:19 AM -
images
By amith in forum AWT / SwingReplies: 3Last Post: 06-27-2008, 08:38 PM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks