0

I want to draw rectangle by hline and vline in Java. I faced some problem to draw it, I don't know exactly, but I think it's at hline1 and vline1 methods.

There is no error, just a problem in algorithm within the parameter.

Here is the code ..

 package hw1;
 import java.awt.*;
 import java.awt.geom.*;
 import java.awt.event.*;

 public class main extends Frame {

Graphics2D g2d;
main()
{
    addWindowListener(new hw1.main.MyFinishWindow());
}

public class MyFinishWindow extends WindowAdapter
{
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
}

public void paint (Graphics g)
{
    g2d=(Graphics2D)g;

    hline(0,40,250,40);
    vline(250,40 , 250 , 80);
    hline1(250,80,0,80);
    vline1(0,80 , 0 , 40);

}

public void hline(int x1,int y1 , int x2 , int y2)
{
    for(int x=x1 ; x<=x2 ; x++)
        putpixel(x,y1,Color.blue);
}

public void vline(int x1 ,int y1 , int x2 , int y2 )
{
    for(int y=y1;y<=y2;y++)
        putpixel(x1,y,Color.blue);
}

 public void hline1(int x1,int y1 , int x2 , int y2)
{
    for(int x=x1 ; x<=x2 ; x++)
        putpixel(x,x1,Color.blue);
}

public void vline1(int x1 ,int y1 , int x2 , int y2 )
{
    for(int y=y1;y<=y2;y++)
        putpixel(x1,y,Color.blue);
}

public void putpixel(int x , int y , Color c)

{
    g2d.setColor(c);
    g2d.drawLine(x, y, x, y);
}

 public void putpixel(int x , int y , Color c , int rad)
 {
     g2d.setColor(c);
     if(rad>4) rad=4;
     if(rad<=0) rad=1;
     g2d.drawOval(x-rad/2, y-rad/2, rad, rad);
 }


public static void main(String[] args) {
    // TODO code application logic here

    main f=new main();
    f.setTitle("Computer Graphics:Java 2D prpgram");
    Dimension screenSize=
            Toolkit.getDefaultToolkit().getScreenSize();
    int width=(int) screenSize.getWidth();
    int height=(int) screenSize.getHeight();
    f.setSize(width, height);
    f.setVisible(true);
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Oct 01 '13 at 10:11
  • 1
    *"I want to draw rectangle by `hline` and `vline` in Java."* BTW - why that way particularly, as opposed to the (now 2) other good ways to draw a box? If it is 'specified by teacher', it might be best to make that clear. Programmers have a tendency to 'reach for the easiest tool' when programming, and the way you are going about it is *not* the easiest way. – Andrew Thompson Oct 01 '13 at 10:17

3 Answers3

2

The main issue is you always increment the loop variables - without taking into account which boundary value is bigger than the other...

   vline1(0,80 , 0 , 40);

This will start y from 80, and increment it until it gets to 40...

This at least does what it should

public void vline(int x1 ,int y1 , int x2 , int y2 )
{
    //use min and max
    for(int y=Math.min(y1, y2);y<=Math.max(y1,y2);y++) { //always use brackets!!!
        putpixel(x1,y,Color.blue);
    }
}

Also, be sure to implement this for the hline() too...

Other issues

  • you don't need two functions for the vertical lines, and two for the horizontal. The purpose of methods is reusing code - if you want to preserve this structure for some reason unknown to us, use 1 function for the horizontal line, and one for the vertical.
  • You shouldn't have any unused parameters in a method.

    hline(y, x1, x2)
    vline(x, y1, y2)
    

    would be totally enough.

  • Not to mention, taking this to a next level: it would be great to have 1 method for all kinds of lines: and that is the drawLine(), as @ling.s pointed out. And that handles all sorts of lines properly...

Moral of the story: know your API, and don't reinvent the wheel (unless this is for school, where the assignment is to reinvent it...)

ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • the solution must be without any extra functions , and i think the problem is in putpixel fun at hline1 and vline1 , how exactly to call putpixel fun in it. may be replace x1 with y , like this. – Khalil Shawahin Oct 01 '13 at 13:10
1

Try drawLine()

public void paint (Graphics g)
{
g2d=(Graphics2D)g;

g.drawLine(0,40,250,40);
g.drawLine(250,40 , 250 , 80);
g.drawLine(250,80,0,80);
g.drawLine(0,80 , 0 , 40);

}
Linga
  • 10,379
  • 10
  • 52
  • 104
0

As a side note, you can use java.awt.Polygon.

  Polygon p;

   p = new Polygon();
   p.addPoint(10, 10);
   p.addPoint(100,10);       
   p.addPoint(100,100);
   p.addPoint(10,100);

 .....

  public void paintComponent(Graphics g){
    super.paintComponent(g);
    setBackground(Color.white);
    g.fillPolygon(p);   
}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225