Hi, i am making a battleship game and i can't get it to run on Swing with JApplet or JFrames, but i did manage to get it to run with awt. But i managed to get one playerboard up.
The main problem with awt was that i coulnd't get a proper scroll to work, becouse i want that two players should be able to have their
own playboard and that you scroll down to your own playboard when it is is your turn, but i couldnt get this working with awt Applet, so i converted to JFrames/JApplet.
But then i coulndt get it to display the playboard no longer and i am stuck and i think the problem is real simple just that i don't know what to do.
I would really appreiate if someone could spot the problem. Also i would like to know if i sohuld use JApplet or JFrame, adn if i use JApplet should the code layout be like Applet with init(), update(), threads or like JFrame?
Battleship.
package Battleship;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Battleship extends JFrame {
public static void main(String[] args) {
Battleship frame = new Battleship();
frame.setVisible(true);
}
private int JAppletWidth = 600;
private int JAppletHeight = 400;
private JScrollPane scroll;
private JFrame Jf;
private Board gameBoard;
private Graphics g;
private Container cont;
public Battleship(){
super("Paintwindow");
cont = this.getContentPane();
JPanel center = new JPanel();
center.setBackground(Color.white);
gameBoard = new Board(10,10);
gameBoard.setPreferredSize(new Dimension(400,400));
gameBoard.setBackground(Color.WHITE);
cont.add(gameBoard,"South");
this.setTitle("My new window");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack(); // does layout of components.
// gameBoard.RitaRutnät(g, 10, 10, 10);
}
}
package Battleship;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Board extends JScrollPane{
private int lineDimension=0,amountLines=0;
public Board(int lineDimension, int amountLines){
this.lineDimension=lineDimension;
this.amountLines=amountLines;
setBackground(Color.white);
setPreferredSize(new Dimension(350, 320));
}
public void changeLD(int lineDimension)
{
this.lineDimension = lineDimension;
}
public void changeAL(int amountLines)
{
this.amountLines = amountLines;
}
public void paintComponent(Graphics c,int x, int y, int storlek)
{
super.paintComponent(c);
for(int i=0; i<amountLines+1;i++)
{
Color cd = new Color(50,50,50);
c.setColor(cd);
c.fillRect(i*(storlek/amountLines)+x, y, lineDimension, storlek);
}
// c.fillRect(x, y, width, height)
for(int i=0; i<amountLines+1;i++)
{
Color cd = new Color(50,50,50);
c.setColor(cd);
c.fillRect(x, i*(storlek/amountLines)+y, storlek, lineDimension);
}
}
public void DrawBoard(Graphics c,int x, int y, int storlek)
{
super.paintComponent(c);
for(int i=0; i<amountLines+1;i++)
{
Color cd = new Color(50,50,50);
c.setColor(cd);
c.fillRect(i*(storlek/amountLines)+x, y, lineDimension, storlek);
}
// c.fillRect(x, y, width, height)
for(int i=0; i<amountLines+1;i++)
{
Color cd = new Color(50,50,50);
c.setColor(cd);
c.fillRect(x, i*(storlek/amountLines)+y, storlek, lineDimension);
}
}
}
Edited by: Arne122 on Jan 3, 2010 9:39 AM