java - Trying to figure out why my graphics program is not working -


i cannot figure out why graphics program's render function not displaying rectangle. also, if change bufferstrategy '3' funky behavior. currently, project has 2 main classes on called main , second called universaljframe. universaljfame class should called display, @ least can think of being display. please keep in mind still new java programming.

public class main extends canvas implements runnable{      public int w = 200;     public int h = 200;     public string t = "hello";     private boolean running = false;       private universaljframe frame;     private thread thread;      private bufferstrategy bs;     private graphics g;      private pausetest pause;      public void run(){         system.out.println("run method");          while(running){             render();             tick();             pause.pause();         }          stop();     }      public synchronized void  start(){         if(running) { return; }          system.out.println("starting main program");         running = true;         thread = new thread(this);         thread.start();     }      public synchronized void stop(){         if(!running) { return; }          system.out.println("stopping");         try {             thread.join();         } catch (interruptedexception e) {             e.printstacktrace();         }     }      public void render(){         bs = frame.getcanvas().getbufferstrategy();         if(bs == null){             frame.getcanvas().createbufferstrategy(2);             return;         }          g = bs.getdrawgraphics();          //draw here          g.setcolor(color.green);         g.fillrect(10, 10, 10, 10);          //end draw          bs.show();         g.dispose();      }      public void tick(){      }      public main(){         frame = new universaljframe(h, w, t, this);         pause = new pausetest();         pause.setduration(500000);         start();     }      public static void main(string args[]){         new main();          system.out.println("running main program");     } }  public class universaljframe extends canvas {      private static final long serialversionuid = 1l;      private jframe jframe;     private canvas canvas;      int height = 200; int width = 200;     string title = "";     main obj;       public universaljframe(int height, int width, string title, main obj){         this.height = height;         this.width = width;         this.obj = obj;         this.title = title;          init();     }      public canvas getcanvas(){         return canvas;     }      private void init(){         jframe = new jframe(title);         jframe.setsize(new dimension(width, height));         jframe.setdefaultcloseoperation(jframe.exit_on_close);          jframe.setresizable(false);         jframe.add(obj);         jframe.setlocationrelativeto(null);          jframe.setvisible(true);          canvas = new canvas();         canvas.setpreferredsize(new dimension(width, height));         canvas.setmaximumsize(new dimension(width, height));         canvas.setminimumsize(new dimension(width, height));          jframe.add(canvas);         jframe.pack();      }  } 

you're using canvas, old awt class.

here's fun example of simple graphics application using java swing.

moving eyes

the eye balls follow cursor around drawing panel.

here's code. can use jframe , drawing jpanel base graphics application.

package com.ggl.testing;  import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.point; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities;  public class movingeyes implements runnable {      private static final int drawingwidth = 400;     private static final int drawingheight = 400;     private static final int eyeballheight = 150;     private static final int eyeballwidthmargin = 125;     private static final int eyeballouterradius = 50;     private static final int eyeballinnerradius = 20;      private drawingpanel drawingpanel;      private eye[] eyes;      private jframe frame;      public static void main(string[] args) {         swingutilities.invokelater(new movingeyes());     }      public movingeyes() {         this.eyes = new eye[2];         this.eyes[0] = new eye(new point(eyeballwidthmargin, eyeballheight));         this.eyes[1] = new eye(new point(drawingwidth - eyeballwidthmargin,                 eyeballheight));     }      @override     public void run() {         frame = new jframe("moving eyes");         frame.setdefaultcloseoperation(jframe.exit_on_close);          drawingpanel = new drawingpanel();         frame.add(drawingpanel);          frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      public class drawingpanel extends jpanel {          private static final long serialversionuid = -2977860217912678180l;          public drawingpanel() {             this.addmousemotionlistener(new eyeballlistener());             this.setbackground(color.white);             this.setpreferredsize(new dimension(drawingwidth, drawingheight));         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);              g.setcolor(color.black);              (eye eye : eyes) {                 drawcircle(g, eye.getorigin(), eyeballouterradius);                 fillcircle(g, eye.geteyeballorigin(), eyeballinnerradius);             }         }          private void drawcircle(graphics g, point origin, int radius) {             g.drawoval(origin.x - radius, origin.y - radius, radius + radius,                     radius + radius);         }          private void fillcircle(graphics g, point origin, int radius) {             g.filloval(origin.x - radius, origin.y - radius, radius + radius,                     radius + radius);         }      }      public class eye {         private final point origin;         private point eyeballorigin;          public eye(point origin) {             this.origin = origin;             this.eyeballorigin = origin;         }          public point geteyeballorigin() {             return eyeballorigin;         }          public void seteyeballorigin(point eyeballorigin) {             this.eyeballorigin = eyeballorigin;         }          public point getorigin() {             return origin;         }      }      public class eyeballlistener extends mousemotionadapter {          private final double eyeballdistance = eyeballouterradius                 - eyeballinnerradius - 5;          @override         public void mousemoved(mouseevent event) {             point p = event.getpoint();             (eye eye : eyes) {                 point origin = eye.getorigin();                 double theta = math.atan2((double) (p.y - origin.y),                         (double) (p.x - origin.x));                 int x = (int) math.round(math.cos(theta) * eyeballdistance)                         + origin.x;                 int y = (int) math.round(math.sin(theta) * eyeballdistance)                         + origin.y;                 eye.seteyeballorigin(new point(x, y));             }              drawingpanel.repaint();         }      }  } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -