Привет. Нужна Ваша помощь. Только начинаю учить джаву и столкнулся с проблемой. Есть задача сделать квадрат во фрейме которым можно двигать мышкой и к трем сторонам квадрата должни идти линии от рамок фрейма до середин граней квадрата, концы линий что примыкают к квадрату должны бегать за квадратом когда его перетягиваеш мышкой вот мой код
package graphicsmain;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class ShapesDragging extends JComponent {
int anchorX = 0;
int anchorY = 0;
int mouseX = 600/2;
int mouseY = 400/2;
int anchor1x=0;
int anchor1y=0;
int anchor2x=0;
int anchor2y=0;
private static class Item {
public Shape shape;
public Color color;
public AffineTransform transform;
}
private java.util.List<Item> items;
private double dx;
private double dy;
private double scale;
public ShapesDragging() {
items = new ArrayList<Item>();
dx = 0.0;
dy = 0.0;
scale = 1.0;
DragShape dragShape = new DragShape();
PanZoom panZoom = new PanZoom();
addMouseListener(dragShape);
addMouseListener(panZoom);
addMouseMotionListener(dragShape);
addMouseMotionListener(panZoom);
addMouseWheelListener(panZoom);
anchorX = 220;
anchorY = 0;
anchor1x=600;
anchor1y=200;
anchor2x=250;
anchor2y=500;
}
public void addItem(Shape shape, Color color) {
AffineTransform transform = AffineTransform.getTranslateInstance(0, 0);
addItem(shape, color, transform);
}
public void addItem(Shape shape, Color color, AffineTransform transform) {
Item item = new Item();
item.shape = shape;
item.color = color;
item.transform = transform;
items.add(item);
}
public void translate(double tx, double ty) {
dx += tx * scale;
dy += ty * scale;
}
public void scale(double factor, double x, double y) {
double coef = scale * (1.0 - factor);
dx += coef * x;
dy += coef * y;
scale *= factor;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.translate(dx, dy);
g2d.scale(scale, scale);
for (Item item : items) {
AffineTransform saved = g2d.getTransform();
g2d.transform(item.transform);
g2d.setColor(item.color);
g2d.fill(item.shape);
g2d.setTransform(saved);
g2d.setPaint(Color.black);
g2d.drawLine(anchorX, anchorY, mouseX, mouseY);
g2d.setPaint(Color.red);
g2d.drawLine(anchor1x, anchor1y, mouseX, mouseY);
g2d.setPaint(Color.blue);
g2d.drawLine(anchor2x, anchor2y, mouseX, mouseY);
}
}
private Point2D mapToScene(Point point) {
double x = (point.x - dx) / scale;
double y = (point.y - dy) / scale;
return new Point2D.Double(x, y);
}
private Item findItem(Point2D point) {
// process items in the order reversed to drawing order
ListIterator<Item> iter = items.listIterator(items.size());
while (iter.hasPrevious()) {
Item item = iter.previous();
AffineTransform transform = item.transform;
Shape shape = item.shape;
Point2D transfPoint = null;
try {
if (shape.contains(transform.inverseTransform(point, transfPoint)))
return item;
} catch (NoninvertibleTransformException ignored) {
}
}
return null;
}
private class DragShape extends MouseAdapter {
private Item hoverItem;
private boolean isDragging;
private Point previous;
private Cursor savedCursor;
@Override
public void mouseDragged(MouseEvent e) {
if (!isDragging)
return;
// don't move item if both mouse buttons are pressed
if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == 0) {
Point2D point = mapToScene(e.getPoint());
Point2D prevPoint = mapToScene(previous);
double tx = point.getX() - prevPoint.getX();
double ty = point.getY() - prevPoint.getY();
AffineTransform transform = AffineTransform.getTranslateInstance(tx, ty);
transform.concatenate(hoverItem.transform);
hoverItem.transform = transform;
repaint();
}
previous = new Point(e.getPoint());
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
Point2D point = mapToScene(e.getPoint());
Item item = findItem(point);
if (hoverItem == null && item != null) {
savedCursor = getCursor();
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else if (hoverItem != null && item == null) {
setCursor(savedCursor);
}
hoverItem = item;
// mouseX = e.getX();
// mouseY = e.getY();
// repaint();
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && !isDragging && hoverItem != null) {
isDragging = true;
previous = new Point(e.getPoint());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && isDragging) {
isDragging = false;
}
}
}
private class PanZoom extends MouseAdapter {
private boolean isDragging;
private Point previous;
private Cursor savedCursor;
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3 && !isDragging) {
isDragging = true;
previous = new Point(e.getPoint());
savedCursor = getCursor();
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3 && isDragging) {
isDragging = false;
setCursor(savedCursor);
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (isDragging) {
Point2D point = mapToScene(e.getPoint());
Point2D prevPoint = mapToScene(previous);
double tx = point.getX() - prevPoint.getX();
double ty = point.getY() - prevPoint.getY();
translate(tx, ty);
previous = new Point(e.getPoint());
repaint();
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
double factor = Math.pow(1.2, e.getWheelRotation());
Point2D point = mapToScene(e.getPoint());
scale(factor, point.getX(), point.getY());
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Рухаем квадрат");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShapesDragging shapesDragging = new ShapesDragging();
shapesDragging.addItem(new Rectangle2D.Double(150, 150,150, 150), Color.RED);
frame.getContentPane().add(shapesDragging);
frame.setSize(600, 500);
frame.setVisible(true);
}
});
}
}
Привязал концы линий к точке клика мышки, но немогу никак привязать к граням квадрата, помогите
Виртуальный выделенный сервер (VDS) становится отличным выбором
Скопировал данный проектКомпилируется без ошибок
При conection в базу, ошибка NoSuchMethodError: orghibernate
Приветствую! Есть url, на который необходимо послать запрос с использованием ЭЦП(электронная цифровая подпись), у меня есть private и public ключи,...
Появятся ли protected/package-private методы в интерфейсах Java 9 или добавят только private?