3

I'm supposed to draw this in Java

enter image description here

This is what I have so far:

DrawPanel.java

package drawpaneltest;

import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        int d = 0;
        while (d < 301) {
            g.drawLine(0, d, width, height);
            d += 15;
        }
    }
}

DrawPanelTest.java

package drawpaneltest;

import javax.swing.JFrame;

public class DrawPanelTest {

    public static void main(String[] args) {
        DrawPanel panel = new DrawPanel();
        JFrame application = new JFrame();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel); // add the panel to the frame 
        application.setSize(250, 250); // set the size of the frame
        application.setVisible(true); // make the frame visible 
    }
}

The above code sample currently displays the following:

Code sample window

What did I missed out? How should I add the curved line and the vertical line?

8
  • 2
    Hint: The "curved" line is not a curved line, it's actually edged. Commented Oct 13, 2015 at 15:07
  • There are no curved lines at all on this picture, just straight lines. Commented Oct 13, 2015 at 15:17
  • 6
    Here's how to draw that picture on paper wikihow.com/… Commented Oct 13, 2015 at 15:18
  • @TassosBassoukos right bottom corner seems like curved Commented Oct 13, 2015 at 15:28
  • 1
    @HRgiger it's a visual illusion :-D Commented Oct 13, 2015 at 21:03

2 Answers 2

1

Here's my approach:

package drawpaneltest;


import java.awt.Graphics;
 import javax.swing.JPanel;
 public class DrawPanel extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        int x1 = 0, y1 = 0,
            x2 = 0, y2 = height;
        while (y1 < height) {
            g.drawLine(x1, y1, x2, y2);
            y1+=15;                 //You should modify this if
            x2+=15;                 //it's not an equal square (like 250x250)
        }
    }
 }

And then it will do this: image

However in JPanel, coordinate (0,0) begin on the left top corner, not on left bottom corner as usual. And it still can not do well on a (m x n) squares, you have to do a lot more than this.

Sign up to request clarification or add additional context in comments.

Comments

0

You should also change the to coordinates (d replacing width):

g.drawLine(0, d, d, height);

4 Comments

Your solution will generate parallel lines.
That wouldn't draw lines to the side of the screen.
Oups, yes indeed. Thanks @Flown for noticing. Just corrected the line (hope I did not miss it this time).
@austinwernli, after my edit 3 min ago, it does reproduce the expected result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.