Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help me!!! I'm need your help

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me!!! I'm need your help

    I'm doing a exercise for dragging mouse to draw a line. I found a code to do this, but I don't know something in the code. example array Shape in the code. please help me know it. Thanks very much!

    The code:
    import java.awt.AlphaComposite;
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;

    import javax.swing.JComponent;
    import javax.swing.JFrame;

    public class DrawingBoardWithMatrit extends JFrame {

    public static void main(String[] args) {
    new DrawingBoardWithMatrit();
    }

    public DrawingBoardWithMatrit() {
    this.setSize(300, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.add(new PaintSurface(), BorderLayout.CENTER);
    this.setVisible(true);
    }

    private class PaintSurface extends JComponent {
    ArrayList<Shape> shapes = new ArrayList<Shape>();

    Point startDrag, endDrag;

    public PaintSurface() {
    this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    startDrag = new Point(e.getX(), e.getY());
    endDrag = startDrag;
    repaint();
    }

    public void mouseReleased(MouseEvent e) {
    Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(),
    e.getY());
    shapes.add(r);
    startDrag = null;
    endDrag = null;
    repaint();
    }
    });

    this.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
    endDrag = new Point(e.getX(), e.getY());
    repaint();
    }
    });
    }

    private void paintBackground(Graphics2D g2) {
    g2.setPaint(Color.LIGHT_GRAY);
    for (int i = 0; i < getSize().width; i += 10) {
    Shape line = new Line2D.Float(i, 0, i, getSize().height);
    g2.draw(line);
    }

    for (int i = 0; i < getSize().height; i += 10) {
    Shape line = new Line2D.Float(0, i, getSize().width, i);
    g2.draw(line);
    }

    }

    public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    paintBackground(g2);
    Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN,
    Color.RED, Color.BLUE, Color.PINK };
    int colorIndex = 0;

    g2.setStroke(new BasicStroke(2));
    g2.setComposite(AlphaComposite.getInstance(AlphaCo mposite.SRC_OVER,
    0.50f));

    for (Shape s : shapes) {
    g2.setPaint(Color.BLACK);
    g2.draw(s);
    g2.setPaint(colors[(colorIndex++) % 6]);
    g2.fill(s);
    }

    if (startDrag != null && endDrag != null) {
    g2.setPaint(Color.LIGHT_GRAY);
    Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x,
    endDrag.y);
    g2.draw(r);
    }
    }

    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
    return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2),
    Math.abs(x1 - x2), Math.abs(y1 - y2));
    }
    }
    }

    --- Update ---

    please help me

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please help me!!! I'm need your help

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the link I've provided.

    What do you need help with? Ask specific questions. If you're getting errors and want help with those, post the errors, the whole thing, just as it appears at your end.

    If you've copied this example directly from the web or a book, there are better, more recent examples out there. What you've posted does not reflect current best practices.

    If you have questions about the classes you're using, you can find the APIs for core Java classes on the web. Search something like "java shape doc," and the result you need should be in the top 2 or 3 results.