Draw a line using Java applet

what should be the parameter ?

Page 1 of 1

3 Replies - 4637 Views - Last Post: 30 November 2010 - 12:14 PM Rate Topic: -----

#1 No Signal X  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 10

Draw a line using Java applet

Posted 30 November 2010 - 12:51 AM

hello everyone

I'm trying to learn how to draw in java, here's a program that takes the coordinates of 2 points & draw a line between them :

my problems

1- in initiating, how can i make x-axis & y-axis appear ?
2- I couldn't pass a parameter from the main to method paint, because variable Graphics. I didn't know what should it be ?


import java.awt.*;
import javax.swing.JApplet;
import javax.swing.JFrame;

public class LineDemo2D extends JApplet{

  public void init() {
    setBackground(Color.white);
    setForeground(Color.white);
  }


  public void paint(Graphics g, int x1, int y1, int x2,int y2) {

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.gray);

    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    g2.drawString("Line2D", x1, 250);

  }
   public static void main(String s[]) {
    JFrame f = new JFrame("ShapesDemo2D");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    JApplet applet = new LineDemo2D();
    f.getContentPane().add("Center", applet);
    applet.init();
    f.pack();
    f.setSize(new Dimension(300, 300));
    f.show();
  }
}



this is the main class

public class Test {
    public void main(String[]args){
        Scanner coor=new Scanner(System.in);
        LineDemo2D g = new LineDemo2D();
        int x1,y1,x2,y2 ;
        System.out.print("enter coordinates: ");
        x1=coor.nextInt();
        y1=coor.nextInt();
        x2=coor.nextInt();
        y2=coor.nextInt();

        
        g.paint( ???, x1, y1, x2, y2);

    }

}

This post has been edited by No Signal X: 30 November 2010 - 12:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Draw a line using Java applet

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Draw a line using Java applet

Posted 30 November 2010 - 07:23 AM

You'll need to do manual drawing of axes. You'd be much better off using an application than an applet. Do the drawing on a JPanel inner class, which will be able to access instance variables which will store your user-entered parameters
Was This Post Helpful? 0
  • +
  • -

#3 No Signal X  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 10

Re: Draw a line using Java applet

Posted 30 November 2010 - 10:21 AM

hey

I tried JPanel ,, but still don't know what should i pass in the parameter paint(Graphics g) from the main

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import java.util.Scanner;

/**
 *
 * @author No Signal X
 */
public class DrawLine extends JPanel{
    int x1, y1, x2, y2;
    public void paintComponent(Graphics g){
        
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        
        System.out.print("enter the coordinates: x1, y1, x2, y2");
        Scanner input=new Scanner(System.in);
        x1=input.nextInt();
        y1=input.nextInt();
        x2=input.nextInt();
        y2=input.nextInt();
        
        g.setColor(Color.BLACK);
        g.drawLine(x1,y2,x2,y2);

    }
}
 


and here's the main

import java.awt.Color;
import javax.swing.JFrame;
/**
 *
 * @author No Signal X
 */
public class Test {
    public static void main(String args[]){

        JFrame frame = new JFrame("Draw Line");

        System.out.println("Enter coordinates: x1, y1, x2,y2");
        
       DrawLine dl= new DrawLine();
       // dl.paintComponenet(Graphics g); <- what should i do here ?!
       dl.setBackground(Color.WHITE);
       frame.add(dl);
       frame.setSize(400, 400);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Draw a line using Java applet

Posted 30 November 2010 - 12:14 PM

Something like


import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class LineDrawer extends JFrame {
    private int x1;
    private int y1;
    private int x2;
    private int y2;

    private void setGui() {
        try {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            cp.add(new JPanel() {
                    public void paintComponent(Graphics g) {
                        g.drawLine(x1, y1, x2, y2);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setLineCoordinates(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        repaint();
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        LineDrawer f = new LineDrawer();

                        Scanner coor = new Scanner(System.in);
                        System.out.print("enter coordinates: ");
                        f.setLineCoordinates(coor.nextInt(), coor.nextInt(),
                            coor.nextInt(), coor.nextInt());
                        f.setGui();
                        f.setSize(400, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1