java - Simultaneous movement of two graphics with different and changing text -


the code wrote giving different output required.

output got:

  1. in upper movement yellow rectangle moves left right in steps. in each step text changes 1 (signal 1, signal 2, signal 3 etc.)

  2. in lower movement text moves left right (at higher speed yellow rectangle above).

  3. text in both (above , below it) remains same i.e. if above "signal 1", below "signal 1", if above "signal 2" below same "signal 2". , on.

output required:

every thing should remain except: 1. in lower movement need text in green rectangle (same yellow rectangle above). 2. in lower movement need text of higher 2 in loop. in other words,

    if above "signal 1"  below should "signal 3".      if above "signal 2"  below should "signal 4".      if above "signal 3"  below should "signal 5".     if above "signal 4"  below should "signal 6".      if above "signal 5"  below should "signal 7".      if above "signal 6"  below should "signal 8".       if above "signal 7"  below should "signal 9".     if above "signal 8"  below should "signal 10".      if above "signal 9"  below should "signal 1".      if above "signal 10" below should "signal 2".  

being new programming , java need required output. in anticipation. complete working code is:

import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.rectangle; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.awt.font.*; import java.awt.font.fontrendercontext; import java.awt.geom.rectangle2d; import java.util.arraylist; import java.util.list;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities;  public class graphsteptwo implements runnable {      private jframe frame;      private voyagerunnable voyagerunnable;      public static void main(string[] args) {         swingutilities.invokelater(new graphsteptwo());     }      @override     public void run() {         frame = new jframe("image move");         frame.setdefaultcloseoperation(jframe.do_nothing_on_close);         frame.addwindowlistener(new windowadapter() {             @override             public void windowclosing(windowevent event) {                 exitprocedure();             }         });          drawingpanel drawingpanel = new drawingpanel();         frame.add(drawingpanel);          frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);          voyagerunnable = new voyagerunnable(drawingpanel, new voyage());         new thread(voyagerunnable).start();     }      public void exitprocedure() {         voyagerunnable.setrunning(false);         frame.dispose();         system.exit(0);       }      public class drawingpanel extends jpanel {////////////////////////////////          private int xpos, ypos, width, height, xpos2, ypos2, width2, height2;          private step step;           public drawingpanel() {             this.width = 100;             this.height = 50;             this.xpos = 0;             this.ypos = 50;               this.width2 = 100;             this.height2 = 40;             this.xpos2 = 0;             this.ypos2 = 150;              this.setpreferredsize(new dimension(800, 200));         }           public void setstep(step step) {             this.step = step;             this.xpos += 10;             this.xpos2 += 35;             repaint();         }            protected void paintcomponent(graphics g) {             super.paintcomponent(g);              g.setcolor(color.white);             g.fillrect(0, 0, getwidth(), getheight());              g.setcolor(color.orange);             g.fillrect(xpos, ypos, width, height);              if (step != null) {                 g.setcolor(color.black);                 centerstring(g, new rectangle(xpos, ypos, width, height),                         step.getsignal(), g.getfont());                     centerstring(g, new rectangle(xpos2 , ypos2 , width2 , height2),                         step.getsignal(), g.getfont());              }         }          public void centerstring(graphics g, rectangle r, string s, font font) {             fontrendercontext frc = new fontrendercontext(null, true, true);              rectangle2d r2d = font.getstringbounds(s, frc);             int rwidth = (int) math.round(r2d.getwidth());             int rheight = (int) math.round(r2d.getheight());             int rx = (int) math.round(r2d.getx());             int ry = (int) math.round(r2d.gety());              int = (r.width / 2) - (rwidth / 2) - rx;             int b = (r.height / 2) - (rheight / 2) - ry;              g.setfont(font);             g.drawstring(s, r.x + a, r.y + b);         }     }      public class voyagerunnable implements runnable {          private boolean running;          private drawingpanel drawingpanel;          private voyage voyage;          public voyagerunnable(drawingpanel drawingpanel, voyage voyage) {             this.drawingpanel = drawingpanel;             this.voyage = voyage;             this.running = true;         }          @override         public void run() {             while (running) {                 step step = voyage.getstep();                 setstep(step);                 sleep(step);             }         }          public void setstep(final step step) {             swingutilities.invokelater(new runnable() {                 @override                 public void run() {                        drawingpanel.setstep(step);                  }             });         }           private void sleep(step step) {             try {                 thread.sleep(step.getdelay());             } catch (interruptedexception e) {              }         }          public void setrunning(boolean running) {             this.running = running;         }      }      public class voyage {          private int index;         private list<step> steps;          public voyage() {             this.steps = new arraylist<>();              this.steps.add(new step("signal 1", 2000l));             this.steps.add(new step("signal 2", 1000l));             this.steps.add(new step("signal 3", 2000l));             this.steps.add(new step("signal 4", 1000l));             this.steps.add(new step("signal 5", 2000l));             this.steps.add(new step("signal 6", 1000l));             this.steps.add(new step("signal 7", 2000l));             this.steps.add(new step("signal 8", 1000l));             this.steps.add(new step("signal 9", 2000l));             this.steps.add(new step("signal 10", 1000l));              this.index = 0;         }          public step getstep() {             step step = steps.get(index);             index = ++index % steps.size();             return step;         }     }      public class step {///////////////////////////////////////////////////         private final string signal;         private final long delay;          public step(string signal, long delay) {             this.signal = signal;             this.delay = delay;         }          public string getsignal() {             return signal;         }          public long getdelay() {             return delay;         }      }  } 

looks homework me. don't understand approach of giving students pile of code don't understand instead of making them code scratch. also, naming "drawingpanel" pretty bad. might better off studying programming reading on internet course material that.

instead of adding width2 etc. drawingpanel, supposed create 2 of them; 1 each box text in it.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -