java - GUI Using JFrame, & JPanel drawing custom shapes -


pretty create shape class, rectangle, circle, triangle extending shape, , square class extending circle. have code working main class, i'm having tough time converting gui because i'm not sure how number 3 make come , how make g.drawoval(with given x,y & radius) , draw triangle(given x,y, base , height).

  1. project6 class have extend jframe class
  2. project6 constructor have set gui window.
  3. a new abstract method: public void display(graphics g); should added base , derived classes.
  4. a custom jpanel must set paintcomponent method
  5. the new display(graphics g) method have draw shapes on gui window , called loop in paintcomponent method.

import javax.swing.*; import java.awt.*; import javax.swing.jframe; import javax.swing.jpanel;  public class project6 extends jframe {     private shape [] thearray = new shape[100];   public static void main (string [] args) {  project6 tpo = new project6(); tpo.run(); }  public void run () { int count = 0;  thearray[count++] = new circle(20, 20, 40); thearray[count++] = new triangle(70, 70, 20, 30); thearray[count++] = new rectangle(150, 150, 40, 40); thearray[count++] = new square(100, 100, 50, 75);  (int = 0; < count; ++ ) {        thearray[i].display();                }    int offset = 0; double totalarea = 0.0; while (thearray[offset] != null) {              totalarea = totalarea + thearray[offset].area();       offset++; }  system.out.println("the total area " + offset + " shape objects " + totalarea); }  public project6() { jframe frame = new jframe(); frame.setsize(800, 700);                            frame.settitle("shapes: circle, triangle, rectangle, square"); frame.setlocationrelativeto(null);                 //center frame frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); }    public static class mypanel extends jpanel {    public static jpanel showjpanel(graphics g) {    panel = new mypanel();    return panel;    }    @override   public void paintcomponent(graphics g) {   super.paintcomponent(g);   for(int = 0; < thearray.length && thearray[i] != null; i++) {   thearray[i].display(); 

do add @ end of each of classes? i.e. circle, square, triangle, rectangle class?

 @override  public void draw(graphics g) {   g.drawrect(getxpos(), getypos(), width, height);   } 

i can't change way array set up, isn't supposed class extends jframe?

 public project6() {  jframe frame = new jframe();  frame.setsize(800, 700);                             frame.settitle("shapes: circle, triangle, rectangle, square");  frame.setlocationrelativeto(null);                 //center frame  frame.setdefaultcloseoperation(jframe.exit_on_close);  frame.setvisible(true);  }   

i'm new gui little hard do, work drawing shapes? error saying nonstatic method get() cant referenced static context

class newpanel extends jpanel { @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawline(triangle.getxpos(), 0, 0, triangle.getypos()); g.drawline(triangle.getxpos(), 0, triangle.getxpos, triangle.getypos()); g.drawline(triangle.getxpos(), triangle.getypos, 0, triangle.getypos()); g.drawrect(rectangle.getxpos(), rectangle.getypos(), rectangle.getwidth(), rectangle.getheight()); g.drawrect(square.getxpos(), square.getypos(), square.getwidth(), square.getheight()); g.drawoval(circle.getxpos(), circle.getypos(), circle.getradius(), 10);  for(int = 0; < thearray.length && thearray[i] != null; i++) { thearray[i].display(); } }  

  1. your class extends jframe never displayed
  2. you should draw shapes in paintcomponent method of jpanel, 1 added jframe.
  3. i use arraylist<shape>, not array, since way i'd able add many or few shapes collection , not have worry null items.
  4. i'd iterate through collection in paintcomponent method override , draw each shape using graphics2d object.
  5. regarding last question, "do add @ end of each of classes? ie(circle, square, triangle, rectangle class?..." no, there's no need "draw" method since you'll using paintcomponent method drawing.

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -