aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help me how to drag and move drawing shaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help me how to drag and move drawing shaps" Watch "Help me how to drag and move drawing shaps" New topic
Author

Help me how to drag and move drawing shaps

Arjun Palanichamy
Ranch Hand

Joined: Jul 15, 2006
Posts: 51
Hi Friends...

I have finished the mouse over drag and draw the some various shapes,example Line,Oval,Rectangle...,But i want to drag and move that drawing shapes one place to other place.

Please help me solv this problem....

my codeing here...

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class appletshape extends JApplet {

JComboBox colorChoice, figureChoice; // Pop-up menus, defined as instance

public void init() {

setBackground(Color.gray);
getContentPane().setBackground(Color.gray);

Display canvas = new Display(); // The drawing area.
getContentPane().add(canvas, BorderLayout.CENTER);

JPanel buttonBar = new JPanel(); // A panel to hold the buttons.
buttonBar.setBackground(Color.gray);
getContentPane().add(buttonBar, BorderLayout.SOUTH);

JPanel choiceBar = new JPanel(); // A panel to hole the pop-up menus
choiceBar.setBackground(Color.gray);
getContentPane().add(choiceBar, BorderLayout.NORTH);

JButton fill = new JButton("Set Background"); // The first button.
fill.addActionListener(canvas);
buttonBar.add(fill);

JButton clear = new JButton("Clear"); // The second button.
clear.addActionListener(canvas);
buttonBar.add(clear);

colorChoice = new JComboBox(); // The pop-up menu of colors.
colorChoice.addItem("Black");
colorChoice.addItem("Red");
colorChoice.addItem("Green");
colorChoice.addItem("Blue");
colorChoice.addItem("Cyan");
colorChoice.addItem("Magenta");
colorChoice.addItem("Yellow");
colorChoice.addItem("White");
colorChoice.setBackground(Color.green);
choiceBar.add(colorChoice);

figureChoice = new JComboBox(); // The pop-up menu of shapes.
figureChoice.addItem("Curve");
figureChoice.addItem("Straight Line");
figureChoice.addItem("Rectangle");
figureChoice.addItem("Oval");
figureChoice.addItem("RoundRect");
figureChoice.addItem("Filled Rectangle");
figureChoice.addItem("Filled Oval");
figureChoice.addItem("Filled RoundRect");
figureChoice.setBackground(Color.green);
choiceBar.add(figureChoice);

}

public Insets getInsets() {

return new Insets(2, 2, 2, 2);
}

private class Display extends JPanel implements MouseListener,
MouseMotionListener, ActionListener {

private final static int BLACK = 0, RED = 1, // Some constants to

GREEN = 2, BLUE = 3, CYAN = 4, MAGENTA = 5, YELLOW = 6,
WHITE = 7;

private final static int CURVE = 0, LINE = 1, RECT = 2, // Some

OVAL = 3, ROUNDRECT = 4, FILLED_RECT = 5, FILLED_OVAL = 6,
FILLED_ROUNDRECT = 7;

Image OSI;

int widthOfOSI,heightOfOSI;

private int mouseX,mouseY;

private int prevX, prevY;

private int startX, startY;

private boolean dragging;

private int figure;

private Graphics dragGraphics;

private Color dragColor;

Display() {

addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.white);
}

private void drawFigure(Graphics g, int shape, int x1, int y1, int x2,
int y2) {

if (shape == LINE) {

g.drawLine(x1, y1, x2, y2);
return;
}
int x, y;
int w, h;

if (x1 >= x2) { // x2 is left edge
x = x2;
w = x1 - x2;
} else { // x1 is left edge
x = x1;
w = x2 - x1;
}
if (y1 >= y2) { // y2 is top edge
y = y2;
h = y1 - y2;
} else { // y1 is top edge.
y = y1;
h = y2 - y1;
}


switch (shape) { // Draw the appropriate figure.

case RECT:
g.drawRect(x, y, w, h);
break;

case OVAL:
g.drawOval(x, y, w, h);
break;

case ROUNDRECT:
g.drawRoundRect(x, y, w, h, 50, 50);
break;

case FILLED_RECT:
g.fillRect(x, y, w, h);
break;

case FILLED_OVAL:
g.fillOval(x, y, w, h);
break;

case FILLED_ROUNDRECT:
g.fillRoundRect(x, y, w, h, 50, 50);
break;

}
}

private void repaintRect(int x1, int y1, int x2, int y2) {

int x, y;
int w, h;
// figure
if (x2 >= x1) { // x1 is left edge
x = x1;
w = x2 - x1;
} else { // x2 is left edge
x = x2;
w = x1 - x2;
}
if (y2 >= y1) { // y1 is top edge
y = y1;
h = y2 - y1;
} else { // y2 is top edge.
y = y2;
h = y1 - y2;
}
repaint(x, y, w + 1, h + 1);
}

private void checkOSI() {

if (OSI == null || widthOfOSI != getSize().width
|| heightOfOSI != getSize().height) {

OSI = null;

OSI = createImage(getSize().width, getSize().height);
widthOfOSI = getSize().width;
heightOfOSI = getSize().height;
Graphics OSG = OSI.getGraphics(); // Graphics context for

OSG.setColor(getBackground());
OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
OSG.dispose();
}
}

public void paintComponent(Graphics g) {

checkOSI();
g.drawImage(OSI, 0, 0, this);
if (dragging && figure != CURVE) {
g.setColor(dragColor);
drawFigure(g, figure, startX, startY, mouseX, mouseY);
}
}

public void actionPerformed(ActionEvent evt) {

String command = evt.getActionCommand();
checkOSI();
if (command.equals("Set Background")) {

setBackground(getCurrentColor());
if (colorChoice.getSelectedIndex() == BLACK)
colorChoice.setSelectedIndex(WHITE);
else
colorChoice.setSelectedIndex(BLACK);
}
Graphics g = OSI.getGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.dispose();
repaint();
}

private Color getCurrentColor() {

int currentColor = colorChoice.getSelectedIndex();
switch (currentColor) {
case BLACK:
return Color.black;
case RED:
return Color.red;
case GREEN:
return Color.green;
case BLUE:
return Color.blue;
case CYAN:
return Color.cyan;
case MAGENTA:
return Color.magenta;
case YELLOW:
return Color.yellow;
default:
return Color.white;
}
}

public void mousePressed(MouseEvent evt) {

if (dragging == true) // Ignore mouse presses that occur
return; // when user is already drawing a curve.

prevX = startX = evt.getX(); // Save mouse coordinates.
prevY = startY = evt.getY();

figure = figureChoice.getSelectedIndex();
dragColor = getCurrentColor();
dragGraphics = OSI.getGraphics();
dragGraphics.setColor(dragColor);

dragging = true; // Start drawing.

} // end mousePressed()

public void mouseReleased(MouseEvent evt) {

if (dragging == false)
return; // Nothing to do because the user isn't drawing.
dragging = false;
mouseX = evt.getX();
mouseY = evt.getY();
if (figure == CURVE) {
// A CURVE is drawn as a series of LINEs
drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
repaintRect(prevX, prevY, mouseX, mouseY);
} else if (figure == LINE) {
repaintRect(startX, startY, prevX, prevY);
if (mouseX != startX || mouseY != startY) {
// Draw the line only if it has non-zero length.
drawFigure(dragGraphics, figure, startX, startY, mouseX,
mouseY);
repaintRect(startX, startY, mouseX, mouseY);

}
} else {
repaintRect(startX, startY, prevX, prevY);
if (mouseX != startX && mouseY != startY) {

drawFigure(dragGraphics, figure, startX, startY, mouseX,
mouseY);
repaintRect(startX, startY, mouseX, mouseY);
}
}
dragGraphics.dispose();
dragGraphics = null;
}

public void mouseDragged(MouseEvent evt) {

if (dragging == false)
return;
mouseX = evt.getX();
mouseY = evt.getY();

if (figure == CURVE) {

drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
repaintRect(prevX, prevY, mouseX, mouseY);
} else {

repaintRect(startX, startY, prevX, prevY);
repaintRect(startX, startY, mouseX, mouseY);
}

prevX = mouseX;
prevY = mouseY;

}

public void mouseEntered(MouseEvent evt) {
}

public void mouseExited(MouseEvent evt) {
}

public void mouseClicked(MouseEvent evt) {
}

public void mouseMoved(MouseEvent evt) {
}
}
}

======================================
Thanks for your time..

Arjun Palanichamy.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
To move things around it is better to not use an offscreen image. Instead, do everything inside paintComponent (whose proper access modifier is protected), keep a List (ArrayList: one for Shapes and one for freehand line segments, or you can put each freehand line draw into a GeneralPath and put it into the shapes list) of what you have drawn and draw it each time paintComponent is called. Or you can make up a small data store class that keeps track of things like geometric primitive (or raw data if you prefer, eg, x, y, width), color, selection state, selection color and keep track of these in your list. If you want to have the data store do more of the work you can give it methods like draw, contains and isSelected.
Then in your mousePressed you can check to see if the mouse position is over one of the list items and, if so, start drag operations. If not then you can drag/draw a new primitive in a temporary way like you do now. In mouseReleased, add the newly drawn primitive to the list, abandon the temporary one and repaint.
Primitives are in the java.awt.geom package and include, Line2D, Arc2D, Ellipse2D.
Also, since these apps can grow in complexity you might consider breaking things up into smaller classes, say like a JPanel extension for the rendering of items in the list, a MouseInputAdapter class to do the mouse work and a class that puts these together in a gui with ui controls (your north and south section components). It may take some experimenting to get the class structure the way you want but will be well worth the expense later on down the line.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me how to drag and move drawing shaps