Currently I am trying to make an agar.io like game, and I am having problems with scrolling and viewports. Here is my code:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Game extends Applet{
public void paint (Graphics g)
{
Player p = new Player("test");
int centerX = p.getPos()[0];
int centerY = p.getPos()[1];
String name = p.getName();
int fontSize = (int)Math.round(20 - name.length() / 1.5);
for(int i = 0; i < p.getCells().length; i++){
g.setFont(new Font("Monospaced", Font.BOLD, fontSize));
g.setColor(p.getColor());
g.fillOval(p.getPos()[0] - p.getCells()[i].getMass() / 2, p.getPos()[1] - p.getCells()[i].getMass() / 2, p.getCells()[i].getMass(), p.getCells()[i].getMass());
g.setColor(new Color(0, 0, 0));
g.drawString(p.getName(), (int)Math.round(centerX - name.length() * Math.round(fontSize / 3)), centerY + Math.round(fontSize / 3));
}
}
public static void main(String args[])
{
Game a = new Game();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame();
frame.setSize(100000, 100000);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(a, BorderLayout.CENTER);
frame.setVisible(true);
}
}
This is the Player object code:
import java.awt.Color;
public class Player {
private int cellNumber, score;
private String name;
private Cell[] cells;
private Color color = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
public Player(){
name = "An unnamed cell";
cellNumber = 1;
cells = new Cell[]{new Cell()};
}
public Player(String n){
name = n;
cellNumber = 1;
cells = new Cell[]{new Cell()};
}
public void setName(String n){
name = n;
}
public void setCellNumber(int n){
cellNumber = n;
}
public void setCells(Cell[] n){
cells = n;
}
public String getName(){
return name;
}
public int getCellNumber(){
return cellNumber;
}
public Cell[] getCells(){
return cells;
}
public int getScore(){
int sum = 0;
for(Cell c : cells) sum += c.getMass();
return sum;
}
public int[] getPos(){
int sumX = 0;
for (Cell c : cells) sumX += c.getX();
int avgX = Math.round(sumX / cells.length);
int sumY = 0;
for (Cell c : cells) sumY += c.getY();
int avgY = Math.round(sumY / cells.length);
return new int[]{avgX, avgY};
}
public Color getColor(){
return color;
}
}
The Cell object Code:
public class Cell {
private int xCoor, yCoor, mass;
public Cell(){
xCoor = (int)(Math.random() * 10000);
yCoor = (int)(Math.random() * 10000);
mass = 100;
}
public void setX(int x){
xCoor = x;
}
public void setY(int y){
yCoor = y;
}
public void setMass(int m){
mass = m;
}
public int getX(){
return xCoor;
}
public int getY(){
return yCoor;
}
public int getMass(){
return mass;
}
}
This picture is a good representation of what I want.
Basically, what is drawn by paint is shown across the entire JFrame (which I think I already achieved, correct me if I'm wrong) but I only want to user to be able to view a small part of it.
I believe that to do this, you need to use a JViewPort and JScrollPane, but I don't know how to implement that into my code. Any help with that would be greatly appreciated.