如何在JFrame中创建视口?

时间:2016-01-05 01:08:25

标签: java swing jframe

目前我正在尝试制作类似agar.io的游戏,而且我遇到滚动和视口问题。这是我的代码:

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);
    }
}

这是播放器对象代码:

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;
    }
}

Cell对象代码:

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;
    }
}

这张照片很好地反映了我的想法。

enter image description here

基本上,在整个JFrame中显示了绘制的内容(我认为我已经实现了,如果我错了,请纠正我),但我只想让用户能够查看它的一小部分

我相信要做到这一点,你需要使用JViewPort和JScrollPane,但我不知道如何在我的代码中实现它。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

我遇到了与您类似的情况,并且实施了类似的方法。我不知道它是否理想,如果有人可以验证的话,我将不胜感激。它确实完成了我一直在寻找的工作,但是缓慢的帧速率并没有帮助。

import javax.swing.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class MovingAnimationJava7 extends Canvas {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private BufferedImage canvas;
    private JViewport viewport;
    

    public MovingAnimationJava7(int width, int height) {
        canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }
    
    public void start() {
        Thread.currentThread();
        int i = 0;
        while(true) {
            try {
                repaint();
                Point point = new Point(i,i++);
                viewport.setViewPosition(point);
                Thread.sleep(100);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void paint(Graphics g) {
        Random r = new Random();
        for (int x = 0; x < canvas.getWidth(); x++) {
            for (int y = 0; y < canvas.getHeight(); y++) {
                int red=r.nextInt(256);
                int green=r.nextInt(256);
                int blue=r.nextInt(256);
                Color c=new Color(red,green,blue);
                int color = c.getRGB();
                canvas.setRGB(x, y, color);
            }
        }
        
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(canvas, null, null);
    }
    
    private JViewport getVP() { return viewport; }
    private void setVP(JViewport vp) { viewport = vp; }

    public static void main(String[] args) {
        MovingAnimationJava7 animatedCanvas = new MovingAnimationJava7(400,400);
        
        JViewport viewport = new JViewport();
        viewport.setSize(159, 143);
        viewport.add(animatedCanvas);
        Point point = new Point(0,0);
        viewport.setViewPosition(point);
        animatedCanvas.setVP(viewport);
        
        JFrame jp = new JFrame();
        jp.getContentPane().add(animatedCanvas.getVP(), BorderLayout.CENTER);
        jp.setSize(new Dimension(500,500));
        jp.setVisible(true);
        animatedCanvas.start();
    }
}