我是Java swing桌面图形应用程序的新手我有一个问题请建议我需要选择一些矩形并填充矩形我们是否有任何预定义的概念就像橡皮筋一样,如果有的话请建议我开始一个绘图
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Exam extends JPanel implements MouseMotionListener {
private static final int recW = 50;
private static final int MAX = 100;
private Rectangle[] rect = new Rectangle[MAX];
private int numOfRecs = 0;
private int currentSquareIndex = -1;
private Point startPoint = new Point();
private Point currentPoint = new Point();
private int x, y, width, height;
private Stroke dashedStroke = new BasicStroke(0.0f, BasicStroke.CAP_ROUND,
BasicStroke.CAP_SQUARE, 5.0f, new float[] { 5f, 5f, 5f, 5f }, 5.0f);
ArrayList list = new ArrayList();
public Exam() {
addRect(50, 50);
addRect(100, 50);
addRect(150, 50);
addRect(200, 50);
addRect(250, 50);
addRect(300, 50);
addRect(350, 50);
addRect(400, 50);
addRect(450, 50);
addRect(500, 50);
//
addRect(50, 100);
addRect(100, 100);
addRect(150, 100);
addRect(200, 100);
addRect(250, 100);
addRect(300, 100);
addRect(350, 100);
addRect(400, 100);
addRect(450, 100);
addRect(500, 100);
//
addRect(50, 150);
addRect(100, 150);
addRect(150, 150);
addRect(200, 150);
addRect(250, 150);
addRect(300, 150);
addRect(350, 150);
addRect(400, 150);
addRect(450, 150);
addRect(500, 150);
//
addRect(50, 200);
addRect(100, 200);
addRect(150, 200);
addRect(200, 200);
addRect(250, 200);
addRect(300, 200);
addRect(350, 200);
addRect(400, 200);
addRect(450, 200);
addRect(500, 200);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
startPoint = evt.getPoint();
}
public void mouseReleased(MouseEvent e) {
System.out.println(" mouseReleased is " + e.getX() + " y i s "
+ e.getY());
Graphics g = getGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
Color myColour = new Color(255, 0, 0);
g.setColor(myColour);
x = Math.min(startPoint.x, currentPoint.x);
y = Math.min(startPoint.y, currentPoint.y);
width = Math.abs(startPoint.x - currentPoint.x);
height = Math.abs(startPoint.y - currentPoint.y);
rect[numOfRecs] = new Rectangle(x, y, width, height);
// System.out.println("list size is:"+list.size());
Rectangle ggg=new Rectangle(x, y, width, height);
for(Rectangle rect3d: rect){
if(ggg.intersects(rect3d)){
g2.fill(rect3d);
}
else
System.out.println("doesn't contains");
}
repaint();
}
});
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < numOfRecs; i++) {
((Graphics2D) g).draw(rect[i]);
list.add(rect[i]);
}
x = Math.min(startPoint.x, currentPoint.x);
y = Math.min(startPoint.y, currentPoint.y);
width = Math.abs(startPoint.x - currentPoint.x);
height = Math.abs(startPoint.y - currentPoint.y);
((Graphics2D) g).setStroke(dashedStroke);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
// Rectangle dashedRec=new Rectangle(x, y, width, height);
}
public void addRect(int x, int y) {
if (numOfRecs < MAX) {
rect[numOfRecs] = new Rectangle(x, y, recW, recW);
currentSquareIndex = numOfRecs;
numOfRecs++;
repaint();
}
}
@Override
public void mouseMoved(MouseEvent event) {
}
@Override
public void mouseDragged(MouseEvent event) {
currentPoint = event.getPoint();
repaint();
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setTitle("");
jFrame.setSize(600, 300);
Container cPane = jFrame.getContentPane();
cPane.add(new Exam());
jFrame.setVisible(true);
}
}
答案 0 :(得分:1)
你应该从改变开始 import java.awt.event.MouseMotionListener;
到
import java.awt.event.MouseListener;
mousePressed不适用于MouseMotionListener
答案 1 :(得分:1)
我看到班级名称为Exam
,因此这是为了学校,我不会给你代码解决方案。只是一些合乎逻辑的方式来看待它。
我注意到当你拖动鼠标时,你只是得到一个点并基于该点绘制。你应该做的是,当第一次按下鼠标时,创建一个Rectangle
(并绘制矩形),并在拖动鼠标时,用setRect()
增加矩形的大小。你可以看到一个很好的例子here。
如果您希望颜色更改是动态的(在拖动时或完成拖动时),则有两种选择:
拖动迭代矩形列表并检查列表中的每个矩形是否与拖动的矩形相交或包含。 Rectangle
从Shape
intersects()
和contains()
如果您只想在拖动完成后更改颜色,请在释放鼠标时执行与上述相同的操作。
另请注意,您不应以任何方式使用getGraphics()
进行自定义绘画。所有绘画都应该在paintComponent
方法
<强>更新强>
以下是基于我上面提出的观点的示例。该示例扩展了上面链接中提供的示例
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class RectangleDrawWithDrag extends JPanel{
private static final int D_W = 400;
private static final int D_H = 400;
private static final Color DEFAULT_COLOR = Color.GRAY;
private static final Color SELECT_COLOR = Color.BLUE;
private static final Color CURSOR_COLOR = new Color(100, 100, 100, 100);
private Point p1;
private Point p2;
private Rectangle2D rectangle;
private List<ColoredRectangle> rectangles;
public RectangleDrawWithDrag() {
rectangles = createRectangleList();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
rectangle = new Rectangle2D.Double(p1.x, p1.y, p1.x - p1.x, p1.y - p1.y);
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
p2 = e.getPoint();
if (isPointTwoInQuadOne(p1, p2)) {
rectangle.setRect(p2.x, p2.y, p1.x - p2.x, p1.y - p2.y);
} else {
rectangle.setRect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
}
checkRectangleContainment();
repaint();
}
});
}
public void checkRectangleContainment() {
for (ColoredRectangle rects: rectangles) {
if (rectangle.contains(rects.getRectangle())) {
rects.setColor(SELECT_COLOR);
} else {
rects.setColor(DEFAULT_COLOR);
}
}
}
public boolean isPointTwoInQuadOne(Point p1, Point p2) {
return p1.x >= p2.x && p1.y >= p2.y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (ColoredRectangle rect: rectangles) {
rect.draw(g2);
}
if (rectangle != null) {
g2.setColor(CURSOR_COLOR);
g2.fill(rectangle);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
private List<ColoredRectangle> createRectangleList() {
List<ColoredRectangle> rects = new ArrayList<>();
Random rand= new Random();
for (int i = 0; i < 20; i++) {
rects.add(new ColoredRectangle(rand.nextInt(D_W), rand.nextInt(D_H)));
}
return rects;
}
public class ColoredRectangle {
Rectangle2D rectangle;
Color color = DEFAULT_COLOR;
double size = 20;
public ColoredRectangle(double x, double y) {
rectangle = new Rectangle2D.Double(x, y, size, size);
}
public ColoredRectangle(double x, double y, double size) {
this.size = size;
rectangle = new Rectangle2D.Double(x, y, size, size);
}
public void setColor(Color color) {
this.color = color;
}
public Rectangle2D getRectangle() {
return rectangle;
}
public void draw(Graphics2D g2) {
g2.setColor(color);
g2.fill(rectangle);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new RectangleDrawWithDrag());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 2 :(得分:-1)
在搜索所需的类似内容时,我遇到了您的问题。我为自己的要求定制了Paul的代码。我在这里写是因为不允许发表评论here my version
$sort = 'qtSTDDEV';
$order = 'order';
DB::select(DB::raw('SELECT mid as mID,
round((x.qty_sum / x.qty_count), 5) as qtAVG,
round(x.qty_stddev, 5) as qtSTDDEV,
x.qty_count as qtCOUNT,
round((x.rel_sum / x.rel_count), 5) as relAVG,
round(x.rel_stddev, 5) as relSTDDEV,
x.rel_count as relCOUNT,
FROM (SELECT mid,
SUM( mt = 'qt' ) as qty_count,
SUM(CASE WHEN mt = 'qt' THEN rt END) as qty_sum,
STD(CASE WHEN mt = 'qt' THEN rt END) as qty_stddev,
SUM( mt = 'rel' ) as rel_count,
SUM(CASE WHEN mt = 'rel' THEN rel END) as rel_sum,
STD(CASE WHEN mt = 'rel' THEN rel END) as rel_stddev
FROM t_r r
right join t_m t on t.mid = r.mid
GROUP BY mid
) x
right join m_k m on m.mid = x.mid
right join k_d k on k.kid = m.kid
order by :sort :order;'), array('sort' => $sort, 'order' => $order));
}