java - I need help not using brute force when using JFrames and DrawWindows -


i making basic hangman game , first time using jframes , drawwindows in java. here confusion lies:

//this initializes window , panel global variables:

jframe window = new jframe("let's play hangman!"); drawwindow panel = new drawwindow(); 

//this sets window , adds panel it:

public hangmantwo() {     window.setdefaultcloseoperation(jframe.exit_on_close);     window.setbackground(color.white);     window.setsize(500, 500);     window.add(panel);     window.setvisible(true); 

}

//this next parts draws head onto window:

public class drawwindow extends jpanel {     public void paintcomponent(graphics g) {     super.paintcomponent(g);     g.setcolor(color.black);     g.drawrect(50, 50, 75, 400);     g.setcolor(color.lightgray);     g.fillrect(50, 50, 75, 400);     g.drawrect(100, 50, 150, 50);     g.fillrect(100, 50, 250, 50);       //draws noose , head     g.setcolor(color.black);     g.fillrect(250, 100, 1, 75);     g.drawoval(220, 175, 60, 60);     g.filloval(220, 175, 60, 60); } } 

now, when person gets second incorrect guess, wanted able add body onto head. however, when try add both on:

...     } else if (score == 2) {         printoutscore(2, 4);         drawhead head = new drawhead();         drawbody body = new drawbody();         window.add(head);         window.add(body);         window.setvisible(true);     } else if (score == 3) { ... 

it shows body , whole head disappears. because of this, unfortunately, when draw body, have redraw head, can imagine, when have write 10 times easy level (which includes head, body, left arm, right arm, left leg, right leg, top hat, pipe, tie, , shot of brandy) code getting ridiculously long. function class draw body looks this: (the drawhead plus body code):

public class drawbody extends jpanel {     public void paintcomponent(graphics g) {         super.paintcomponent(g);          /*draws wood structure start         g.setcolor(color.black);         g.drawrect(50, 50, 75, 400);         g.setcolor(color.lightgray);         g.fillrect(50, 50, 75, 400);         g.drawrect(100, 50, 150, 50);         g.fillrect(100, 50, 250, 50);          //draws noose , head         g.setcolor(color.black);         g.fillrect(250, 100, 1, 75);         g.drawoval(220, 175, 60, 60);         g.filloval(220, 175, 60, 60);          //draws body         g.drawrect(245, 235, 10, 120);         g.fillrect(245, 235, 10, 120);         } } 

can please me figure out how smarter? can't figure out how call drawhead in drawbody , on. appreciated!!

saludos!

the basic problem you're having fact jframe using borderlayout, which, default, allow single component occupy each of it's 5 possible layout positions. means, when add new component default position, component use exist there "hidden"

you have 2 options (you have more, based on following concepts...)

one...

you make use of different layout manager , paint each body part on it's own panel...

for example...

enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here

the immediate issue in order layout components, need ability "hide" painted until need it. is, each body part panel must added frame first , it's content "turned on" or "off" required

import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class hangman03 {      public static void main(string[] args) {         new hangman03();     }      public hangman03() {         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 gallospane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public static class gallospane extends jpanel {          private headpane headpane;         private bodypane bodypane;         private armpane rightarm;         private armpane leftarm;         private legpane rightleg;         private legpane leftleg;          private int score = 0;          public gallospane() {             setlayout(new borderlayout());             jpanel personpane = new jpanel(new gridbaglayout());             gridbagconstraints gbc = new gridbagconstraints();             gbc.gridx = 0;             gbc.gridy = 0;             gbc.gridwidth = gridbagconstraints.remainder;             gbc.anchor = gridbagconstraints.south;             gbc.fill = gridbagconstraints.horizontal;              headpane = new headpane();             personpane.add(headpane, gbc);              gbc.fill = gridbagconstraints.none;             gbc.gridwidth = 1;             gbc.gridx = 0;             gbc.gridy = 1;             gbc.anchor = gridbagconstraints.northeast;             rightarm = new armpane(side.right);             personpane.add(rightarm, gbc);              gbc.gridx = 1;             gbc.anchor = gridbagconstraints.north;             bodypane = new bodypane();             personpane.add(bodypane, gbc);              gbc.gridx = 2;             gbc.anchor = gridbagconstraints.northwest;             leftarm = new armpane(side.left);             personpane.add(leftarm, gbc);              gbc.gridx = 0;             gbc.gridy = 2;             gbc.anchor = gridbagconstraints.northeast;             rightleg = new legpane(side.right);             personpane.add(rightleg, gbc);              gbc.gridx = 2;             gbc.anchor = gridbagconstraints.northwest;             leftleg = new legpane(side.left);             personpane.add(leftleg, gbc);              add(personpane);              jbutton btn = new jbutton("next");             add(btn, borderlayout.south);              btn.addactionlistener(new actionlistener() {                 @override                 public void actionperformed(actionevent e) {                     score++;                     if (score == 1) {                         headpane.setpainted(true);                     } else if (score == 2) {                         leftarm.setpainted(true);                     } else if (score == 3) {                         rightarm.setpainted(true);                     } else if (score == 4) {                         bodypane.setpainted(true);                     } else if (score == 5) {                         leftleg.setpainted(true);                     } else if (score == 6) {                         rightleg.setpainted(true);                     } else {                         headpane.setpainted(false);                         leftarm.setpainted(false);                         rightarm.setpainted(false);                         bodypane.setpainted(false);                         leftleg.setpainted(false);                         rightleg.setpainted(false);                         score = 0;                     }                 }             });          }      }      public enum side {          left, right;     }      public static class partpane extends jpanel {          private boolean painted;          public partpane() {             setopaque(false);         }          public void setpainted(boolean painted) {             this.painted = painted;             repaint();         }          public boolean ispainted() {             return painted;         }      }      public static class headpane extends partpane {          public static final int radius = 10;          @override         public dimension getpreferredsize() {             return new dimension(radius, radius);         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             if (ispainted()) {                 graphics2d g2d = (graphics2d) g.create();                 int x = (getwidth() - radius) / 2;                 int y = (getheight() - radius) / 2;                 g2d.drawoval(x, y, radius - 1, radius - 1);                 g2d.dispose();             }         }      }      public static class bodypane extends partpane {          public bodypane() {             setopaque(false);         }          @override         public dimension getpreferredsize() {             return new dimension(1, headpane.radius * 2);         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             if (ispainted()) {                 graphics2d g2d = (graphics2d) g.create();                 g2d.fillrect(0, 0, getwidth(), getheight());                 g2d.dispose();             }         }      }      public static class armpane extends partpane {          private side side;          public armpane(side side) {             super();             this.side = side;             setopaque(false);         }          @override         public dimension getpreferredsize() {             return new dimension(headpane.radius, headpane.radius);         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             if (ispainted()) {                 graphics2d g2d = (graphics2d) g.create();                 int x1 = 0;                 int y1 = 0;                 int x2 = 0;                 int y2 = 0;                  switch (side) {                     case left:                         x2 = getwidth() - 1;                         y2 = getheight() - 1;                         break;                     case right:                         x1 = getwidth() - 1;                         y1 = 0;                         y2 = getheight() - 1;                         break;                 }                  g2d.drawline(x1, y1, x2, y2);                 g2d.dispose();             }         }      }      public static class legpane extends armpane {          public legpane(side side) {             super(side);         }          @override         public dimension getpreferredsize() {             return new dimension(headpane.radius, headpane.radius * 2);         }      }  } 

now, mind, complicated , fraught problems , broken...

two

you use single component , paint directly it.

this is, far, simpler approach, gain complete control on how each part rendered.

import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class hangman031 {      public static void main(string[] args) {         new hangman031();     }      public hangman031() {         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 gallospane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public static class gallospane extends jpanel {          private personpane personpane;          private int score = 0;          public gallospane() {             setlayout(new borderlayout());              personpane = new personpane();             add(personpane);              jbutton btn = new jbutton("next");             add(btn, borderlayout.south);              btn.addactionlistener(new actionlistener() {                 @override                 public void actionperformed(actionevent e) {                     personpane.badguess();                 }             });          }      }      public static class personpane extends jpanel {          public static final int radius = 10;         public static final int head_height = radius;         public static final int body_height = head_height * 2;         public static final int arm_height = head_height;         public static final int arm_width = radius;         public static final int leg_height = head_height * 2;         public static final int leg_width = radius;         private int score = 0;          public personpane() {             setopaque(false);         }          @override         public dimension getpreferredsize() {             return new dimension(((arm_width + leg_width) * 2) + 1, head_height + body_height + leg_height);         }          public void badguess() {             score++;             if (score > 6) {                 score = 0;             }             repaint();         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             graphics2d g2d = (graphics2d) g.create();              dimension size = getpreferredsize();              int centerx = getwidth() / 2;             int y = ((getheight() - (size.height)) / 2);               switch (score) {                 case 6:                     int x = centerx;                     int ypos = y + head_height + body_height;                     g2d.drawline(x, ypos, x - leg_width, ypos + leg_height);                 case 5:                     x = centerx;                     ypos = y + head_height + body_height;                     g2d.drawline(x, ypos, x + leg_width, ypos + leg_height);                 case 4:                     x = centerx;                     ypos = y + head_height;                     g2d.drawline(x, ypos, x, ypos + body_height);                 case 3:                     x = centerx;                     ypos = y + head_height;                     g2d.drawline(x, ypos, x - arm_width, ypos + arm_height);                 case 2:                     x = centerx;                     ypos = y + head_height;                     g2d.drawline(x, ypos, x + arm_width, ypos + arm_height);                 case 1:                     x = centerx - (head_height / 2);                     ypos = y;                     g2d.drawoval(x, y, head_height, head_height);                 case 0:                     break;             }             g2d.dispose();         }                 } } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -