[英]Paint Like JComponent Drawing Pad
I have a class Pad_Draw extending JComponent
. 我有一个类Pad_Draw扩展
JComponent
。 Constructor is 构造函数是
public Pad_Draw()
{
this.setDoubleBuffered(true);
this.setLayout(null);
};
The paintComponent methpod is : paintComponent方法是:
public void paintComponent( Graphics g )
{
graphics2D = (Graphics2D)g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
}
In a JFrame
I am adding this by a ScrollPane: 在
JFrame
我通过ScrollPane添加它:
JScrollPane Padscroller = new JScrollPane();
Padscroller.setWheelScrollingEnabled(true);
Padscroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Padscroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
Padscroller.setPreferredSize(new Dimension(200, 100));
Padscroller.setMinimumSize(new Dimension(200, 100));
Padscroller.setMaximumSize(new Dimension(200, 100));
Padscroller.setViewportView(drawPad);
content.add(Padscroller, BorderLayout.CENTER);
But as soon as I add the content in the Frame and set the size of the frame. 但是只要我在Frame中添加内容并设置框架的大小。 The drawing pad is taking whole size whatever it needs.
绘图板根据需要采用整个尺寸。
I want my specified size (200,100) to be maintained and I actually want something like Windows Paint application has. 我想要保持我指定的大小(200,100),我实际上想要像Windows Paint应用程序一样。 I should be able to increase the size by extending a corner.
我应该能够通过扩大角落来增加尺寸。 As soon as I extend corner the scrollbar gets activated.
一旦我伸出角落,滚动条就会被激活。 Can anyone give me any idea how to achieve it?
任何人都可以给我任何想法如何实现它?
The default layout manager for JFrame
, BorderLayout
does not respect preferred sizes of its components. JFrame
的默认布局管理器, BorderLayout
不尊重其组件的首选大小。
You could a layout manager that does, such as BoxLayout
, and override getPreferredSize in your Padscroller
component: 你可以使用一个布局管理器,例如
BoxLayout
,并覆盖Padscroller
组件中的getPreferredSize:
JScrollPane padScroller = new JScrollPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
};
Regarding increasing the size, have a look at using a MouseAdapter
and updating this property in the mouseDragged method. 关于增加大小,请查看使用
MouseAdapter
并在mouseDragged方法中更新此属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.