2d Graphics


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: 2d Graphics

  1. #1
    Join Date
    Jan 2003
    Posts
    8

    2d Graphics - with the code added!!

    Hi,
    Please see the following code which will ask the user how many Roads lead to a junction, and then draws a primitive "junction" on the screen.
    There are two things I want to do:

    i) On the screen, have a name at the end of each road/line, ie R1, R2, R3, R4 etc depending on how many roads there are

    ii) The tricky bit! I want the user to be able to manipulate the lines on the screen to shape the "junction" to their needs, ie be able to drag the lines into the shape of a T-Junction if 3 "roads" are entered.

    Any ideas on how to do this?

    Thanks



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Lines extends JFrame {

    private int startx, starty;
    static int numPoints;
    static String number;
    private JLabel statusBar;


    public Lines(){
    super("Lines");

    number = JOptionPane.showInputDialog("How many roads are there?");

    statusBar = new JLabel();
    getContentPane().add(statusBar, BorderLayout.SOUTH);

    setSize(400,400);
    show();
    }

    private void drawShape( Graphics g, int numPoints, int xOffset, int yOffset ){

    double factor = 2.0 * Math.PI / ( numPoints /*- 1*/ ) ;
    int [] x = new int[ numPoints ];
    int [] y = new int[ numPoints ];
    int radius = 100 ; // this will need changing...

    for (int i=0; i<numPoints; i++){
    x[i] = (int)( radius * Math.cos( factor * i ) ) + xOffset;
    y[i] = (int)( radius * Math.sin( factor * i ) ) + yOffset;

    System.out.println("x = " + x[i] + " y = " + y[i]);
    }

    for (int i = 0; i<numPoints; i++){
    g.drawLine(200,200,x[i], y[i]);
    }

    g.setColor(Color.red);
    //g.drawArc(100,100,200,200,0,360);

    // Draw polygon object with call to drawPolygon
    //Polygon poly = new Polygon(x,y,numPoints);
    //g.drawPolygon(poly);

    }

    public void paint( Graphics g ){
    numPoints = Integer.parseInt(number);
    startx = 200;
    starty = 200;

    drawShape( g, numPoints, startx, starty ) ;
    }

    public static void main(String args[]){

    Lines app = new Lines();

    app.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }
    );
    }
    }

  2. #2
    Join Date
    Oct 2002
    Posts
    66
    Ok, I hope I haven't got the wrong of the stick here:

    1. Use the Graphics method drawString(str, x, y) to output the String str at co-ords x and y.

    2. You have to create an on mouse down listener on the corner of each road. You then have to check for any mouse movement whilst the mouse button is down and whenever it is moved correct the saved co-ords of the two roads to fit that point and-redraw each time. That should make it look as if you are dragging the roads. Then on mouse up you can leave everything where it is and save it, or whatevery ou need to do.

    Hope that helps.
    Meethoss

  3. #3
    Join Date
    Jan 2003
    Posts
    8
    Hi,
    Thanks for the advice, not sure what u mean by putting a mouse down listener on the corner of each road though. I can put a mouse listener on the whole frame and find out what point the mouse is at, but not sure how it will know whether it is on a line or not!

  4. #4
    Join Date
    Oct 2002
    Posts
    66
    Sorry if I sounded a bit vague but I've not done a great deal using listeners and stuff so I know how to use them if I'm working on something at the time (using the API) but when I try and remember I only get VB thoughts in my head (being an ex-VB programmer)

    Basically you need to detect when someone holds their mouse down. Then you need to check to see if the co-ordinate they've clicked on is within an average 5 pixel radius of the end of a road(s) by comparing the x,y with the x,y of all of the roads. Then you need to check for movement and each time there is movement then you need to correct the co-ordinates of the road and re-draw the whole thing after wiping the screen. Once they've let go of the button it will then stop the whole action and everything should be where they want it (or they'll move it more).

    Another way would be to have a mouse click at the start and keep moving the roads until the mouse click again. This is, also, all assuming that you create the line by drawing a line from aXaY to bXbY and save those co-ordinates (in an array of Point's for instance).

    If you're doing it someother way then post me what you've got or tell me your system and I'll try and help you more with that.

    Hope that helps.
    Meethoss

  5. #5
    Join Date
    Jan 2003
    Posts
    8
    Thanks for the help....I thought the following might work.......but it keeps giving me an Array out of bounds error when I try it!! I know its a problem with the code between the stars but cant figure out what! I have changed the i in the x and y arrays to 2 because that seems to be the only point it is "working" from.
    Which leads me to another little problem!!

    My code at the end of the drawShape method wouldn't work the way I wanted it (ie the code commented out inside the for loop dealing with the circles array) I thought I'd be able to store the three seperate circles in an Array by using the three lines commented out instead of the other 3, so I could check to see if the mouse clicked on the three of them, but again, another error came up!! ( I'm not having much luck with arrays!!)

    I know there is an awful lot there, and you mightn't have time to help me, so its okay, but if u can I'd be very grateful!!!!!







    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Lines1 extends JFrame implements MouseListener, MouseMotionListener{

    private int startx, starty;
    static int numPoints;
    static String number;
    private JLabel statusBar;
    private int lastX, lastY;
    static Circle1 circles[];
    static Circle1 circle1;
    static int x[], y[];
    static Graphics g;

    public Lines1(){
    super("Lines1");

    number = JOptionPane.showInputDialog("How many roads are there?");

    addMouseListener(this);
    addMouseMotionListener(this);
    statusBar = new JLabel();
    getContentPane().add(statusBar, BorderLayout.SOUTH);
    circles = new Circle1[numPoints];

    setSize(400,400);
    show();
    }

    private void drawShape( Graphics g, int numPoints, int xOffset, int yOffset ){

    double factor = 2.0 * Math.PI / ( numPoints ) ;
    x = new int[ numPoints ];
    y = new int[ numPoints ];
    int radius = 100 ;

    for (int i=0; i<numPoints; i++){
    x[i] = (int)( radius * Math.cos( factor * i ) ) + xOffset;
    y[i] = (int)( radius * Math.sin( factor * i ) ) + yOffset;

    System.out.println("R" + (i+1) + "=> " + "x = " + x[i] + " y = " + y[i]);
    }

    for (int i = 0; i<numPoints; i++){
    g.drawLine(200, 200, x[i], y[i]);
    g.drawString("R" + (i+1), (x[i]+3), (y[i]+3));
    }

    //g.setColor(Color.red);
    //g.drawOval(100,100,200,200);

    for (int i = 0; i<numPoints; i++){
    //circles[i] = new Circle1();
    circle1 = new Circle1();
    //circles[i].setCircle(g,(x[i]-3),(y[i]-3),3);
    circle1.setCircle(g,(x[i]-3),(y[i]-3),3);
    //circles[i].draw(g);
    circle1.draw(g);

    }

    // Draw polygon object with call to drawPolygon
    //Polygon poly = new Polygon(x,y,numPoints);
    //g.drawPolygon(poly);

    }

    public void paint( Graphics g ){
    numPoints = Integer.parseInt(number);
    startx = 200;
    starty = 200;

    drawShape( g, numPoints, startx, starty ) ;
    }

    // MouseListener Event Handlers
    public void mouseClicked( MouseEvent e ){
    statusBar.setText( "Clicked at [" + e.getX() +
    ", " + e.getY() + "]" );
    }
    public void mousePressed( MouseEvent e ){
    statusBar.setText( "Pressed at [" + e.getX() +
    ", " + e.getY() + "]" );


    lastX = e.getX();
    lastY = e.getY();

    if ( circle1.isInside( lastX, lastY ) )
    System.out.println("fhdskj");
    }
    public void mouseReleased( MouseEvent e ){
    statusBar.setText( "Released at [" + e.getX() +
    ", " + e.getY() + "]" );

    }
    public void mouseEntered( MouseEvent e ){
    statusBar.setText( "Mouse in window" );
    }
    public void mouseExited( MouseEvent e ){
    statusBar.setText( "Mouse outside window" );
    }
    // MouseMotionListener event handlers
    public void mouseDragged( MouseEvent e ){

    /**********************************
    statusBar.setText( "Dragged at [" + e.getX() +
    ", " + e.getY() + "]" );

    System.out.println(x[2] + " " + y[2]);
    x[2] = e.getX();
    y[2] = e.getY();
    System.out.println(x[2] + " " + y[2]);

    g.drawLine(200, 200, x[2], y[2]);
    g.drawString("R" + (2+1), (x[2]+3), (y[2]+3));
    ************************************/

    }
    public void mouseMoved( MouseEvent e ){
    statusBar.setText( "Moved at [" + e.getX() +
    ", " + e.getY() + "]" );
    }

    public static void main(String args[]){

    Lines1 app = new Lines1();

    app.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }
    );
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center