gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:10:06 28 4
gpt4 key购买 nike

我想问的基本问题是:我如何制作一个图表,其中y=0处的线实际上位于y=*某个数字* 随着我的 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);

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

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

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

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

resulting graph

最佳答案

让我们将此功能提取到两个函数中,计算给定计数的正确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]));
...
}

关于java - 在Java中使用Graphics时如何反转Y轴的极性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59490277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com