2 years ago

#66510

test-img

Hasnain

Eclipse game window opening very small

Whenever I run my game, the game window opens up very small, like below, how can I fix this. I believe it is something to do with JFrame.setPreferredSize(new Dimension(x,y)); but I don't know where to implement this in the code for it to work.

This is what it shows: enter image description here

Where do I implement the code? Like in the class, outside the class, or do I need to show my current code for someone to help me here?

This is my code:

public class RoadAppMain extends JFrame {

private static final int D_W = 1920; //Window dimension
private static final int D_H = 1280; //Window height
int grassWidth = 1920; //Width of grass
int vanishHeight = 768; // Height of vanishing point
int roadWidth = 900; //Width of the road
int rumbleLen = 800; //Length of each track stripe 
double camDist = 0.8; //Camera distance 
int N; //Size of each row or line
int playerPosX = 0; //Movement of player left and right
int playerPosY = 0; //Movement of player up and down

List<Line> lines = new ArrayList<RoadAppMain.Line>();

List<Integer> listValues = new ArrayList<Integer>();

DrawPanel drawPanel = new DrawPanel();

public RoadAppMain() {
    for (int i = 0; i < 1600; i++) {
        Line line = new Line();
        line.z = i * rumbleLen;
        int curveAngle = (int) (Math.random() * 15 + 1);
        
        if (i > 20 && i < 70) { 
            line.curve = curveAngle;
        }
        
        else if (i > 100 && i < 150) {
            line.curve = -curveAngle;
        }
        
        else if (i > 180 && i < 230) {
            line.curve = curveAngle;
        }
        
        else if (i > 260 && i < 310) {
            line.curve = -curveAngle;
        }
        
        else if (i > 340 && i < 390) {
            line.curve = curveAngle;
        }
        
        else if (i > 400 && i < 420) {
            
        }
        
        lines.add(line);
        
    }
    
    N = lines.size();

    //Handles action events by user
    ActionListener listener = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {

            drawPanel.repaint();

        }
    };
    Timer timer = new Timer(1, listener);
    timer.start();
    add(drawPanel);

    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

//Moves screen using arrow keys
private class DrawPanel extends JPanel {

    public DrawPanel() {
        
        String VK_LEFT = "VK_LEFT";
        KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW); //necessary
        inputMap.put(W, VK_LEFT);
        ActionMap actionMap = getActionMap(); //necessary
        actionMap.put(VK_LEFT, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                playerPosX -= 200;
                drawPanel.repaint();
            }
        });

        String VK_RIGHT = "VK_RIGHT";
        KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
        inputMap.put(WVK_RIGHT, VK_RIGHT);
        actionMap.put(VK_RIGHT, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                playerPosX += 200;
                drawPanel.repaint();
            }
        });

        String VK_UP = "VK_UP";
        KeyStroke WVK_UP = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        inputMap.put(WVK_UP, VK_UP);
        actionMap.put(VK_UP, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                playerPosY += 200;
                drawPanel.repaint();
            }
        });

        String VK_DOWN = "VK_DOWN";
        KeyStroke WVK_DOWN = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
        inputMap.put(WVK_DOWN, VK_DOWN);
        actionMap.put(VK_DOWN, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                playerPosY -= 200;
                drawPanel.repaint();

        }
        });
    }
        //Drawing components feature
        protected void paintComponent(Graphics g) {
            drawValues(g);
            g.setColor(Color.black);
            g.fillRect(0, 0, 1920, 395);
        }

}

private void drawValues(Graphics g) {
    int startPos = playerPosY / rumbleLen;
    double x = 0; //Initial X position of screen on road
    double dx = 0; //Correlation between the angle of the road and player
    double maxY = vanishHeight;
    int camH = 1700 + (int) lines.get(startPos).y; //Height of the camera 
    //Starting position 
    for (int n = startPos; n < startPos + 300; n++) {
        Line l = lines.get(n % N); //Position of line
        l.project(playerPosX - (int) x, camH, playerPosY); 
        x += dx;
        dx += l.curve;
        if (l.Y > 0 && l.Y < maxY) {

            maxY = l.Y;

            Color grass = ((n / 2) % 2) == 0 ? new Color(21, 153, 71) : new Color(22, 102, 52); //Color for grass (first is for lighter, second for darker)
            Color rumble = ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(222, 4, 4); // Color for rumble (first is white, second is red)
            Color road = new Color(54, 52, 52); // Color of road or asphalt
            Color midel =  ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(54, 52, 52); //Color of hashed lines (first for white lines, second for gap)

            Color car = new Color(104, 104, 104);
            Color tire = new Color(0, 0, 0);
            Color stripe = new Color(0, 0, 0);
            Color light = new Color(253, 0, 0);
            Color hood = new Color(0, 0, 0);
            Color frame = new Color(0,0,255);
        
            
            Line p = null;

            if (n == 0) {
                p = l;
            } else {
                p = lines.get((n - 1) % N);
            }

            draw(g, grass, 0, (int) p.Y, grassWidth, 0, (int) l.Y, grassWidth); //(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2)

            draw(g, rumble, (int) p.X, (int) p.Y, (int) (p.W * 2.03), (int) l.X, (int) l.Y, (int) (l.W * 2.03)); //Affects width of rumble
            
            draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 1.8), (int) l.X, (int) l.Y, (int) (l.W * 1.8));
            
            draw(g, midel, (int) p.X, (int) p.Y, (int) (p.W * 0.78), (int) l.X, (int) l.Y, (int) (l.W * 0.78)); //ADD HERE

            draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 0.7), (int) l.X, (int) l.Y, (int) (l.W* 0.7)); //To cover the gap in between each midel. Must be after to allow it to colour over it
            
            draw(g, car, 965, 927, 125, 965, 1005, 125);
            draw(g, tire, 875, 1005, 35, 875, 1050, 35);
            draw(g, tire, 1055, 1005, 35, 1055, 1050, 35);
            draw(g, stripe, 1050, 965, 30, 870, 980, 30);
            draw(g, stripe, 1050, 950, 30, 870, 965, 30);
            draw(g, hood, 965, 880, 90, 965, 927, 125);
            
            draw(g, light, 875, 950, 5, 875, 980, 5);
            draw(g, light, 890, 950, 5, 890, 980, 5);
            draw(g, light, 905, 950, 5, 905, 980, 5);
            draw(g, light, 1025, 950, 5, 1025, 980, 5);
            draw(g, light, 1040, 950, 5, 1040, 980, 5);
            draw(g, light, 1055, 950, 5, 1055, 980, 5);
            
            draw(g, frame, 965, 874, 86, 965, 880, 90);
            draw(g, light, 965, 874, 35, 965, 880, 45);
        
            }
        }
    
    }

    void draw(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2) {

        Graphics g9d = g;
        int[] x9Points = { x1 - w1, x2 - w2, x2 + w2, x1 + w1 };
        int[] y9Points = { y1, y2, y2, y1 };
        int n9Points = 4;
        g9d.setColor(c);
        g9d.fillPolygon(x9Points, y9Points, n9Points);

    }
    
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            new RoadAppMain();
        }
    });
}

class Line {
    double x, y, z;
    double X, Y, W;
    double scale, curve;


    public Line() {
        curve = x = y = z = 0;
    }

    void project(int camX, int camY, int camZ) {
        scale = camDist / (z - camZ);
        X = (1 + scale * (x - camX)) * grassWidth / 2;
        Y = (1 - scale * (y - camY)) * vanishHeight / 2;
        W = scale * roadWidth * grassWidth / 2;
    }
}

}

java

eclipse

window

game-development

java

eclipse

window

game-development

0 Answers

Your Answer

Accepted video resources