我正在处理UDP Socket的服务器端,每次都在线上遇到NullPointerException DatagramPacket receivePacket = new DatagramPacket(receiveData, 4096);.客户端是Python文件.这是服务器的代码.这些方法setup()和draw()通过不同的加工文件调用.
import processing.net.*;
import java.io.*;
import java.net.*;
import java.util.*;
//Server myServer;
DatagramSocket serverSocket;
byte[] receiveData;
InetAddress IPAddressList;
int portList = -1;
void setup(){
try{
DatagramSocket serverSocket = new DatagramSocket(21567);
}
catch (IOException e){
e.printStackTrace();
System.out.println(e);
}
byte[] receiveData = new byte[4096];
}
void draw(){
float P1,P2,P3;
print ("hello");
try{
DatagramPacket receivePacket = new DatagramPacket(receiveData, 4096);
serverSocket.receive(receivePacket);
String greeting = new String(receivePacket.getData());
System.out.println("From Client: " + greeting);
IPAddressList = …Run Code Online (Sandbox Code Playgroud) 我有这段代码,我想知道如何制作一个循环来绘制这些线...... x 轴上每 50 个像素......我很好奇如何做到这一点并想使用一个循环而不是手动绘制每一行!以下是这些行的代码......请提供任何帮助,将不胜感激!
//set sidewalk
fill(255,255,255);
rect(0,490,500,10);
line(50,490,50,500);
line(100,490,100,500);
line(150,490,150,500);
line(200,490,200,500);
line(250,490,250,500);
line(300,490,300,500);
line(350,490,350,500);
line(400,490,400,500);
line(450,490,450,500);
Run Code Online (Sandbox Code Playgroud) 我的计算不正常.我看不出代码有什么问题.有时它不能正确计算得分.有时它完美无缺.我甚至不能理解它什么时候做得正确以及什么时候做得不好.
分数计算应该是这样的:
Ace可以将总分增加到1或11.如果分数高于21,则ace计算为1; 否则ace是11.
这是我的代码:
// Updates the the value of the cards the player has in their hand
int updateValueOfHand() {
int result = 0; // it will be returned
int ace = 0; // value of ace
for (int i =0; i < playerHand.size(); i++) // loop to see players hand
{
int cardValue; // card value of hand
Card card=(Card)playerHand.get(i); // check the card
cardValue = card.getRank();
if (cardValue == 1) // if card value is 1 …Run Code Online (Sandbox Code Playgroud) 我试图在Processing中的屏幕上绘制一个非常大的字符(例如数字"3"和字符"A").理想情况下,角色的高度将略小于屏幕高度.
我试过了 :
textSize(360); 在屏幕上绘制文字之前使用.然而,它仍然不够大,这个词是模糊的.以前有人做过这样的事吗?谢谢你的帮助!
我有这个arraylist:
// Add predators
predators = new ArrayList();
for (int i = 0; i < predNum; i++) {
Creature predator = new Creature(random(width), random(height), 2);
predators.add(predator);
}
Run Code Online (Sandbox Code Playgroud)
如何构造语句,以便predators每隔500帧删除arraylist 中的最后一个元素?它需要某种循环吗?
if (frameCount == 500){
predators.remove(1)
}
Run Code Online (Sandbox Code Playgroud) 我正在试验以下代码:
//3D Spectrogram with Microphone Input
//Modified by kylejanzen 2011 - https://kylejanzen.wordpress.com
//Based on script written by John Locke 2011 - http://gracefulspoon.com
//Output .DXF file at any time by pressing "r" on the keyboard
import processing.dxf.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
import peasy.*;
PeasyCam cam;
FFT fftLin;
FFT fftLog;
Waveform audio3D;
Minim minim;
AudioInput microphone;
boolean record;
PFont font;
float camzoom;
float maxX = 0;float maxY = 0;float maxZ = 0;
float minX = 0;float minY = 0;float minZ = 0; …Run Code Online (Sandbox Code Playgroud) 我有一个处理弹跳球和矩形的程序。我可以正确地获得矩形边的碰撞,但我不知道如何获得角。这是我到目前为止:
int radius = 20;
float circleX, circleY; //positions
float vx = 3, vy = 3; //velocities float boxW, boxH; //bat dimensions
void setup(){
size(400,400);
ellipseMode(RADIUS);
rectMode(RADIUS);
circleX = width/4;
circleY = height/4;
boxW = 50;
boxH = 20;
}
void draw(){
background(0);
circleX += vx;
circleY += vy;
//bouncing off sides
if (circleX + radius > width || circleX < radius){ vx *= -1; } //left and right
if (circleY + radius > height || circleY < radius){ vy …Run Code Online (Sandbox Code Playgroud) 我正在Processing中编写一个程序,我将矢量规范化为单位矢量.当我这样做时,我希望我的矢量的幅度为1.但是,我的矢量幅度是这个(见下文)
幅度接近于1,但它并不完全是 1.我发现,如果我声明一个等于矢量幅度的变量,我可以将矢量的幅度设为1,并将矢量的分量除以变量而不是矢量的幅度,就像下面.
我知道为什么当我使用变量时我的矢量幅度更准确?
magequals mag(),mag是一个浮点数,同时mag()返回一个浮点数,所以我不明白为什么我的数量会有所不同.
我的整个代码如下.
PVector center, mouse;
void setup() {
size(1000,300);
background(0);
stroke(255);
center = new PVector(width/2,height/2);
}
void draw() {
background(0);
mouse = new PVector(mouseX, mouseY);
mouse.sub(center); //Mouse is now the vector between mouse and center.
mouse.normalize();
println("magnitude: " + mouse.mag() +
", Vector coordinates: (" + mouse.xPos + "," + mouse.yPos + ")" );
/* These values are getting normalized already, so the magnitude of the …Run Code Online (Sandbox Code Playgroud) 我正在创建一个程序,通过以下输入计算一顿饭的总成本:meal_cost,tax_rate,tip_rate,number_eating
并使用函数调用将它们打印在字符串中.我查看了StackOverflow,但找不到适合我情况的问题(打印字符串函数返回的字典)
我有一个函数,它接受所有输入并返回一个字典输出.我想在函数调用的字符串中打印返回的值,所有这些都在同一行中.这是我试过的:
def calculatedCost(meal_cost,tax_rate,tip_rate,number_eating):
tax = round(float(meal_cost * tax_rate) / 100,2)
tip = round(float(meal_cost * tip_rate) / 100,2)
total_cost = round(meal_cost + tax + tip,2)
division = round(total_cost / number_eating,2)
return {'tax': tax, 'tip': tip, 'total_cost':total_cost, 'division':division}
print("The cost of your meal is: {total_cost}, the tax on your meal is: {tax}, the tip is equal to: {tip}, and the split total is: {division}".format(calculatedCost(62.75,5,20,2)))
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:(我正在使用Processing)
KeyError: total_cost
processing.app.SketchException: KeyError: total_cost
at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:240)
at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:119)
at java.lang.Thread.run(Thread.java:748)
Run Code Online (Sandbox Code Playgroud)