Click here to Skip to main content
15,887,332 members
Home / Discussions / Java
   

Java

 
GeneralRe: C to Java translation Pin
Richard MacCutchan13-Jan-14 7:18
mveRichard MacCutchan13-Jan-14 7:18 
GeneralRe: C to Java translation Pin
Kujtim Hyseni13-Jan-14 7:24
Kujtim Hyseni13-Jan-14 7:24 
GeneralRe: C to Java translation Pin
Richard MacCutchan13-Jan-14 7:39
mveRichard MacCutchan13-Jan-14 7:39 
GeneralRe: C to Java translation Pin
Kujtim Hyseni13-Jan-14 7:41
Kujtim Hyseni13-Jan-14 7:41 
GeneralRe: C to Java translation Pin
Richard MacCutchan13-Jan-14 23:14
mveRichard MacCutchan13-Jan-14 23:14 
AnswerRe: C to Java translation Pin
thatraja13-Jan-14 6:43
professionalthatraja13-Jan-14 6:43 
AnswerRe: C to Java translation Pin
jschell13-Jan-14 9:25
jschell13-Jan-14 9:25 
QuestionResize objects from an ArrayList Pin
ririsuperstar11-Jan-14 4:44
ririsuperstar11-Jan-14 4:44 
Ok, here is the code to resize one rectangle.
I would like to display several rectangles and resize them independantly, so the best way is to create an arraylist, right ? I have modified the code (see at the bottom), but it displays only the rectangles. No error messages when I test the program, but impossible to resize them.

Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Resizing extends JPanel {
    Rectangle rect = new Rectangle(100,100,150,150);

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.blue);
        g2.draw(rect);
    }

    public static void main(String[] args) {
        Resizing test = new Resizing();
        Resizer resizer = new Resizer(test);
        test.addMouseListener(resizer);
        test.addMouseMotionListener(resizer);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(test);
        f.setSize(400,400);
        f.setLocation(100,100);
        f.setVisible(true);
    }
}

/**
* MouseAdapter okay for j2se 1.6+
* Use MouseInputAdapter for j2se 1.5-
*/
class Resizer extends MouseAdapter {
    Resizing component;
    boolean dragging = false;
    // Give user some leeway for selections.
    final int PROX_DIST = 3;

    public Resizer(Resizing r) {
        component = r;
    }

    public void mousePressed(MouseEvent e) {
        if(component.getCursor() != Cursor.getDefaultCursor()) {
            // If cursor is set for resizing, allow dragging.
            dragging = true;
        }
    }

    public void mouseReleased(MouseEvent e) {
        dragging = false;
    }

    public void mouseDragged(MouseEvent e) {
        if(dragging) {
            Point p = e.getPoint();
            Rectangle r = component.rect;
            int type = component.getCursor().getType();
            int dx = p.x - r.x;
            int dy = p.y - r.y;
            switch(type) {
                case Cursor.N_RESIZE_CURSOR:
                    int height = r.height - dy;
                    r.setRect(r.x, r.y+dy, r.width, height);
                    break;
                case Cursor.S_RESIZE_CURSOR:
                    height = dy;
                    r.setRect(r.x, r.y, r.width, height);
                    break;
                default:
                    System.out.println("unexpected type: " + type);
            }
            component.repaint();
        }
    }

    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        if(!isOverRect(p)) {
            if(component.getCursor() != Cursor.getDefaultCursor()) {
                // If cursor is not over rect reset it to the default.
                component.setCursor(Cursor.getDefaultCursor());
            }
            return;
        }
        // Locate cursor relative to center of rect.
        int outcode = getOutcode(p);
        Rectangle r = component.rect;
        switch(outcode) {
            case Rectangle.OUT_TOP:
                if(Math.abs(p.y - r.y) < PROX_DIST) {
                    component.setCursor(Cursor.getPredefinedCursor(
                                        Cursor.N_RESIZE_CURSOR));
                }
                break;
            case Rectangle.OUT_BOTTOM:
                if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                    component.setCursor(Cursor.getPredefinedCursor(
                                        Cursor.S_RESIZE_CURSOR));
                }
                break;
            default:    // center
                component.setCursor(Cursor.getDefaultCursor());
        }
    }

    /**
     * Make a smaller Rectangle and use it to locate the
     * cursor relative to the Rectangle center.
     */
    private int getOutcode(Point p) {
        Rectangle r = (Rectangle)component.rect.clone();
        r.grow(-PROX_DIST, -PROX_DIST);
        return r.outcode(p.x, p.y);
    }

    /**
     * Make a larger Rectangle and check to see if the
     * cursor is over it.
     */
    private boolean isOverRect(Point p) {
        Rectangle r = (Rectangle)component.rect.clone();
        r.grow(PROX_DIST, PROX_DIST);
        return r.contains(p);
    }
}



MODIFIED CODE


Java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.*;

class Resizing extends JPanel {

    ArrayList paths = new ArrayList();


        public Resizing() {
            paths.add(wrap(new Rectangle2D.Double(100, 100, 75, 50)));
            paths.add(wrap(new Rectangle2D.Double(250, 100, 75, 50)));

        }

        private Path2D.Double wrap(Shape s) {
            return new Path2D.Double(s);
        }

    <a href="/Members/override">@Override</a>
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            for(int j = 0; j < paths.size(); j++) {
                Path2D.Double path = (Path2D.Double)paths.get(j);
                g2.draw(path);
            }
        }


    public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new Resizing());
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);

    }

class Resizer extends MouseAdapter {
    Resizing component;
    boolean dragging = false;
    // Give user some leeway for selections.
    final int PROX_DIST = 3;
    Path2D.Double selectedPath;

    public Resizer(Resizing rz) {
        component = rz;
        component.addMouseListener(this);
        component.addMouseMotionListener(this);
    }

    public void mousePressed(MouseEvent e) {
        if(component.getCursor() != Cursor.getDefaultCursor()) {
            // If cursor is set for resizing, allow dragging.
            dragging = true;
        }
    }

    public void mouseReleased(MouseEvent e) {
        dragging = false;
    }

    public void mouseDragged(MouseEvent e) {
        if(dragging) {
            Point p = e.getPoint();
            ArrayList list = component.paths;
            for(int j = 0; j < list.size(); j++) {
                Path2D.Double path = (Path2D.Double)list.get(j);
                selectedPath = path;
                Rectangle r = selectedPath.getBounds();
                int type = component.getCursor().getType();
                int dx = p.x - r.x;
                int dy = p.y - r.y;
                switch(type) {
                case Cursor.N_RESIZE_CURSOR:
                    int height = r.height - dy;
                    r.setRect(r.x, r.y+dy, r.width, height);
                    break;
                case Cursor.S_RESIZE_CURSOR:
                    height = dy;
                    r.setRect(r.x, r.y, r.width, height);
                    break;
                default:
                    System.out.println("unexpected type: " + type);
                }
                component.repaint();
            }}
    }

    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        if(!isOverRect(p)) {
            if(component.getCursor() != Cursor.getDefaultCursor()) {
                // If cursor is not over rect reset it to the default.
                component.setCursor(Cursor.getDefaultCursor());
            }
            return;
        }
        // Locate cursor relative to center of rect.
        int outcode = getOutcode(p);
        ArrayList list = component.paths;
        Rectangle r = selectedPath.getBounds();
        switch(outcode) {
            case Rectangle.OUT_TOP:
                if(Math.abs(p.y - r.y) < PROX_DIST) {
                    component.setCursor(Cursor.getPredefinedCursor(
                                        Cursor.N_RESIZE_CURSOR));
                }
                break;
            case Rectangle.OUT_BOTTOM:
                if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                    component.setCursor(Cursor.getPredefinedCursor(
                                        Cursor.S_RESIZE_CURSOR));
                }
                break;
            default:    // center
                component.setCursor(Cursor.getDefaultCursor());
        }}

    /**
     * Make a smaller Rectangle and use it to locate the
     * cursor relative to the Rectangle center.
     */
    private int getOutcode(Point p) {
        Rectangle r = (Rectangle)component.paths.clone();
        r.grow(-PROX_DIST, -PROX_DIST);
        return r.outcode(p.x, p.y);
    }

    /**
     * Make a larger Rectangle and check to see if the
     * cursor is over it.
     */
    private boolean isOverRect(Point p) {
        Rectangle r = (Rectangle)component.paths.clone();
        r.grow(PROX_DIST, PROX_DIST);
        return r.contains(p);
    }
}}

Questionjava technology Pin
Member 1051702110-Jan-14 16:25
Member 1051702110-Jan-14 16:25 
AnswerRe: java technology Pin
Richard Andrew x6410-Jan-14 16:44
professionalRichard Andrew x6410-Jan-14 16:44 
GeneralRe: java technology Pin
Richard MacCutchan10-Jan-14 22:11
mveRichard MacCutchan10-Jan-14 22:11 
AnswerRe: java technology Pin
Richard MacCutchan10-Jan-14 22:11
mveRichard MacCutchan10-Jan-14 22:11 
QuestionWould like to retrieve RGB pixel values from another method but couldn't retrieve it Pin
nurul azila9-Jan-14 5:02
nurul azila9-Jan-14 5:02 
QuestionRe: Would like to retrieve RGB pixel values from another method but couldn't retrieve it Pin
Richard MacCutchan9-Jan-14 21:17
mveRichard MacCutchan9-Jan-14 21:17 
QuestionProblem is to classify whether the video on YouTube is harassment type or not... Pin
nishaaggarwal8-Jan-14 22:39
nishaaggarwal8-Jan-14 22:39 
QuestionRe: Problem is to classify whether the video on YouTube is harassment type or not... Pin
Richard MacCutchan8-Jan-14 23:39
mveRichard MacCutchan8-Jan-14 23:39 
QuestionCode to convert from C# to Java (with goto) Pin
Kujtim Hyseni8-Jan-14 9:00
Kujtim Hyseni8-Jan-14 9:00 
AnswerRe: Code to convert from C# to Java (with goto) Pin
gettgotcha8-Jan-14 9:20
gettgotcha8-Jan-14 9:20 
AnswerRe: Code to convert from C# to Java (with goto) Pin
Richard MacCutchan8-Jan-14 22:24
mveRichard MacCutchan8-Jan-14 22:24 
GeneralRe: Code to convert from C# to Java (with goto) Pin
Kujtim Hyseni9-Jan-14 1:45
Kujtim Hyseni9-Jan-14 1:45 
GeneralRe: Code to convert from C# to Java (with goto) Pin
Richard MacCutchan9-Jan-14 1:52
mveRichard MacCutchan9-Jan-14 1:52 
QuestionTool for generating JNI (calling C/C++ code from java) Pin
doofx8-Jan-14 3:19
doofx8-Jan-14 3:19 
AnswerRe: Tool for generating JNI (calling C/C++ code from java) Pin
Richard MacCutchan8-Jan-14 6:09
mveRichard MacCutchan8-Jan-14 6:09 
GeneralRe: Tool for generating JNI (calling C/C++ code from java) Pin
doofx8-Jan-14 6:25
doofx8-Jan-14 6:25 
GeneralRe: Tool for generating JNI (calling C/C++ code from java) Pin
Richard MacCutchan8-Jan-14 6:44
mveRichard MacCutchan8-Jan-14 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.