Created
March 17, 2013 16:33
-
-
Save szymanskip/5182312 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseMotionListener; | |
import java.util.ArrayList; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: Pawel | |
* Date: 17.03.13 | |
* Time: 09:02 | |
*/ | |
public class Shapes extends JPanel implements MouseListener, MouseMotionListener { | |
public static final int IDLE = -99; | |
public static final int TOP_LEFT = 1; | |
public static final int TOP_RIGHT = 2; | |
public static final int BOTTOM_RIGHT=3; | |
public static final int BOTTOM_LEFT =4; | |
private ArrayList<Shape> shapes; | |
private int flag = IDLE; | |
private Rectangle[] selection ; | |
private int direction = 0; | |
private Rectangle startPos; | |
private Rectangle startRect; | |
private Point pressedPoint; | |
private StatusBar statusBar; | |
private JList list; | |
public Shapes() { | |
shapes = new ArrayList<Shape>(); | |
shapes.add(new Rectangle(100,100,50,50)); | |
shapes.add(new Rectangle(300,200,120,90)); | |
selection = new Rectangle[]{new Rectangle(),new Rectangle(),new Rectangle(),new Rectangle()}; | |
statusBar = new StatusBar(); | |
list = new JList(shapes.toArray()); | |
list.setSize(20,200); | |
add(list, BorderLayout.SOUTH); | |
add(statusBar,BorderLayout.SOUTH); | |
addMouseListener(this); | |
addMouseMotionListener(this); | |
} | |
public static void main(String [] args) | |
{ | |
JFrame frame = new JFrame("Shapes"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(800, 600); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
frame.add(new Shapes(),BorderLayout.CENTER); | |
} | |
@Override | |
protected void paintComponent(Graphics graphics) { | |
super.paintComponent(graphics); | |
Graphics2D g2D = (Graphics2D) graphics; | |
g2D.setColor(Color.RED); | |
for(int i=0;i<shapes.size();i++) | |
{ | |
g2D.fill(shapes.get(i)); | |
if(flag == i){ | |
select(shapes.get(i)); | |
g2D.setColor(Color.GRAY); | |
for(int j=0;j<selection.length;j++) | |
g2D.fill(selection[j]); | |
g2D.setColor(Color.RED); | |
} | |
} | |
} | |
private void select(Shape shape) { | |
Rectangle bounds = shape.getBounds(); | |
double x = bounds.getX(); | |
double y = bounds.getY(); | |
double width = bounds.getWidth(); | |
double height = bounds.getHeight(); | |
selection[0].setRect(x - 3, y - 3, 6, 6); | |
selection[1].setRect(x + width - 3, y - 3, 6, 6); | |
selection[2].setRect(x+width-3,y+height-3,6,6); | |
selection[3].setRect(x-3,y+height-3,6,6); | |
} | |
@Override | |
public void mouseClicked(MouseEvent mouseEvent) { | |
Point p = mouseEvent.getPoint(); | |
if(flag == IDLE) | |
for(int i=0;i<shapes.size();i++) | |
{ | |
if(shapes.get(i).contains(p)) { | |
flag = i; | |
startRect = shapes.get(flag).getBounds(); | |
break; | |
} | |
} | |
else | |
flag = IDLE; | |
repaint(); | |
} | |
@Override | |
public void mousePressed(MouseEvent mouseEvent) { | |
Point p = mouseEvent.getPoint(); | |
if(flag != IDLE) { | |
for(int i=0;i<selection.length;i++) | |
{ | |
if(selection[i].contains(p)){ | |
direction = i + 1; | |
startPos = selection[i].getBounds(); | |
break; | |
} | |
} | |
} else{ | |
for(int i=0;i<shapes.size();i++) | |
{ | |
if(shapes.get(i).contains(p)) { | |
flag = i; | |
pressedPoint = p; | |
startRect = shapes.get(i).getBounds(); | |
break; | |
} | |
} | |
} | |
} | |
@Override | |
public void mouseReleased(MouseEvent mouseEvent) { | |
if(direction>0) | |
direction = 0; | |
if(flag!=IDLE) | |
flag = IDLE; | |
} | |
@Override | |
public void mouseEntered(MouseEvent mouseEvent) { | |
} | |
@Override | |
public void mouseExited(MouseEvent mouseEvent) { | |
} | |
@Override | |
public void mouseDragged(MouseEvent mouseEvent) { | |
Point p = mouseEvent.getPoint(); | |
statusBar.setMessage("x = " + p.x + " y = "+ p.y + " flag = " + flag); | |
if(direction > 0) { | |
Rectangle rect = ((Rectangle)shapes.get(flag)); | |
int dx = startPos.x - p.x; | |
int dy = startPos.y - p.y; | |
switch(direction) | |
{ | |
case TOP_LEFT: | |
rect.setBounds(startRect.x - dx, startRect.y - dy,startRect.width + dx, startRect.height + dy); | |
break; | |
case TOP_RIGHT: | |
rect.setBounds(startRect.x, startRect.y - dy,startRect.width - dx, startRect.height + dy); | |
break; | |
case BOTTOM_RIGHT: | |
rect.setSize(startRect.width - dx, startRect.height - dy); | |
break; | |
case BOTTOM_LEFT: | |
rect.setBounds(startRect.x - dx, startRect.y,startRect.width + dx, startRect.height - dy); | |
break; | |
} | |
} else if (flag != IDLE){ | |
Rectangle rect = ((Rectangle)shapes.get(flag)); | |
int dx = pressedPoint.x - startRect.x; | |
int dy = pressedPoint.y - startRect.y; | |
rect.setBounds(p.x - dx, p.y - dy, rect.width, rect.height); | |
} | |
repaint(); | |
} | |
@Override | |
public void mouseMoved(MouseEvent mouseEvent) { | |
Point p = mouseEvent.getPoint(); | |
statusBar.setMessage("x = " + p.x + " y = "+ p.y + " flag = " + flag); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: Pawel | |
* Date: 17.03.13 | |
* Time: 16:43 | |
*/ | |
public class StatusBar extends JLabel { | |
public StatusBar() { | |
super(); | |
super.setPreferredSize(new Dimension(200, 16)); | |
setMessage("Ready"); | |
} | |
public void setMessage(String message) { | |
setText(" "+message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment