resizable

dddd
 avatar
user_9487184
java
a year ago
3.8 kB
4
Indexable
Never
package it.unibo.game.app.view.jswing.impleentation;

import java.util.Map;
import java.util.Random;

import javax.swing.*;
import it.unibo.game.Pair;
import it.unibo.game.app.model.ball.Ball;
import it.unibo.game.app.view.jswing.api.GameView;
import it.unibo.game.app.view.jswing.api.UIController;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class GameViewImpl extends JPanel implements KeyListener, ActionListener, GameView {

    private UIController observer;
    private boolean play = true;

    public GameViewImpl(UIController control) {
        setFocusable(true);
        addKeyListener(this);
        setLayout(new FlowLayout());
        setFocusTraversalKeysEnabled(false);
        this.observer = control;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        // Determina la dimensione del pannello
        int panelWidth = getWidth();
        int panelHeight = getHeight();

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, panelWidth, panelHeight);

        // delta di 
        // getdimension torna le dimensioni del model
        double deltaH = ( observer.getFrameDim().getX() / observer.getDimension().getX());
        double deltaW = ( observer.getFrameDim().getY() / observer.getDimension().getY());

        observer.getList().entrySet().stream().forEach(x -> {
            g2d.setColor(Color.RED);
            // g2d.draw(new Rectangle2D.Double(x.getKey().getX(),x.getKey().getY(),
            // observer.getDimensionBrick().getY(), observer.getDimensionBrick().getX()));
            var rec = new Rectangle2D.Double(x.getKey().getX() * deltaH,  x.getKey().getY() * deltaW,
            observer.getDimensionBrick().getY() * deltaW, observer.getDimensionBrick().getX() * deltaH);
            g2d.fill(rec);
            g2d.setColor(Color.BLACK);
            g2d.draw(rec);
        });

        g2d.setColor(Color.GREEN);
        g2d.fill(new Ellipse2D.Double(observer.getBall().getX() * deltaH, observer.getBall().getY() * deltaW,
                observer.getRBall() * deltaW, observer.getRBall() * deltaH));

        g2d.setColor(Color.BLACK);
        g2d.fill(new Rectangle2D.Double(observer.getPadPos().getX() * deltaW, observer.getPadPos().getY() * deltaH,
                observer.getPadWight() * deltaW, observer.getPadHeight() * deltaH));
        
        g2d.dispose();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
            observer.movePadRight();
        }
        if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
            observer.movePadLeft();
        }
    }


    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        // throw new UnsupportedOperationException("Unimplemented method
        // 'keyReleased'");
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'keyTyped'");
    }

    @Override
    public void drawBall(Pair<Integer, Integer> pos, Integer radius, Graphics g) {
        g.setColor(Color.WHITE);
        g.drawOval(pos.getX() - radius, pos.getY() - radius, radius * 2, radius * 2);
    }

    @Override
    public void drawPad(Pair<Integer, Integer> pos, Pair<Integer, Integer> dimPad, Graphics g) {
        g.setColor(Color.GRAY);
        g.fillRect(pos.getX(), pos.getY(), dimPad.getX(), dimPad.getY());
        g.drawRect(pos.getX(), pos.getY(), dimPad.getX(), dimPad.getY());
    }
}