2014-06-15 2 views
0

Vraiment besoin d'aide !!! Je me demandais comment je pouvais changer mon applet en JFrame. Je n'aime pas l'applet que le gars a utilisé dans son tutoriel et je voudrais passer à un JFrame. Je suis très nouveau à Java et oui j'ai très peu de connaissances. Toute aide serait grandement appréciée! Le jeu est un jeu de serpent.Comment changer mon code d'applet en JFrame?

TUTORIAL: https://www.youtube.com/watch?v=FABTl1Q1byw

APPLET CLASSE:

import java.applet.Applet; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Color; 

public class snakeApplet extends Applet{ 

    private SnakeCanvas c;//410x310 

    public void init(){ 
    c = new SnakeCanvas(); 
    c.setPreferredSize(new Dimension(410, 310)); 
    c.setVisible(true); 
    c.setFocusable(true); 
    this.add(c); 
    this.setVisible(true); 
    this.setSize(new Dimension(410, 310)); 

} 

public void paint(Graphics g){ 
    this.setSize(new Dimension(410, 310)); 
} 
} 

JEU DE CLASSE:

public class SnakeCanvas extends Canvas implements Runnable, KeyListener{ 
private final int BOX_HEIGHT = 10; 
private final int BOX_WIDTH = 10; 
private final int GRID_WIDTH = 25; 
private final int GRID_HEIGHT = 25; 

private LinkedList<Point> snake; 
private Point fruit; 
private int direction = Direction.NO_DIRECTION; 

private Thread runThread; 
private int score = 0; 
private String highscore = ""; 

public void init() { 

} 

public void paint(Graphics g) { 
    if (snake == null) { 
     snake = new LinkedList<Point>(); 
     GenerateDefaultSnake(); 
     PlaceFruit(); 
    } 

if (runThread == null) { 
    this.setPreferredSize(new Dimension(640, 480)); 
    this.addKeyListener(this); 
    runThread = new Thread(this); 
    runThread.start(); 
} 

if (highscore.equals("")) { 
    highscore = this.GetHighScore(); 
} 

drawFruit(g); 
drawGrid(g); 
drawSnake(g); 
drawScore(g); 

} 

public void update(Graphics g) { 
Graphics offScreenGraphics; //Graphics used to draw offscreen 
BufferedImage offscreen = null; 
Dimension d = this.getSize(); 

offscreen = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); 
offScreenGraphics = offscreen.getGraphics(); 
offScreenGraphics.setColor(this.getBackground());//CHANGE BACKGROUND COLOR 
offScreenGraphics.fillRect(0, 0, d.width, d.height); 
offScreenGraphics.setColor(this.getForeground()); 
paint(offScreenGraphics); 

//flip 
g.drawImage(offscreen, 0, 0, this); 
} 

public void GenerateDefaultSnake() { 
score = 0; 
snake.clear(); 

snake.add(new Point(0,2)); 
snake.add(new Point(0,1)); 
snake.add(new Point(0,0)); 
direction = Direction.NO_DIRECTION; 

} 


public void Move() { 
Point head = snake.peekFirst(); 
Point newPoint = head; 
switch(direction) { 
    case Direction.NORTH: 
     newPoint = new Point(head.x, head.y - 1); 
     break; 
    case Direction.SOUTH: 
     newPoint = new Point (head.x, head.y + 1); 
     break; 
    case Direction.WEST: 
     newPoint = new Point (head.x - 1, head.y); 
     break; 
    case Direction.EAST: 
     newPoint = new Point (head.x + 1, head.y); 
     break; 
} 

snake.remove(snake.peekLast()); 

if(newPoint.equals(fruit)){ 
score+=10; 
//the snake has hit fruit 
Point addPoint = (Point) newPoint.clone(); 

switch(direction){ 
case Direction.NORTH: 
newPoint = new Point(head.x, head.y-1); 
break; 
case Direction.SOUTH: 
newPoint = new Point (head.x, head.y+1); 
break; 
case Direction.WEST: 
newPoint = new Point (head.x-1, head.y); 
break; 
case Direction.EAST: 
newPoint = new Point (head.x+1, head.y); 
break; 
} 
snake.push(addPoint); 
PlaceFruit(); 
} 
else if(newPoint.x < 0 || newPoint.x > GRID_WIDTH - 1){ 
//we went out of bounce, reset game 
CheckScore(); 
GenerateDefaultSnake(); 
return; 
} 
else if(newPoint.y < 0 || newPoint.y > GRID_HEIGHT - 1){ 
//we went out of bounce, reset game 
CheckScore(); 
GenerateDefaultSnake(); 
return; 
} 
else if(snake.contains(newPoint)){ 
//we ran into ourselves, reset game 
CheckScore(); 
GenerateDefaultSnake(); 
return; 
} 

//if we reach this point in code, we're still alive 
snake.push(newPoint); 
} 

public void drawScore(Graphics g){ 
g.drawString("Score: " + score, 0, BOX_HEIGHT * GRID_HEIGHT + 10); 
g.drawString("HighScore: " + highscore, 0, BOX_HEIGHT * GRID_HEIGHT + 20); 
} 

public void CheckScore(){ 
if(highscore.equals("")) 
return; 

if(score > Integer.parseInt((highscore.split(":")[1]))){ 
String name = JOptionPane.showInputDialog("NEW HIGHSCORE! PLEASE ENTER YOUR NAME."); 
highscore = name + ":" + score; 

File scoreFile = new File("highscore.dat"); 
if(!scoreFile.exists()){ 
try{ 
scoreFile.createNewFile(); 
} 
catch(Exception e){ 
e.printStackTrace(); 
} 
} 
FileWriter writeFile = null; 
BufferedWriter writer = null; 
try{ 
writeFile = new FileWriter(scoreFile); 
writer = new BufferedWriter(writeFile); 
writer.write(this.highscore); 
} 
catch(Exception e){ 
} 
finally{ 
try{ 
if(writer !=null) 
    writer.close(); 
} 
catch(Exception e){} 
} 
} 
} 



public void drawGrid(Graphics g){ 
//drawing an outside rectangle 
g.drawRect(0, 0, GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT); 

} 

public void drawSnake(Graphics g){ 
g.setColor(Color.GREEN); 
for (Point p: snake){ 
g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT); 
} 
g.setColor(Color.BLACK); 
} 

public void drawFruit(Graphics g){ 
g.setColor(Color.RED); 
g.fillOval(fruit.x * BOX_WIDTH, fruit.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT); 
g.setColor(Color.BLACK); 
} 


public void PlaceFruit(){ 
Random rand = new Random(); 
int randomX = rand.nextInt(GRID_WIDTH); 
int randomY = rand.nextInt(GRID_HEIGHT); 
Point randomPoint = new Point(randomX, randomY); 
while(snake.contains(randomPoint)){ 
randomX = rand.nextInt(GRID_WIDTH); 
randomY = rand.nextInt(GRID_HEIGHT); 
randomPoint = new Point (randomX, randomY); 
} 
fruit = randomPoint; 
} 

@Override 
public void run(){ 
while(true){//runs indefinitely 
Move(); 
repaint(); 

try{ 
Thread.currentThread(); 
Thread.sleep(100); 
} 
catch(Exception e){ 
e.printStackTrace(); 
} 
} 

} 

public String GetHighScore(){ 

FileReader readFile = null; 
BufferedReader reader = null; 

try{ 
readFile = new FileReader("highscore.dat"); 
reader = new BufferedReader(readFile); 
return reader.readLine(); 
} 
catch(Exception e){ 
return "Nobody:0"; 
} 

finally{ 
try{ 
if(reader != null) 
reader.close(); 
} 
catch(Exception e){ 
e.printStackTrace(); 
} 
} 

} 

@Override 
public void keyTyped(KeyEvent e){ 
//TODO Auto-generated method stub 

} 

@Override 
public void keyPressed(KeyEvent e) { 
switch (e.getKeyCode()) 
{ 
case KeyEvent.VK_UP: 
if (direction != Direction.SOUTH) 
direction = Direction.NORTH; 
break; 
case KeyEvent.VK_DOWN: 
if (direction != Direction.NORTH) 
direction = Direction.SOUTH; 
break; 
case KeyEvent.VK_RIGHT: 
if (direction != Direction.WEST) 
direction = Direction.EAST; 
break; 
case KeyEvent.VK_LEFT: 
if (direction != Direction.EAST) 
direction = Direction.WEST; 
break; 

} 
} 

@Override 
public void keyReleased(KeyEvent e){ 

} 



} 
+1

Avez-vous envisagé de créer un [hybride] (http://stackoverflow.com/q/12449889/230513)? – trashgod

+0

comme faire fonctionner l'applet dans un JFrame? aimerait savoir comment – Pasoon

+0

@Pasoon: En fait, le code que vous avez collé ici, n'appartient pas à 'Swing', il appartient à' AWT'. Alors pourquoi la question est marquée avec «Swing»? Erreur –

Répondre

1

Je pense que la solution la plus simple est de faire un snakeAppletJFrame.

public class SnakeFrame extends JFrame { 
    private static final long serialVersionUID = 1L; 
    private SnakeCanvas c;// 410x310 

    public SnakeFrame() { 
    c = new SnakeCanvas(); 
    c.setPreferredSize(new Dimension(410, 310)); 
    c.setVisible(true); 
    c.setFocusable(true); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.add(c); 
    } 

    public static void main(String[] args) { 
    Runnable doRun = new Runnable() { 
     @Override 
     public void run() { 
     SnakeFrame sf = new SnakeFrame(); 
     sf.setSize(new Dimension(410, 310)); 
     sf.setVisible(true); 
     } 
    }; 
    new Thread(doRun).start(); 
    } 
} 
+0

où devrais-je placer ces? Je les ai placés tous les deux dans la classe snakeapplet et j'ai juste eu une jframe – Pasoon

+0

Dans une nouvelle classe appelée SnakeFrame –

+0

merci beaucoup !!! Où puis-je changer le titre de mon JFrame ou définir LocationRelativeto (null) ... où dois-je placer ce – Pasoon