swing - Java Draw Lines in one Frame -


i try simple thing.. @ class main draw 2 lines coordinate system.. , @ class userpaint draw 1 line x1 y1 x2 y2 (already initialised). problem 3 lines (the coordinate system , x1y1x2y2 line) not in same window/frame. compiler creates 2 windows... how can fix that?

main class:

import static javax.swing.jframe.exit_on_close; import java.awt.*; import javax.swing.*;  public class main extends jframe {      @override     public void paint(graphics g) {         super.paint(g);         g.drawline(20, 80, 20, 200);         g.drawline(20, 200, 140, 200);     }      public main(string title){         super(title);         setsize(800, 600);         setdefaultcloseoperation(exit_on_close);         setvisible(true);     }      public static void main(string[] args) {         main main = new main("graph");         userpaint = new userpaint();     } } 

userpaint class:

import java.awt.*; import javax.swing.*;  public class userpaint extends jframe {     int x1, y1, x2, y2;      @override     public void paint(graphics g) {         super.paint(g);         g.drawline(x1, y1, x2, y2);     }      public userpaint(){         //gives 4 numbers points drawline. assume x1,y1,x2,y2 given scanner.. im gonna initialize         x1 = 200;         y1 = 200;         x2 = 300;         y2 = 300;         setsize(800, 600);         setvisible(true);     } } 

  1. don't paint directly jframe, rather paint jpanel can add the content pane of jframe via jframe's add method
  2. painting in (1) should done overriding paintcomponent method (not paint method).

for example:

public class painter extends jpanel{      public painter(){      }      @override     public void paintcomponent(graphics g){         super.paintcomponent(g);         g.drawline(20, 80, 20, 200);         g.drawline(20, 200, 140, 200);         g.drawline(x1, y1, x2, y2);     } }  ... jframe frame = new jframe("title"); frame.add(new painter()); 

draw 2 lines coordinate system

you mention coordinate system, may wish offset x1..y2 values of coordinate system drawn line falls within bounds of axis. example:

        g.drawline(20, 80, 20, 200);//y-axis         g.drawline(20, 200, 140, 200);//x-axis         g.drawline(x1 + 20, 200 - y1, x2 + 20, 200 - y2);//offset coordinate system 

the above offsets x-values position of of x-axis, , y-values position of y-axis (negatively plot not inverted) - assuming these values not offset already, , pixel positions relative bounds of axis.

one final note: class names should uppercase


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -