java 覆盖paintComponent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15602235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:13:27 来源:igfitidea点击:
Overriding paintComponent
提问by bob
Ok so I have been trying to figure this out for the past 26 hours with guide and online help but no success.
好的,所以我在过去 26 小时内一直试图通过指南和在线帮助解决这个问题,但没有成功。
All I want to do is draw an oval when the user clicks in the PaintPanel Please someone help so I can sleep :P
我想要做的就是在用户单击 PaintPanel 时绘制一个椭圆 请有人帮忙,这样我就可以睡觉了:P
In PaintApplet class:
在 PaintApplet 类中:
private void PaintPanelMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(FillRadioButton.isSelected())
{
PaintPanel.setBackground(JColor.getColor());
PaintPanel.repaint();
}
if(BrushRadioButton.isSelected())
{
Point ComponentPoint = PaintPanel.getLocationOnScreen();
PaintPanel.add(new Painter(ComponentPoint));
}
}
Painter Class:
画家班:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
public class Painter extends JPanel{
Point Component;
public Painter(Point Com)
{
Component = Com;
}
public void paintComponent(Graphics g)
{
super.paint(g);
Point CursorPoint= MouseInfo.getPointerInfo().getLocation(); //gets cursorpoint
int ComPX = Component.x;
int ComPY = Component.y;
int CurPX = CursorPoint.x;
int CurPY = CursorPoint.y;
int FinalX = CurPX - ComPX;
int FinalY = CurPY - ComPY;
g.drawOval(FinalX, FinalY, 20, 20);
}
}