Helo家伙,
我的问题是,有时如果我的Rectangle2D的宽度或高度很大,它的底部,左边框分裂并且不再形成连续的边框,如果我甚至使它变宽,边框会变得越来越小,就像有限制一样矩形边框可以多长时间...这真的令人困惑,到目前为止我还没有找到解决方案:SI将链接放到图片上,以便您自己查看。
new Rectangle2D.Double(mojrectangle.getUpperleftPointmojRectangle().getX(), mojrectangle.getUpperleftPointmojRectangle().getY(),1000,1000)
repaint();
感谢您的帮助..
顺便说一句,如果它变得非常大,我对Arc2D也有同样的问题
更新:我从函数中删除了setStroke命令,现在它正确地绘制它,但是将来我需要设置矩形笔划让它让我感到难过。
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//g2.setStroke(stroke);
g2.draw(rectangle);
}
这里我放了一个我项目的示例代码,请用g2.setStroke(selectedstroke)试试 - 它不会工作,没有它......我希望我能解释清楚。
package com.awtgraphicsdemo;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.JComboBox;
public class AWTgraphicsdemo extends Frame {
final float dash[] = {10.0f};
final float solid[] = {1000.0f}; // these must be in an Array
float lineWidth[]={2.0f,4.0f,6.0f,8.0f,10.0f}; // width of the drawing line
String[] lineWidthString={"2.0","4.0","6.0","8.0","10.0"};
JComboBox strokecombobox=new JComboBox(lineWidthString);
BasicStroke selectedStroke = new BasicStroke(lineWidth[0],BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, solid, 0.0f);
public AWTgraphicsdemo(){
super("Java AWT Examples");
prepareGUI();
}
public static void main(String[] args){
AWTgraphicsdemo awtGraphicsDemo = new AWTgraphicsdemo();
awtGraphicsDemo.setVisible(true);
}
private void prepareGUI(){
setSize(400,400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(selectedStroke);
g2.draw (new Rectangle2D.Double(10, 10, 1000, 900));
Font font = new Font("Serif", Font.PLAIN, 24);
g2.setFont(font);
g.drawString("Welcome to TutorialsPoint", 50, 70);
g2.drawString("Rectangle2D.Rectangle", 100, 120);
}
}
答案 0 :(得分:1)
Helo,
我想出了我的问题,它是在笔画的属性中,所以在一些长度的组件之后,笔画被激活,这改变了绘制的形状。通过修改笔画实体数组我能够得到结果我想要的。
感谢您的帮助和建议:)
保重
答案 1 :(得分:0)
最好:
paint(...)
(如MadProgrammer所述),因为这也会改变边框和子组件的绘制 - 这是一件危险的事情。paintComponent(...)
(再次作为MadProgrammer说明)并将该JPanel放入您的顶级窗口。如,
public class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setStroke(....);
// do drawing with g2 here
g2.dispose();
}
}