我正在使用java和Processing进行延时可视化。这段代码来自Eclipse,我用它来帮助自己,因为Processing缺乏自动完成功能。问题在于void PozeniPrezentacijo()
方法。我遇到的问题是,当我做了10次risi()(risi的意思是绘制)然后TimeUnit.SECONDS.sleep(1)
时,它会冻结10秒,只显示最后一个draw() (risi())
的输出。我也尝试过使用Thread.sleep()
,但同样的情况也发生了。这可能很难调试,因为您必须将处理库导入Eclipse。链接
我的完整代码:
import g4p_controls.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import de.bezier.data.sql.*;
import java.awt.Font;
import java.util.concurrent.TimeUnit;
import processing.core.*;
public class MyProcessingSketch extends PApplet {
// Create all the GUI controls.
// autogenerated do not edit
/* =========================================================
* ==== WARNING ===
* =========================================================
* The code in this tab has been generated from the GUI form
* designer and care should be taken when editing this file.
* Only add/edit code inside the event handlers i.e. only
* use lines between the matching comment tags. e.g.
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
* Do not rename this tab!
* =========================================================
*/
public void handleSliderEvents(GSlider slider, GEvent event) {
println("integer value:" + slider1.getValueI() + " float value:" + slider1.getValueF());
if(slider1.getValueI() == 1810){NastaviRadije(slovar1);
risi();}
else if(slider1.getValueI() == 1820){NastaviRadije(slovar2);
risi();}
else if(slider1.getValueI() == 1830){NastaviRadije(slovar3);
risi();}
else if(slider1.getValueI() == 1840){NastaviRadije(slovar4);
risi();}
else if(slider1.getValueI() == 1850){NastaviRadije(slovar5);
risi();}
else if(slider1.getValueI() == 1860){NastaviRadije(slovar6);
risi();}
else if(slider1.getValueI() == 1870){NastaviRadije(slovar7);
risi();}
else if(slider1.getValueI() == 1880){NastaviRadije(slovar8);
risi();}
else if(slider1.getValueI() == 1890){NastaviRadije(slovar9);
risi();}
else if(slider1.getValueI() == 1900){NastaviRadije(slovar2);
risi();}
}
public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:393804:
NastaviBarve();
risi();
} //_CODE_:button1:393804:
public void button2_click1(GButton source, GEvent event) throws Exception { //_CODE_:button1:393804:
PozeniPrezentacijo();
}
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setCursor(ARROW);
if(frame != null)
frame.setTitle("Sketch Window");
sketchPad1 = new GSketchPad(this, 18, 13, 762, 459);
sketchPad1.setOpaque(true);
label1 = new GLabel(this, 220, 270, 240, 50);
label1.setText("Drzava1");
label1.setOpaque(false);
label2 = new GLabel(this, 620, 300, 240, 50);
label2.setText("Drzava2");
label2.setOpaque(false);
label3 = new GLabel(this, 790, 110, 240, 50);
label3.setText("Drzava3");
label3.setOpaque(false);
label4 = new GLabel(this, 860, 300, 240, 50);
label4.setText("Drzava4");
label4.setOpaque(false);
label5 = new GLabel(this, 1030, 200, 240, 50);
label5.setText("Drzava5");
label5.setOpaque(false);
label6 = new GLabel(this, 220, 580, 240, 50);
label6.setText("Drzava6");
label6.setOpaque(false);
label7 = new GLabel(this, 470, 550, 240, 50);
label7.setText("Drzava7");
label7.setOpaque(false);
label8 = new GLabel(this, 590, 500, 240, 50);
label8.setText("Drzava8");
label8.setOpaque(false);
label9 = new GLabel(this, 710, 550, 240, 50);
label9.setText("Drzava9");
label9.setOpaque(false);
label10 = new GLabel(this, 830, 500, 240, 50);
label10.setText("Drzava10");
label10.setOpaque(false);
//label11 = new GLabel(this, 430, 20, 300, 30);
//label11.setText("Vizualizacija prikazuje število avtoric iz posamezne države po desetletjih");
//label11.setOpaque(false);
slider1 = new GSlider(this, 123, 620, 983, 61, (float) 10.0);
slider1.setShowValue(true);
slider1.setShowLimits(true);
slider1.setLimits(1810, 1810, 1900);
slider1.setNbrTicks(10);
slider1.setStickToTicks(true);
slider1.setShowTicks(true);
slider1.setNumberFormat(G4P.DECIMAL, 2);
slider1.setOpaque(false);
slider1.addEventHandler(this, "handleSliderEvents");
button1 = new GButton(this, 1150, 600, 80, 30);
button1.setText("Spremeni barve");
button1.addEventHandler(this, "button1_click1");
button2 = new GButton(this, 1150, 650, 120, 30);
button2.setText("Poženi predstavitev");
button2.addEventHandler(this, "button2_click1");
}
// Variable declarations
// autogenerated do not edit
GSketchPad sketchPad1;
GLabel label1;
GLabel label2;
GLabel label3;
GLabel label4;
GLabel label5;
GLabel label6;
GLabel label7;
GLabel label8;
GLabel label9;
GLabel label10;
GLabel label11;
GLabel label12;
GSlider slider1;
GButton button1;
GButton button2;
MySQL msql;
HashMap<String, Integer> slovar1 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar2 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar3 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar4 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar5 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar6 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar7 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar8 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar9 = new HashMap<String, Integer>();
HashMap<String, Integer> slovar10 = new HashMap<String, Integer>();
Integer radij1, radij2, radij3, radij4, radij5, radij6, radij7, radij8, radij9, radij10;
String drzava1, drzava2,drzava3, drzava4, drzava5, drzava6, drzava7, drzava8, drzava9, drzava10;
PFont f;
//color c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
//colorMode(RGB,255);
int c1 = color(255,0,0);
int c2 = color(255,128,0);
int c3 = color(255,255,0);
int c4 = color(128,255,0);
int c5 = color(0,255,0);
int c6 = color(0,255,128);
int c7 = color(0,255,255);
int c8 = color(0,128,255);
int c9 = color(0,0,255);
int c10 = color(127,0,255);
public void setup() {
size(1280, 720);
f = createFont("Arial",16,true);
createGUI();
String user = "studentgo";
String pass = "yP6YZcbSacFcCpuq";
String database = "studentgo";
// imena polj v bazi: ime, naslov, leto_izdaje, drzava_izdaje, receptor, spol, tip, leto_recepcije, drzava_recepcije
msql = new MySQL( this, "delphi.fri.uni-lj.si:3306", database, user, pass );
if (msql.connect()) {//Drzave za obdobje 1800 do 1900;
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1800 AND leto_izdaje<1810 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar1.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1810 AND leto_izdaje<1820 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar2.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1820 AND leto_izdaje<1830 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar3.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1830 AND leto_izdaje<1840 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar4.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1840 AND leto_izdaje<1850 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar5.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1850 AND leto_izdaje<1860 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar6.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1860 AND leto_izdaje<1870 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar7.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1870 AND leto_izdaje<1880 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar8.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1880 AND leto_izdaje<1890 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar9.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
msql.query("SELECT drzava_izdaje, count(drzava_izdaje) s FROM avtorice where leto_izdaje>1890 AND leto_izdaje<1900 group by drzava_izdaje");
while (msql.next()) {
String stevilo = msql.getString("s");
int pravoSt = Integer.parseInt(stevilo);
String beseda = msql.getString("drzava_izdaje");
if (beseda.contains("unknown / not relevant")) {
continue;
} else {
slovar10.put(beseda, pravoSt);
}
//print(beseda +"," + pravoSt +"n");
}
}
else {
// connection failed !
}
NastaviRadije(slovar1);
risi();
}//konec setup
public void draw() {
//background(255, 255, 255);
}
void NastaviBarve(){
c1 = color(random(255), random(255), random(255));
c2 = color(random(255), random(255), random(255));
c3 = color(random(255), random(255), random(255));
c4 = color(random(255), random(255), random(255));
c5 = color(random(255), random(255), random(255));
c6 = color(random(255), random(255), random(255));
c7 = color(random(255), random(255), random(255));
c8 = color(random(255), random(255), random(255));
c9 = color(random(255), random(255), random(255));
c10 = color(random(255), random(255), random(255));
}
void NastaviRadije(HashMap slovar){
ArrayList<Integer> stevila = new ArrayList<Integer>(slovar.values());
Collections.sort(stevila);
Collections.reverse(stevila);
radij1 = stevila.get(0);
radij2 = stevila.get(1);
radij3 = stevila.get(2);
radij4 = stevila.get(3);
radij5 = stevila.get(4);
radij6 = stevila.get(5);
radij7 = stevila.get(6);
radij8 = stevila.get(7);
radij9 = stevila.get(8);
radij10 = stevila.get(9);
NastaviDrzave(slovar);
}
void NastaviDrzave(HashMap slovar){
ArrayList<String> drzave = new ArrayList<String>(slovar.keySet());
for( String drzava : drzave){
Integer radij = (Integer) slovar.get(drzava);
if(radij == radij1) {drzava1 = drzava;}
else if(radij == radij2) {drzava2 = drzava;}
else if(radij == radij3) {drzava3 = drzava;}
else if(radij == radij4) {drzava4 = drzava;}
else if(radij == radij5) {drzava5 = drzava;}
else if(radij == radij6) {drzava6 = drzava;}
else if(radij == radij7) {drzava7 = drzava;}
else if(radij == radij8) {drzava8 = drzava;}
else if(radij == radij9) {drzava9 = drzava;}
else if(radij == radij10) {drzava10 = drzava;}
else continue;
}
}
void risi(){
background(255, 255, 255);
fill(254,0,0);
textFont(f,30);
textAlign(LEFT);
text("Vizualizacija prikazuje število avtoric iz posamezne države po desetletjih",200,30);
fill(0,0,0);
textFont(f,20);
textAlign(LEFT);
text("Premakni drsnik za začetek.",950,80);
textFont(f,20);
textAlign(LEFT);
text("Pritisni gumb za spremembo barv.",950,100);
ellipseMode(CENTER);
fill(c1);
ellipse(220,270,radij1,radij1);
label1.setText(drzava1+" ("+radij1.toString()+")");
label1.setFont(new Font("Monospaced", Font.PLAIN, 20));
label1.setTextBold();
fill(c2);
ellipse(620,300,radij2,radij2);
label2.setText(drzava2+" ("+radij2.toString()+")");
label2.setFont(new Font("Monospaced", Font.PLAIN, 20));
label2.setTextBold();
fill(c3);
ellipse(790,110,radij3,radij3);
label3.setText(drzava3+" ("+radij3.toString()+")");
label3.setFont(new Font("Monospaced", Font.PLAIN, 20));
label3.setTextBold();
fill(c4);
ellipse(860,300,radij4,radij4);
label4.setText(drzava4+" ("+radij4.toString()+")");
label4.setFont(new Font("Monospaced", Font.PLAIN, 20));
label4.setTextBold();
fill(c5);
ellipse(1030,200,radij5,radij5);
label5.setText(drzava5+" ("+radij5.toString()+")");
label5.setFont(new Font("Monospaced", Font.PLAIN, 20));
label5.setTextBold();
fill(c6);
ellipse(220,580,radij6,radij6);
label6.setText(drzava6+" ("+radij6.toString()+")");
label6.setFont(new Font("Monospaced", Font.PLAIN, 20));
label6.setTextBold();
fill(c7);
ellipse(470,550,radij7,radij7);
label7.setText(drzava7+" ("+radij7.toString()+")");
label7.setFont(new Font("Monospaced", Font.PLAIN, 20));
label7.setTextBold();
fill(c8);
ellipse(590,500,radij8,radij8);
label8.setText(drzava8+" ("+radij8.toString()+")");
label8.setFont(new Font("Monospaced", Font.PLAIN, 20));
label8.setTextBold();
fill(c9);
ellipse(710,550,radij9,radij9);
label9.setText(drzava9+" ("+radij9.toString()+")");
label9.setFont(new Font("Monospaced", Font.PLAIN, 20));
label9.setTextBold();
fill(c10);
ellipse(830,500,radij10,radij10);
label10.setText(drzava10+" ("+radij10.toString()+")");
label10.setFont(new Font("Monospaced", Font.PLAIN, 20));
label10.setTextBold();
}
void PozeniPrezentacijo() throws Exception{
slider1.setValue(1810);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1820);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1830);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1840);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1850);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1860);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1870);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1880);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1890);
risi();
TimeUnit.SECONDS.sleep(1);
slider1.setValue(1900);
risi();
TimeUnit.SECONDS.sleep(1);
}
}
如果你要使用Processing及其库,你必须遵守它的规则。。特别是G4P的UI依赖于void draw(),这意味着你不能随意调用noLoop并刷新屏幕。。。而且看起来Processing的draw()实际上并没有直接调用draw(,而是只设置了一个需要重新绘制窗口的标志。这里的总体想法是,你必须围绕Processing的draw()循环进行设计。。。所以我建议这样做:
而不是实际调用捕获方法来触发一系列事件,这些事件将在每个draw()中根据顺序显示不同的信息。因此:
触发事件:
public void button2_click1(GButton source, GEvent event) throws Exception { //_CODE_:button1:393804:
val = 1810;
}
绘制方法:
public void draw() {
PozeniPrezentacijo();
}
实际方法:
int val = 1901;
void PozeniPrezentacijo() {
if (val < 1901 && frameCount%30 == 0) {
slider1.setValue(val);
risi();
val+=10;
}
}
frameCount%30=0允许您每30帧只运行一次