3

so i'm new in stackoverflow.

I am about to create a line, a triangle anything, but i'm just focusing on the line and in a good Object orient Programming.

So i create the class Point2D:

    package draw;

/**
 *
 * @author Pedro
 */
public class Point2D {
    private int x,y;

    // Construtores
    public Point2D(){
        this(0,0);
    }

    public Point2D(int x, int y){
        this.x=x;
        this.y=y;
    }

    // Set's e Get's
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

Later i create the class line using the class point2D to get the starting point and final point

    package draw;

/**
 *
 * @author Pedro
 */
public class Linha extends Figura{
    private Point2D pinicial;
    private Point2D pfinal;

    //construtores    
    public Linha(int xinicial, int yinicial, int xfinal, int yfinal){
        pinicial=new Point2D(xinicial, yinicial);
        pfinal=new Point2D(xfinal, yfinal);
    }
    public Linha(Point2D pinicial, Point2D pfinal){
        this.pinicial=pinicial;
        this.pfinal=pfinal;
    }
    //Get's e Set's
    public Point2D getPinicial() {
        return pinicial;
    }
    public void setPinicial(Point2D pinicial) {
        this.pinicial = pinicial;
    }
    public Point2D getPfinal() {
        return pfinal;
    }
    public void setPfinal(Point2D pfinal) {
        this.pfinal = pfinal;
    }

}

And then i created a Jframe with a button called "line" and put it a panel inside the jFrame that is the place where its going to get the line draw.

The problems is ... I dont know how to draw the line or how should i cal it.

Can you help me?

Pedro Nunes
  • 402
  • 3
  • 11

2 Answers2

4

simply, in your JPanel class ovverride paintComponent() method:

JPanel panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g)
        g.drawLine(x1, y1, x2, y2);
    }
}

Where x1, y1, x2, and y2, are the cords of your line. If you ONLY want it to draw line AFTER the button is pressed, create a global boolean variable, in your main class, and when the button is pressed, set it to true, then when you create your JPanel do:

JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            if (myBoolean) {
                super.paintComponent(g)
                g.drawLine(x1, y1, x2, y2);
            }
        }
    }
Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • 2
    1+ but please don't forget to also remind the OP to call the super's `paintComponent(g)` method inside of the `paintComponent(Graphics g)` override in your JPanel extended class – Hovercraft Full Of Eels Jun 14 '14 at 16:57
  • 2
    One more advice, being, keeping the access specifier the same, as much as possible for the overridden methods. Here it is `protected` and not `public` :-) – nIcE cOw Jun 14 '14 at 17:09
  • 2
    Also, you need to override the `getPreferredSize()` method of the panel otherwise the size will be (0, 0) and the panel may not appear depending on the layout manager being used. – camickr Jun 14 '14 at 18:21
  • @camickr Can you please edit my answer, and add that? I don't understand what you mean, and I never had problems with getPreferredSize() before. Thanks. – Victor2748 Jun 14 '14 at 18:24
  • 1
    @Victor2748, try adding the custom component to the frame using BorderLayout.NORTH. The NORTH respects the height of the component. Since the height is 0, there is nothing to paint. If you add the component to the CENTER of the frame, then yes the painting will work because the component is automatically resized to take all the space available to the frame. – camickr Jun 14 '14 at 20:25
1

Though the advice given in the other answer is too good, but just couldn't stop myself, from adding a word or two to the same, like, in order for the painting to take place, you need to call repaint(), from inside the actionPerformed attached to JButton.

As already stated by @camickr, the use of getPreferredSize() inside the extended class for Drawing, that will provide a valid staging area, where the drawing needs to be done. This example might can help too in this direction.

Moreover, in case you wanted to keep all the lines which have been drawn on the Board so far intact, then you can simply store them in a List and iterate on this List to draw them all again, whenever a new line is to be drawn.

A simple example is as follows:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;

public class LineExample {

    private DrawingBoard board;
    private JButton drawLineButton;

    private Random random;

    private ActionListener buttonAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int width = board.getWidth();
            int height = board.getHeight();
            Line line = new Line(random.nextInt(width),
                                random.nextInt(height),
                                random.nextInt(width),
                                random.nextInt(height));
            board.setValues(line);
        }    
    };

    public LineExample() {
        random = new Random();
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Drawing Lines Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout(5, 5));
        board = new DrawingBoard(400, 400);
        contentPane.add(board, BorderLayout.CENTER);

        drawLineButton = new JButton("LINE");
        drawLineButton.addActionListener(buttonAction);
        contentPane.add(drawLineButton, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new LineExample().displayGUI();
            }    
        };
        EventQueue.invokeLater(runnable);
    }
}

class DrawingBoard extends JPanel {

    private int width;
    private int height;

    private  List<Line> lines;

    public DrawingBoard(int w, int h) {
        width = w;
        height = h;
        lines = new ArrayList<Line>();
    }

    public void setValues(Line line) {
        lines.add(line);
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }    

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            int xs = line.getXStart();
            int ys = line.getYStart();
            int xe = line.getXEnd();
            int ye = line.getYEnd();
            g.drawLine(xs, ys, xe, ye);
        }
    }    
}

class Line {

    private Point startPoint;
    private Point endPoint;

    public Line(int xs, int ys, int xe, int ye) {
        startPoint = new Point(xs, ys);
        endPoint = new Point(xe, ye);
    }

    public int getXStart() {
        return startPoint.getX();
    }

    public int getYStart() {
        return startPoint.getY();
    }

    public int getXEnd() {
        return endPoint.getX();
    }

    public int getYEnd() {
        return endPoint.getY();
    }
}

class Point {

    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143