- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我这里有两个类(class):
照片组件类:
(此类用于将特定图像作为 JComponent 进行处理。当“翻转”时,我想绘制笔划而不是图像。因此我用矩形替换图像,尝试在其上绘制笔划。)
public class PhotoComponent extends JComponent {
private Image pic;
private boolean flipped;
private int contentAreaWidth;
private int contentAreaHeight;
p
@Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
System.out.println("Draw: " + draw + ", Pic: " + pic);
if (draw && pic != null) {
super.paintComponent(g);
System.out.println("width using this: " + this.getWidth() + ", actual width of JPanel: " + contentAreaWidth);
System.out.println("height using this: " + this.getHeight() + ", actual height of JPanel: " + contentAreaHeight);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x,y,pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (drawingMode) {
g2.setPaint(Color.RED);
if (drawOval) {
penStrokes.put(ovalX, ovalY);
if (penStrokes != null) {
for (Integer xCoor : penStrokes.keySet()) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((xCoor - (brushSize / 2)), (penStrokes.get(xCoor) - (brushSize / 2)), brushSize, brushSize);
//System.out.println("SIZE OF HASHTABLE: " + penStrokes.size());
}
}
System.out.println("Filling an oval!" + ovalX + ", " + ovalY);
}
} else if (textMode) {
g2.setPaint(Color.YELLOW);
if (drawRect) {
rectDimensions.add(rectX);
rectDimensions.add(rectY);
rectDimensions.add(rectWidth);
rectDimensions.add(rectHeight);
for (int i = 0; i < rectDimensions.size(); i+=4) {
g2.fillRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
g2.drawRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
}
}
}
System.out.println("This is being called again!");
}
}
}
public void setRectangle(int x, int y, int width, int height) {
drawRect = true;
rectX = x;
rectY = y;
rectWidth = width;
rectHeight = height;
}
public void removeRectangle() {
drawRect = false;
}
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX =
注意上面的 Drawline() 方法。我正在给定点绘图,重新绘制,并将旧变量设置为当前变量。
主类:
private static PhotoComponent img;
private static JFrame frame;
private static JPanel contentArea;
//Mouse Coordinates for PenStrokes
private static int oldX, oldY;
//Mouse Coordinates for Sticky Notes
private static Point clickPoint;
public static void main (String[] args) {
frame = new JFrame("PhotoManip");
img = null;
contentArea = null;
oldX = 0;
oldY = 0;
setupMenubar(frame);
setupJFrame(frame);
}
private static void addPhotoComponent(File file) {
}
if (img.getTextMode()) {
img.removeRectangle();
clickPoint = null;
}
}
});
img.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (img.getDrawingMode()) {
if (withinRange(e.getX(), e.getY())) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
img.repaint();
}
}
if (img.getTextMode()) {
if (withinRange(e.getX(), e.getY())) {
Point dragPoint = e.getPoint();
h, height);
img.repaint();
}
}
}
});
if (img!=null) {
contentArea.add(img);
}
}
private static boolean withinRange(int x, int y) {
if (x > img.getX() && x < img.getX() + img.getWidth()) {
if (y > img.getY() && y < img.getY() + img.getHeight()) {
return true;
}
}
return false;
}
private static void flipImage() {
if (!img.isFlipped()) {
img.setFlipped(true);
} else if (img.isFlipped()) {
img.setFlipped(false);
}
}
当鼠标拖动发生时,在这个主类中调用上面的drawLine()。问题是笔画似乎没有显示。
我知道程序正在调用 g2.fillOval(),因为我随后打印了一条验证语句。
此外,我已经创建了当按下并拖动鼠标时的打印语句,并且它们获得了正确的坐标?
为什么不出现红色笔画?我很困惑。这是我的代码的结构方式吗?
最佳答案
问题的症结在于您试图在 paintComponent
方法之外绘制某些内容,而该方法永远不受支持。无论您绘制什么,都将被下一次调用 paintComponent
覆盖,这几乎会立即发生。我们可以通过存储椭圆的坐标并在 PaintComponent 中绘制它来解决此问题,而不是尝试在 PaintComponent 方法外部的图形对象上绘制。请参阅下面的代码:
首先,我们将向您的 PhotoComponent
类添加以下变量:
private boolean drawOval = false;
private int ovalX = 0;
private int ovalY = 0;
然后我们将添加控制它们的方法:
public int[] setOval(int currentX, int currentY) {
drawOval = true;
int[] toReturn = {ovalX, ovalY};
ovalX = currentX;
ovalY = currentY;
return toReturn;
}
public void removeOval() {
drawOval = false;
}
之后,我们可以更改 paintComponent
方法,让它根据这些变量绘制椭圆形:
@Override
public void paintComponent(Graphics g) {
//Does all the drawing and contains whatever state information is associated with the photo
//create an action event to auto call repaint
//call repaint anytime flip was changed to true or false
super.paintComponent(g);
g2 = (Graphics2D) g;
int x = (contentAreaWidth - pic.getWidth(null)) / 2;
int y = (contentAreaHeight - pic.getHeight(null)) / 2;
if (!flipped) {
g2.drawImage(pic, x, y, null);
} else if (flipped) {
g2.setColor(Color.WHITE);
g2.fillRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.RED);
}
//took the code you already used for drawing the oval and moved it here
if (drawOval) {
g2.setPaint(Color.RED);
int brushSize = 5;
g2.fillOval((ovalX - (brushSize / 2)), (ovalY - (brushSize / 2)), brushSize, brushSize);
}
}
最后更改 addPhotoComponent
方法来更新这些变量,而不是尝试直接绘制椭圆形:
private static void addPhotoComponent(File file) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e2) {
System.out.println("ERROR: Couldn't get image.");
}
img = new PhotoComponent(image, contentArea);
img.revalidate();
img.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// your code here
System.out.println("You flipped the photo!!!");
flipImage();
img.repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
img.setOval(e.getX(), e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
img.removeOval();
}
});
img.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int[] toUpdate = img.setOval(e.getX(), e.getY());
oldX = toUpdate[0];
oldY = toUpdate[1];
}
});
if (img != null) {
contentArea.add(img);
}
}
关于java - 使用 java swing 绘制笔画...笔画不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39901185/
我正在开发一个基于fabricjs 的图表工具。我们的工具有我们自己的形状集合,它是基于 svg 的。我的问题是当我缩放对象时,边框(描边)也会缩放。我的问题是:如何缩放对象但保持笔触宽度固定。请检查
我在 Canvas 上画线时遇到一点问题, 基本上我希望线条漂亮、柔软且半不透明,但 Canvas 似乎只想对最后渲染的线段执行此操作。 看看这里,您会看到最后绘制的线段很好,而且..我想要它)但是随
也许这是一个错误,但请检查一下。 https://codepen.io/Firsh/pen/LegGQq /* Only these work:*/ /* symbol{ overflow: visi
我已经在终端中启动了一个 python 脚本(该终端已关闭)并将其发送到后台。现在这个程序需要一些来自键盘的输入并一直等待。如何将输入“y\n”(字母“y”后跟一个 Enter)发送到该程序?假设它的
我想实现这样的效果: 有人知道如何在 Canvas 上画这样一条线吗? 最佳答案 再靠近一点: chalkPaint = new Paint(); chalkPaint.setStyl
我建立了一个very simple Twitter Instant Search为了好玩,使用 jQuery 和 PHP。我将一个事件绑定(bind)到搜索表单上的 keyup,并对 PHP 页面进行
使用通用 Windows 平台 (UWP) 使用 InkCanvas 控件 我似乎无法确定在使用 InkCanvas 时删除墨迹笔划的正确方法 - 有一个可以处理的事件“StrokeErased”。
我是一名优秀的程序员,十分优秀!