[英]Drawing shapes over an ImagePanel
I am new to swing and I have been following the tutorials at the Oracle website. 我是新手,我一直在关注Oracle网站上的教程。 So I have an imagePanel class using which, I paint an image onto a JPanel
. 所以我有一个imagePanel类,使用它可以将图像绘制到JPanel
。 Next, I need to draw shapes over this image. 接下来,我需要在该图像上绘制形状。 So I call the drawShapes
function in the paintComponent
method of the imagePanel class. 因此,我在imagePanel类的paintComponent
方法中调用了drawShapes
函数。 The issue that I am facing is, whenever I draw any shape say rect
or oval
it doesnt not draw the shape according to the position I specify for the y-axis. 我面临的问题是,每当我绘制任何形状(例如rect
或oval
,都不会按照我为y轴指定的位置绘制形状。 It only takes the x-axis into account. 它仅考虑x轴。 So ideally, 所以理想情况下
fillOval(30,70,10,10)
is tantamount to fillOval(30,30,10,10)
Am i doing something wrong or is there some way to overcome this? fillOval(30,70,10,10)
等同于fillOval(30,30,10,10)
我是在做错什么还是有某种方法可以克服这个问题?
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String loc)
{
this(new ImageIcon(loc).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
renderShapes(g);
}
private void renderShapes(g){
Graphics2D g2d = (Graphics2D)g;
g2d.fillOval(20,70,10,10);
g2d.fillRect(120,40,10,10);
}
}
EDIT The renderShapes
method for the screenshot provided is posted below. 编辑下面提供了提供的屏幕快照的renderShapes
方法。
private void renderShapes(g){
Graphics2D g2d = (Graphics2D)g;
g2d.fillRect(220,50,10,10);
g2d.fillRect(20,140,10,10);
}
You have to understand that how x, y coordinate works in Swing custom drawing to position the component. 您必须了解x,y坐标在Swing自定义工程图中如何定位组件。
Try to understand the below screenshot. 尝试了解以下屏幕截图。
Find a sample code here to understand the same concept 在此处找到示例代码以了解相同的概念
Try with this sample code to understand the x and y coordinate using grids separated by 10 pixels and look at the shapes whether is it at the correct position or not? 尝试使用此示例代码,以使用由10个像素分隔的网格来了解x和y坐标,并查看形状是否位于正确的位置?
Sample code: 样例代码:
class DrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
for (int i = 0; i < getHeight(); i = i + 10) {
g.drawLine(0, i, getWidth(), i);
}
for (int i = 0; i < getWidth(); i = i + 10) {
g.drawLine(i, 0, i, getHeight());
}
g.setColor(Color.RED);
g.fillOval(20, 70, 10, 10);
g.setColor(Color.GREEN);
g.fillRect(120, 40, 10, 10);
}
}
DrawPanel drawPanel = new DrawPanel();
drawPanel.setBackground(Color.WHITE);
snapshot: 快照:
Use JComponent#getPreferredSize() instead of setPreferredSize()
. 使用JComponent#getPreferredSize()代替setPreferredSize()
。
Read more Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 阅读更多我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.