Not able to Display image on canvas, want to create game.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 3
Reputation: deepak_gurav22 is an unknown quantity at this point 
Solved Threads: 0
deepak_gurav22 deepak_gurav22 is offline Offline
Newbie Poster

Not able to Display image on canvas, want to create game.

 
0
  #1
Jan 7th, 2007
Here is the code that I have written please check what is wrong, as I am not able to display Images on canvas. I am trying to create game.
package PuyoPuyo;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* Puyo.java
*
* Created on January 1, 2007, 11:18 AM
*/
public class Puyo implements KeyListener, Runnable{
Display display;
Canvas canvas;
//Ball ball[];
final int height = 12;
final int width = 6;
final int canvas_height = height*32;
final int canvas_width = width*32;
public Image img[];
private Image doubleBuffer = null;
/** Creates new form Puyo */

public Puyo() {
initComponents();
}
private void initComponents() {
JFrame mainpuyo = new JFrame() ;
//GEN-BEGIN:initComponents
java.awt.Toolkit toolkit = Toolkit.getDefaultToolkit();

img = new Image[4];
img[0] = toolkit.getImage(this.getClass().getResource("/puyo_blue.png"));
img[1] = toolkit.getImage(this.getClass().getResource("/puyo_red.png"));
img[2] = toolkit.getImage(this.getClass().getResource("/puyo_green.png"));
img[3] = toolkit.getImage(this.getClass().getResource("/puyo_yellow.png"));

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
mainpuyo.getContentPane().setLayout(null);
mainpuyo.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mainpuyo.setTitle("Puyo");
mainpuyo.setMaximizedBounds(new java.awt.Rectangle(0, 0, 368, 400));
mainpuyo.setResizable(false);
mainpuyo.pack();
mainpuyo.setSize(new java.awt.Dimension(400,500));
mainpuyo.setVisible(true);
jButton1.setText("New Game");
mainpuyo.add(jButton1);
jButton1.setBounds(270, 70, 120, 23);
jButton2.setText("Close");
mainpuyo.add(jButton2);
jButton2.setBounds(270, 120, 120, 23);

jPanel1.setBorder(new javax.swing.border.TitledBorder("Puyo Game"));
mainpuyo.add(jPanel1);
jPanel1.setBounds(20, 20, 240, 430);

jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

canvas = new Canvas();
canvas.setBounds(20,20,240,430);
canvas.setSize(canvas_width,canvas_height);
canvas.setBackground(Color.white);
jPanel1.add(canvas);
canvas.addKeyListener(this);

}//GEN-END:initComponents

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{

}
public static void main(String args[]) {
Puyo puyo = new Puyo();
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private JButton jButton1;
private javax.swing.JButton jButton2;
private JPanel jPanel1;

public void keyTyped(KeyEvent arg0) {


}
public void keyPressed(KeyEvent arg0) {
int id = arg0.getKeyCode();
switch(id)
{
case(KeyEvent.VK_LEFT):
{
display.left();
}
break;
case( KeyEvent.VK_RIGHT):
{
display.right();
}
break;
case ( KeyEvent.VK_UP):
{
display.rotateup();
}
break;
case (KeyEvent.VK_DOWN):
{
display.rotatedown();
}
break;
}
}
public void keyReleased(KeyEvent arg0) {

}
protected void paint()
{
Graphics g=canvas.getGraphics();
if (doubleBuffer != null) {
// paint to double buffer
Graphics g2 = doubleBuffer.getGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,canvas_width,canvas_height);
// draw the balls
drawBall(g2);
g2.dispose();
// copy double buffer to screen
g.drawImage(doubleBuffer, 0, 0, null);
}else{

doubleBuffer = canvas.createImage(canvas_width, canvas_height);
}
}
/**
* @param g2
*/
private void drawBall(Graphics g2) {
// TODO Auto-generated method stub

g2.drawImage(img[2], 6*32, 5*32, null);

}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub

}




}

Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Not able to Display image on canvas, want to create game.

 
0
  #2
Jan 7th, 2007
your coding style leaves a lot to be desired. Do you really code your program with everything left justified on the page as you posted it? I wanted to edit your post to add code tags but that would not have helped. Not very many (if any) people will even read your program when it looks like that.
Last edited by Ancient Dragon; Jan 8th, 2007 at 8:55 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3
Reputation: deepak_gurav22 is an unknown quantity at this point 
Solved Threads: 0
deepak_gurav22 deepak_gurav22 is offline Offline
Newbie Poster

Re: Not able to Display image on canvas, want to create game.

 
0
  #3
Jan 8th, 2007
no, When I pasted the code from Notepad, it must have got Left Justified.
Can you please let me know what can be the possible problem?
Thanxs for you help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Not able to Display image on canvas, want to create game.

 
0
  #4
Jan 8th, 2007
I don't know what your problem is. Do you use spaces or tabs to indent?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3
Reputation: deepak_gurav22 is an unknown quantity at this point 
Solved Threads: 0
deepak_gurav22 deepak_gurav22 is offline Offline
Newbie Poster

Re: Not able to Display image on canvas, want to create game.

 
0
  #5
Jan 9th, 2007
I use Tabs,
Can you provide me with your e-mail address, so that I can e-mail you the code that that I have written,
SO that you can view it. Please go through the code and let me know whts happening as I am not able to figure it out.
My only problem is that the image is not getting displayed. The code is running fine, without any error.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC