在Java中使用Graphics时如何反转Y轴的极性?

How to reverse the polarities of Y-axis when using Graphics in Java?

我想问的基本问题是:我如何制作一个图表,其中 y=0 处的线实际上位于 y=*some number* 并且随着我的 Y 增加,Java的y减少了?

我正在尝试制作一个折线图来记录动物种群的变化。我需要使图形的 Y=0 围绕 y=980 线。我还需要做一些会注意到人口增加的事情,并将其绘制为 y 的减少(使线向上)。我想说的是,我需要创建一个看起来像折线图的折线图。

我尝试了多种不同的方法,每种方法都会根据不同的输入给出不同的结果。我已经成功创建了从 y=980 线开始向上的图表,如下图所示。我为此使用的方法是画线(对于图形本身),然后取10乘以该值的差值的绝对值减去90,如

所示
g.drawLine((125), (y+1)*93, width, (y+1)*93);
g.drawString(Math.abs(y*10-90) + " alive", (80), (y+1)*93);

这完全适用于图表,但是当我试图将其应用到线条图中时,我收到了不同的结果。

对于这个项目,我有 3 个种群:我的动物的捕食者、我的动物的猎物和我的动物。我想绘制所有这些的人口规模。使用之前展示的方法时,我成功绘制了捕食者种群的第一个值。然而,另外两个位于图表的另一侧,与它们必须所在的位置相反。 (例如,我的动物的种群数量设置为从 90 开始,但在图表上它大约在 20 多岁左右,如下图所示)。我用于其中每一个的代码是:

// predator animal line
g.setColor(Color.red);
g.drawLine(i*93+125, (Math.abs((predatorAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((predatorAnimalAmt[i+1]*10)-90)));
g.drawLine(100, height-45, 120, height-45);
g.drawString("PREDATOR", 30, height-40);
// prey animal line
g.setColor(Color.green);
g.drawLine(i*93+125, (Math.abs((preyAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((preyAnimalAmt[i+1]*10)-90)));
g.drawLine(100, height-60, 120, height-60);
g.drawString("PREY", 30, height-55);
// our animal's line
g.setColor(Color.blue);
g.drawLine(i*93+125, (Math.abs((ourAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((ourAnimalAmt[i+1]*10)-90)));
g.drawLine(100, height-75, 120, height-75);
g.drawString("OUR ANIMAL", 30, height-70);

这是我在 class 中的代码(my github page 中还有其他 classes 可以访问)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Random;

// ALL OF MY CLASSES
import natural.selection.main.animalia.Animal;
import natural.selection.main.animalia.OurAnimal;
import natural.selection.main.animalia.Predator;
import natural.selection.main.animalia.Prey;

public class MainApp implements Runnable {

private Display display;
private int width, height;
public String title;

private boolean running = false;
private Thread thread;

private Random random = new Random();

private BufferStrategy bs;
private Graphics g;

private ArrayList<Animal> allAnimals = new ArrayList<>();
private int[] animalAmt = new int[20];

private ArrayList<Prey> allPreyAnimals = new ArrayList<>();
private int[] preyAnimalAmt = new int[20];
private ArrayList<OurAnimal> allOurAnimals = new ArrayList<>();
private int[] ourAnimalAmt = new int[20];
private ArrayList<Predator> allPredatorAnimals = new ArrayList<>();
private int[] predatorAnimalAmt = new int[20];

public MainApp(String title, int width, int height){
    this.width = width;
    this.height = height;
    this.title = title;
}

private void init(){
    display = new Display(title, width, height);

    for(int i=0; i<90; i++) {
        OurAnimal animal = new OurAnimal(random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10));
        allOurAnimals.add(animal);
        allAnimals.add(animal);
    }
    for(int i=0; i<(80+random.nextInt(10)); i++) {
        Prey animal = new Prey(random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10));
        allPreyAnimals.add(animal);
        allAnimals.add(animal);
    }
    for(int i=0; i<(50+random.nextInt(10)); i++) {
        Predator animal = new Predator(random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10), random.nextInt(10));
        allPredatorAnimals.add(animal);
        allAnimals.add(animal);
    }
    animalAmt[0] = allAnimals.size();
    preyAnimalAmt[0] = allPreyAnimals.size();
    ourAnimalAmt[0] = allOurAnimals.size();
    predatorAnimalAmt[0] = allPredatorAnimals.size();
}

private void tick() {

}

// Amount of weeks to simulate
private int weeksToSim = 20;

private void render(){
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    //Clear Screen
    g.clearRect(0, 0, width, height);
    //Draw Here!

    g.setColor(Color.black);

    // LINE GRAPH OF SPECIES COUNT
    // Draw the graph
    for(int x=0; x<weeksToSim; x++) {
        g.drawLine(x*93+125, height-100, x*93+125, 0);
        g.drawString("Week " + (x+1), x*93+125-20, height-80);
    }
    for(int y=0; y<10; y++) {
        g.drawLine((125), (y+1)*93, width, (y+1)*93);
        g.drawString(Math.abs(y*10-90) + " alive", (80), (y+1)*93);
    }
    // Draw the line
    for(int i=0; i<(animalAmt.length-1); i++) {
        // predator animal line
        g.setColor(Color.red);
        g.drawLine(i*93+125, (Math.abs((predatorAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((predatorAnimalAmt[i+1]*10)-90)));
        g.drawLine(100, height-45, 120, height-45);
        g.drawString("PREDATOR", 30, height-40);
        // prey animal line
        g.setColor(Color.green);
        g.drawLine(i*93+125, (Math.abs((preyAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((preyAnimalAmt[i+1]*10)-90)));
        g.drawLine(100, height-60, 120, height-60);
        g.drawString("PREY", 30, height-55);
        // our animal's line
        g.setColor(Color.blue);
        g.drawLine(i*93+125, (Math.abs((ourAnimalAmt[i]*10)-90)), (i+1)*93+125, (Math.abs((ourAnimalAmt[i+1]*10)-90)));
        g.drawLine(100, height-75, 120, height-75);
        g.drawString("OUR ANIMAL", 30, height-70);
    }

    //End Drawing!
    bs.show();
    g.dispose();
}

public void run(){

    init();

    int fps = 60;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    int ticks = 0;

    Toolkit.getDefaultToolkit().sync();

    while(running){
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        if(delta >= 1){
            tick();
            render();
            ticks++;
            delta--;
        }

        if(timer >= 1000000000){
            System.out.println("Ticks and Frames: " + ticks);
            ticks = 0;
            timer = 0;
        }
    }

    stop();

}

public int getWidth(){
    return width;
}

public int getHeight(){
    return height;
}

public synchronized void start(){
    if(running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop(){
    if(!running)
        return;
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}   
}

我如何在不完全破坏我的所有代码的情况下,只用一个公式来使所有的点都正确?

这是我的代码得到的结果:

让我们将此功能提取到两个函数中,计算给定 weekcount 的正确 xy 位置。这样,您只需更改单个函数即可更改图表布局,而不是分散在整个代码中的多行代码:

private int getChartX(double week) {
    return (int)Math.round(week * 93 + 32);
}

private int getChartY(double count) {
    return (int)Math.round(930 - count * 9.3);
}

使用这些函数,您可以像这样重新构建绘图代码:

// Draw the graph
for(int week = 1; week <= weeksToSim; week++) {
    int x = getChartX(week);
    g.drawLine(x, getChartY(0), x, 0);
    g.drawString("Week " + week, x-20, getChartY(0) + 20);
}
for(int y=0; y<10; y++) {
    int count = 10 * y;
    int y = getChartY(count);
    g.drawLine(getChartX(1),  y, width, y);
    g.drawString(count + " alive", getChartX(1) - 45, y);
}
// Draw the line
for(int i=0; i<(animalAmt.length-1); i++) {
    int week = i + 1;
    // predator animal line
    g.setColor(Color.red);
    g.drawLine(getChartX(week), getChartY(predatorAnimalAmt[i]), getChartX(week + 1), getChartY(predatorAnimalAmt[i + 1]));
    ...
}