944,116 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5257
  • Java RSS
Jan 7th, 2007
0

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

Expand Post »
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

}




}

Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepak_gurav22 is offline Offline
3 posts
since Jan 2007
Jan 7th, 2007
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Jan 8th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepak_gurav22 is offline Offline
3 posts
since Jan 2007
Jan 8th, 2007
0

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

I don't know what your problem is. Do you use spaces or tabs to indent?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Jan 9th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deepak_gurav22 is offline Offline
3 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to switch an eclipse application to plugin?
Next Thread in Java Forum Timeline: unsafe operation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC