///////////////////////////////////////Den andra klassen:
///////////////////////////////////////Den andra klassen:
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
public class Box extends JApplet
{
Vector shape = new Vector();
Vector arrowArray = new Vector();
int theta;
class axel extends JPanel implements KeyListener
{
GeneralPath arrow = new GeneralPath();
Stroke dashStroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10f, new float[] {10f, 10f}, 5f);
Paint gradientPaint = new GradientPaint( -30, 0, Color.red, 10, 0, Color.yellow);
boolean delete_box, addSquare, addArrow, dragging;
axel selectedRect;
RectangularShape boxen;
AffineTransform xform;
RectSelector action;
String texten;
public axel()
{
shape.addElement(new axel("Text1", 40, 50, 50, 60));
shape.addElement(new axel("Text2", 150, 100, 50, 60));
shape.addElement(new axel("Text3", 40, 150, 50, 60));
setBackground(Color.white);
action = new RectSelector(this);
addMouseListener(action);
addMouseMotionListener(action);
addKeyListener(this);
}
public axel(String text, int x, int y, int w, int h)
{
texten = text;
boxen = new RoundRectangle2D.Double(x, y, w, h, 20, 20);
xform = new AffineTransform();
theta = 5;
}
public void rita(Graphics2D g2)
{
double cx = boxen.getCenterX();
double cy = boxen.getCenterY();
int mus_x =(int) cx;
int mus_y =(int) cy;
AffineTransform orig = g2.getTransform();
if (!xform.isIdentity())
g2.setTransform(xform);
g2.fill(boxen);
g2.setColor(Color.black);
g2.setFont(new Font("Helvetica", Font.BOLD, 12));
g2.drawString(texten, mus_x + -11, mus_y + 5);
g2.draw(boxen);
g2.setTransform(orig);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Image bild = getImage(getDocumentBase(), "images.jpg");
g2.drawImage(bild, 0, 0, this);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//Draw the Shapes
for (int i = 0; i < shape.size(); i++)
{
axel rsh = (axel) shape.get(i);
if (rsh == selectedRect)
{
g2.setStroke(dashStroke);
g2.setPaint(Color.green);
}
else
{
g2.setStroke(new BasicStroke(0));
g2.setPaint(Color.yellow);
}
rsh.rita(g2);
}
//Draw the Arrows
for (int i = 0; i < arrowArray.size(); i++)
{
GraphicArrow arrowen = (GraphicArrow) arrowArray.get(i);
arrowen.paintComponent(g);
}
}
public void resizen()
{
Rectangle r = boxen.getBounds();
double width = boxen.getWidth()+2;
double height = boxen.getHeight()+2;
boxen.setFrame(r.x, r.y, width, height);
}
public void rotateRect()
{
double cx = boxen.getCenterX();
double cy = boxen.getCenterY();
theta = theta - 5;
xform.setToRotation(Math.toRadians(theta), cx, cy);
}
public void rotateRect_right()
{
double cx = boxen.getCenterX();
double cy = boxen.getCenterY();
theta = theta + 5;
xform.setToRotation(Math.toRadians(theta), cx, cy);
}
public void keyPressed(KeyEvent e) {
System.out.println("Test1");
if (e.isShiftDown()) {
System.out.println("Test3");
}
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
System.out.println("Test2");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
class RectSelector extends MouseInputAdapter
{
axel rotationPanel;
RectangularShape selectedShape;
axel rs;
Point offset;
final int size = 50;
public RectSelector(axel fp)
{
rotationPanel = fp;
offset = new Point();
delete_box = false;
addSquare = false;
addArrow = false;
dragging = false;
}
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
//New Box creates
if (addSquare)
{
shape.addElement(new axel("Another text", p.x, p.y, 50, 60));
rotationPanel.repaint();
addSquare = false;
return;
}
//New Arrow creates
if (addArrow)
{
arrowArray.addElement(new GraphicArrow(p.x, p.y));
rotationPanel.repaint();
addArrow = false;
return;
}
for (int i = 0; i < shape.size(); i++)
{
rs = (axel) shape.get(i);
if (rs.boxen.contains(p))
{
if (delete_box)
{
shape.removeElementAt(i);
delete_box = false;
repaint();
}
Rectangle r = rs.boxen.getBounds();
offset.x = p.x - r.x;
offset.y = p.y - r.y;
selectedRect = rs;
selectedShape = rs.boxen;
dragging = true;
repaint();
}
//else
// System.out.println("press does not work " + delete_box + i + rs.boxen.contains(p));
}
}
public void mouseReleased(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
selectedShape = null;
dragging = false;
setCursor(new Cursor(Cursor.DEFAULT_CURSOR) );
//Graphics2D g3 = (Graphics2D) getGraphics();
//g3.setStroke(new BasicStroke(0));
//g3.setPaint(Color.yellow);
}
public void mouseDragged(MouseEvent e)
{
if (dragging)
{
Point p = e.getPoint();
if (selectedShape != null)
{
setCursor(new Cursor(Cursor.MOVE_CURSOR) );
Rectangle r = selectedShape.getBounds();
System.out.println("dragging " + p + p.x + " " + p.y);
r.x = p.x - offset.x;
r.y = p.y - offset.y;
selectedShape.setFrame(r);
repaint();
}
}
}
public void mouseMoved(MouseEvent e)
{
}
}