java - Component Paint method not painting to middle of JPanel? -


alright, have jpanel paint method below, , works fine @ first, when jpanel resized(is in jframe) doesn't paint center of frame.

 @override     protected void paintcomponent(graphics g) {         graphics2d graphics = (graphics2d) g;         dimension dimension = frame.getsize();         insets insets = getinsets();         int w = (int) ((dimension.getwidth() - insets.left - insets.right) / 2);         int h = (int) ((dimension.getheight() - insets.top - insets.bottom) / 2);         graphics.translate(w, h);         graphics.drawstring("origin", 0, 0);         double y = 0;         (double x = -25; x <= 25; x += .01) {             y = -math.pow(x, 2);             int gx = (int) x;             int gy = (int) y;             system.out.println("parabola coordinate: " + x + ", " + y);             g.drawrect(gx, gy, 0, 0);         }     } 

change paintcomponent more like

@override protected void paintcomponent(graphics g) {     // create copy of graphics context...     graphics2d graphics = (graphics2d) g.create();     // don't rely on frame, rely on own components size...     //dimension dimension = frame.getsize();     insets insets = getinsets();     int w = (int) ((getwidth() - insets.left - insets.right) / 2);     int h = (int) ((getheight() - insets.top - insets.bottom) / 2);     graphics.translate(w, h);     graphics.drawstring("origin", 0, 0);     double y = 0;     (double x = -25; x <= 25; x += .01) {         y = -math.pow(x, 2);         int gx = (int) x;         int gy = (int) y;         system.out.println("parabola coordinate: " + x + ", " + y);         // using "un-translated" reference, don't know if deliberate         graphics.drawrect(gx, gy, 0, 0);     }     // dispose of copy , safe resources...     graphics.dispose(); } 

make sure you're using suitable layout manager!

enter image description hereenter image description here

import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.insets; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class badpaint21 {      public static void main(string[] args) {         new badpaint21();     }      public badpaint21() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.setlayout(new borderlayout());                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          public testpane() {         }          @override         public dimension getpreferredsize() {             return new dimension(200, 200);         }          @override         protected void paintcomponent(graphics g) {             graphics2d graphics = (graphics2d) g.create(); //            dimension dimension = frame.getsize();             insets insets = getinsets();             int w = (int) ((getwidth() - insets.left - insets.right) / 2);             int h = (int) ((getheight() - insets.top - insets.bottom) / 2);             graphics.translate(w, h);             graphics.drawstring("origin", 0, 0);             double y = 0;             (double x = -25; x <= 25; x += .01) {                 y = -math.pow(x, 2);                 int gx = (int) x;                 int gy = (int) y;                 system.out.println("parabola coordinate: " + x + ", " + y);                 graphics.drawrect(gx, gy, 0, 0);             }             graphics.dispose();         }     } } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -