问题描述
我正在尝试绘制可用于创建图片的形状(三角形,矩形,正方形和圆形)。用户应该能够在画布上放置一个形状并将其拖动,甚至将其放大以创建所需的图片。
I am trying to draw shapes (triangle, rectangle, square and circle) that can be used in creating a picture. A user should be able to place a shape on a canvas and drag it around or even enlarge it to create a desired picture.
我试图覆盖 paintComponent()
一个 JPanel
来实现这一点但是要意识到Jpanels的形状仍然是一个正方形所以当你有一个圆圈时你仍然可以拖动它即使你不一定触摸它,因为它在一个正方形内并且也难以画三角形
I tried overriding the paintComponent()
of a JPanel
to achieve this but realize that the Jpanels shape remains a square so when you have a circle you can still drag it even if u are not necessarily touching it as it is within a square and also having difficulty drawing the triangle
你会建议哪些图书馆?
推荐答案
Shape接口和派生类(如Path2D,Ellipse2D和Rectangle2D)将在这里为您提供帮助,因为从中派生的所有类必须具有包含(Point p)
您可以使用的方法。
The Shape interface and derived classes, such as Path2D, Ellipse2D and Rectangle2D, will help you here, since all classes derived from it must have a contains(Point p)
method that you can use.
例如:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class DragShape extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 700;
private static final Color SHAPE_COLOR = new Color(255, 100, 100);
private Path2D myShape = new Path2D.Double();
public DragShape() {
myShape.append(new Ellipse2D.Double(150, 50, 200, 200), true);
myShape.append(new Rectangle2D.Double(150, 150, 200, 400), true);
myShape.append(new Ellipse2D.Double(350, 250, 150, 150), true);
myShape.append(new Ellipse2D.Double(150, 450, 200, 200), true);
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (myShape != null) {
g2.setColor(SHAPE_COLOR);
g2.fill(myShape);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
class MyMouseAdapter extends MouseAdapter {
private boolean pressed = false;
private Point point;
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
if (myShape != null && myShape.contains(e.getPoint())) {
pressed = true;
this.point = e.getPoint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (pressed) {
int deltaX = e.getX() - point.x;
int deltaY = e.getY() - point.y;
myShape.transform(AffineTransform.getTranslateInstance(deltaX, deltaY));
point = e.getPoint();
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
pressed = false;
}
}
private static void createAndShowGui() {
DragShape mainPanel = new DragShape();
JFrame frame = new JFrame("DragShape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
我会让你弄清楚轮换和重新大小。
I'll let you figure out rotation and re-sizing.
这篇关于在java中绘制形状对象(可拖动,可调整大小并可以旋转)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!