2017-05-25 65 views
1

嗨,大家好我在这里新来的,因此我提前对我的模糊问题表示歉意。 我有一个学校项目完成,它的目标是创建一个完全正常的Paint程序。 我们有3个班。椭圆形,线和多边形。这些类的工作大部分都是相同的,主要区别在于它们绘制的形式。其中一个类看起来像这样:使用PaintComponent在面板外绘制

public class Oval extends Drawable{ 
    private int x1,y1,x2,y2; 
    private Color c; 
    private JFrame f; 
/** 
* Constructor of the Oval Class 
* Initialises the attributes of this Class 
* 
* @return void 
*/ 
public Oval(int X, int Y, int width, int height, Color c){ 
    this.x1 = x1; 
    this.y1= y1; 
    this.x2 = x2; 
    this.y2 = y2; 
    this.c = c; 
} 
/** 
* Draws an Oval based on the Values x1,y1,x2,y2 
* 
* @return void 
*/ 
@Override 
public void draw(Graphics g) { 
    g.setColor(c); 
    g.drawOval(x1, y1, x2, y2); 
} 
} 

现在我的问题是,我不知道如何从我的面板调用此类。当我尝试在PaintComponent方法内从我的JPanel中调用绘制(...)时,它绝对没有任何效果。 这是我的JPanel类,我添加到我的JFrame fyi。

public class PaintPanel extends JPanel { 
    private PaintFrame f; 
public PaintPanel(PaintFrame f){ 
    this.f = f; 
} 
@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Oval o = new Oval(100, 100, 50, 50, new Color(127, 184, 255), f); 
    o.draw(g); 
} 
} 

不介意的参数,这是为椭圆形,线,面类中的克隆方法,以避免OutOfBounce图纸框架。

现在对于我的框架:

public class PaintFrame extends JFrame{ 
    private PaintPanel pp; 
public PaintFrame(){ 
    pp = new PaintPanel(this); 

    this.setSize(500, 500); 
    this.setTitle("Paint"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setContentPane(pp); 
    this.setVisible(true); 
} 
} 

因此,这是相当多了,我猜。我只想完成这项工作,因为这几乎是整个项目的重要部分。提前感谢您的任何帮助,如果您有任何提示,使我的下一个问题更好,更精确随时批评:)

+0

尝试加入'pp.repaint();''后this.setVisible(真); ' – CraigR8806

+0

不会改变任何东西^^仍然thx的评论 – SecondTry

+0

好吧,我没有用'setContentPane()'做很多事情,但是尝试留下'repaint()'方法并改变'this.setContentPane pp)''this.setLayout(new BorderLayout()); this.add(pp,BorderLayout.CENTER);' – CraigR8806

回答

0

它看起来像您的Oval的坐标没有正确设置在Oval构造函数。你需要做的是使用初始x和y位置和宽度值和高度,像这样计算它们:

this.x1 = X; 
this.y1= Y; 
this.x2 = x+width; 
this.y2 = y+height;