how to separate the logic from the form in java swing? -


i have difficulties in separating logic of game view. view should dumb , render current state of model. can not figure out how that. please me in making code simpler , separating logic form? because want extend game , add new things can not because of confusing form of code. new in java.

import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.geom.*;  public class projectileshootertest {     public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 createandshowgui();             }         });     }      private static void createandshowgui() {         jframe f = new jframe();         f.setdefaultcloseoperation(jframe.exit_on_close);         f.setsize(600, 600);         final projectileshooter projectileshooter = new projectileshooter();         projectileshooterpanel projectileshooterpanel = new projectileshooterpanel(                 projectileshooter);         projectileshooter.setpaintingcomponent(projectileshooterpanel);         jpanel controlpanel = new jpanel(new gridlayout(1, 0));         controlpanel.add(new jlabel("angle"));         final jslider angleslider = new jslider(0, 90, 45);         controlpanel.add(angleslider);         controlpanel.add(new jlabel("power"));         final jslider powerslider = new jslider(0, 100, 50);         controlpanel.add(powerslider);         jbutton shootbutton = new jbutton("shoot");         shootbutton.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent e) {                 int angledeg = angleslider.getvalue();                 int power = powerslider.getvalue();                 projectileshooter.setangle(math.toradians(angledeg));                 projectileshooter.setpower(power);                 projectileshooter.shoot();             }         });         controlpanel.add(shootbutton);         f.getcontentpane().setlayout(new borderlayout());         f.getcontentpane().add(controlpanel, borderlayout.north);         f.getcontentpane().add(projectileshooterpanel, borderlayout.center);         f.setvisible(true);     } }  class projectileshooter {     private double anglerad = math.toradians(45);     private double power = 50;     private projectile projectile;     private jcomponent paintingcomponent;      void setpaintingcomponent(jcomponent paintingcomponent) {         this.paintingcomponent = paintingcomponent;     }      void setangle(double anglerad) {         this.anglerad = anglerad;     }      void setpower(double power) {         this.power = power;     }      void shoot() {         thread t = new thread(new runnable() {             @override             public void run() {                 executeshot();             }         });         t.setdaemon(true);         t.start();     }      private void executeshot() {         if (projectile != null) {             return;         }         projectile = new projectile();         point2d velocity = affinetransform.getrotateinstance(anglerad).transform(                 new point2d.double(1, 0), null);         velocity.setlocation(velocity.getx() * power * 0.5, velocity.gety() * power * 0.5);         projectile.setvelocity(velocity);         long prevtime = system.nanotime();         while (projectile.getposition().gety() >= 0) {             long currenttime = system.nanotime();             double dt = 3 * (currenttime - prevtime) / 1e8;             projectile.performtimestep(dt);             prevtime = currenttime;             paintingcomponent.repaint();             try {                 thread.sleep(10);             } catch (interruptedexception e) {                 thread.currentthread().interrupt();                 return;             }         }         projectile = null;         paintingcomponent.repaint();     }      projectile getprojectile() {         return projectile;     } }  class projectile {     private final point2d acceleration = new point2d.double(0, -9.81 * 0.1);     private final point2d position = new point2d.double();     private final point2d velocity = new point2d.double();      public point2d getposition() {         return new point2d.double(position.getx(), position.gety());     }      public void setposition(point2d point) {         position.setlocation(point);     }      public void setvelocity(point2d point) {         velocity.setlocation(point);     }      void performtimestep(double dt) {         scaleaddassign(velocity, dt, acceleration);         scaleaddassign(position, dt, velocity);     }      private static void scaleaddassign(point2d result, double factor, point2d addend) {         double x = result.getx() + factor * addend.getx();         double y = result.gety() + factor * addend.gety();         result.setlocation(x, y);     } }  class projectileshooterpanel extends jpanel {     private final projectileshooter projectileshooter;      public projectileshooterpanel(projectileshooter projectileshooter) {         this.projectileshooter = projectileshooter;     }      @override     protected void paintcomponent(graphics gr) {         super.paintcomponent(gr);         graphics2d g = (graphics2d) gr;         projectile projectile = projectileshooter.getprojectile();         if (projectile != null) {             g.setcolor(color.red);             point2d position = projectile.getposition();             int x = (int) position.getx();             int y = getheight() - (int) position.gety();             ellipse2d.double gerd = new ellipse2d.double(x - 01, y - 10, 20, 20);             g.draw(gerd);             // g.filloval(x-01, y-10, 20, 20);         }         rectangle hadaf1 = new rectangle(450, 450, 50, 50);         rectangle hadaf2 = new rectangle(500, 450, 50, 50);         rectangle hadaf3 = new rectangle(475, 400, 50, 50);         g.draw(hadaf1);         g.draw(hadaf2);         g.draw(hadaf3);     } } 

i thankful.


Comments