我正在尝试使用MouseListeners绘制一条线并覆盖paintComponent方法。我现在设置了paintComponent,它在面板上自行绘制一条线。但现在我需要将其更改为当您单击面板并将鼠标拖动到另一个点以创建一条线时。我一直在阅读所有这些教程,我仍然很丢失。谁可以指导我?
到目前为止,这是我的代码:
package gui;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1697489704611349844L;
/** The width of the panel. */
private static final int WIDTH = 600;
/** The height of the panel. */
private static final int HEIGHT = 300;
/** The stroke width in pixels. */
private static final int STROKE_WIDTH = 1;
/** The width for the rectangle. */
private static final int RECTANGLE_WIDTH = 50;
/** The height for the rectangle. */
private static final int RECTANGLE_HEIGHT = 50;
public DrawPanel() {
super();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
class LineListener implements MouseListener, MouseMotionListener {
@Override
public void mouseClicked(MouseEvent theEvent) {
theX = theEvent.getX();
theY = theEvent.getY();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent theEvent) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void paintComponent(final Graphics theGraphics) {
super.paintComponent(theGraphics);
final Graphics2D g2d = (Graphics2D) theGraphics;
// for better graphics display
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
final Shape line = new Line2D.Double(10, 10, 100, 100);
g2d.setPaint(Color.ORANGE);
g2d.setStroke(new BasicStroke(STROKE_WIDTH));
g2d.draw(line);
}
public static void main(final String[] theArgs) {
final DrawPanel panel = new DrawPanel();
final JFrame frame = new JFrame("Drawing Panel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
答案 0 :(得分:2)
您正在对线路参数10,10,100,100进行硬编码:
final Shape line = new Line2D.Double(10, 10, 100, 100);
制作一个非常不灵活的结构 - 当用户点击并拖动时,你的代码无法改变这一点。解决方法不是这样做,而是使用硬编码的“魔术”数字来使用int变量,这些变量是在代码的MouseListener中更改的变量。在同一个MouseListener内部,您可以在更改参数值后调用repaint()
,这将建议JVM重新绘制GUI,然后重新调用paintComponent
方法。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class DrawPanel2 extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 300;
private static final Color BG = Color.WHITE;
// create fields that can be set in your mouse listeners
// and used in the painting methods
private Point p1 = null;
private Point p2 = null;
public DrawPanel2() {
setBackground(BG);
// create your mouse adapter and add as mouse and mouse motion listeners
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// if your points have been set,.... draw them!
if (p1 != null && p2 != null) {
int x1 = p1.x;
int y1 = p1.y;
int x2 = p2.x;
int y2 = p2.y;
g2.drawLine(x1, y1, x2, y2);
}
}
// to set the size of the GUI
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// my mouse adapter inner class
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
p2 = e.getPoint();
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
p2 = e.getPoint();
repaint();
}
}
private static void createAndShowGui() {
DrawPanel2 mainPanel = new DrawPanel2();
JFrame frame = new JFrame("Draw Panel 2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
关于您发布的代码:
答案 1 :(得分:0)
查看鼠标侦听器方法。您将需要一个鼠标侦听器,用于存储已单击或拖动的点列表。然后在paintComponent()
中,您可以用与当前绘图相同的方式绘制这些点。
关于鼠标侦听器的课程:http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html