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

}


}

Recommended Answers

All 4 Replies

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.

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.

I don't know what your problem is. Do you use spaces or tabs to indent?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.