swing - Drawing two circles with different positions and colors in java -


i'm new java , want try graphic things it. want generate 2 circles 2 different colors , different positions. code:

paint class:

package de.test.pkg; import javax.swing.*;  public class paint {      public static void main(string[] args) throws exception{          jframe frame = new jframe("titel");         frame.setdefaultcloseoperation(jframe.exit_on_close);         circle d = new circle();         circle r = new circlered();         frame.add(d);         frame.add(r);         frame.setsize(600,200);         frame.setvisible(true);      }  } 

circle class

package de.test.pkg; import java.awt.*; import java.awt.geom.*; import javax.swing.*;   public class circle extends jpanel {      private double iconradius = 100;     private color defaultcolor = new color(89,104,99);      private int positionx = 0;     private int positiony = 0;      private ellipse2d iconbody = new ellipse2d.double(getpositionx(),getpositiony(),iconradius,iconradius);      public icon(){      }      public color getdefaultcolor() {         return defaultcolor;     }      public void setdefaultcolor(color defaultcolor) {         this.defaultcolor = defaultcolor;     }      public int getpositionx() {         return positionx;     }      public void setpositionx(int positionx) {         this.positionx = positionx;     }      public int getpositiony() {         return positiony;     }      public void setpositiony(int positiony) {         this.positiony = positiony;     }      public void paintcomponent(graphics g) {         graphics2d g2d = (graphics2d)g;         super.paintcomponent(g2d);         this.setbackground(new color(255,255,255));          g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);         g2d.setpaint(getdefaultcolor());         g2d.draw(iconbody);         g2d.fill(iconbody);     }  } 

circlered class

package de.design.pkg; import java.awt.color;  public class circlered extends circle {  private color defaultcolor = color.red; private int positionx = 120; private int positiony = 0;  public circlered() { }  public color getdefaultcolor() {     return defaultcolor; }  public void setdefaultcolor(color defaultcolor) {     this.defaultcolor = defaultcolor; }   public int getpositionx() {     return positionx; }  public void setpositionx(int positionx) {     this.positionx = positionx; }  public int getpositiony() {     return positiony; }  public void setpositiony(int positiony) {     this.positiony = positiony;     }  } 

the result circles have same positions.

my questions are:

  1. why have same positions?
  2. is way or there better solutions? want use class please don't gave me answers paint thing in main.
  3. is there better way hold position. maybe in array? how should setter , getter if want return array[position]?
  4. if want set position main function. how can this?

the result circles have same positions.

(1) why have same positions?

not really. result circlered displayed. problem here not painting, it's adding components container suitable layout manager. lines

circle d = new circle(); circle r = new circlered(); frame.add(d); frame.add(r); 

add r instead of d. because jframe uses borderlayout default , replacing center component d r line after. show point, add line

frame.setlayout(new gridlayout(1, 2)); 

enter image description here

(2) way or there better solutions? want use class please don't gave me answers paint thing in main.

it depends on aiming do. venture guess if want practice inheritance, better create abstract class general circle shared code , subclass concrete implementation , specific code various types. otherwise, can create single customizable circle class , instantiate 1 different parameters.

it's not practical approach in case because placement of circles (which jpanels) determined layout manager. painting determines location of painted shape in panel. better paint shapes on single big panel , not using multiple panels.

there few answers on site moving components around.

(3) there better way hold position. maybe in array? how should setter , getter if want return array[position]?

there 2 positions in design. 1 panels in frame, other shapes in panels.

for latter, use point or int x, y fields in class itself. getters , setters standard ones, setters control position (you need call repaint() though).

for first, determined layout manager , don't (shouldn't) control in pixel-prefect way. instruct layout manager "guidelines" , calculations you.

(4) if want set position main function. how can this?

again, depends on position talking about. see above.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -