Java在多个JFrame中绘制两种不同的形状

时间:2017-11-20 16:24:10

标签: java swing java-2d

我有一个问题

我创建了一个JComponent派生类来绘制图形。我想要一个包含g1的JFrame和一个包含g2的第二个。我只得到g1或g2的副本,但不是第1帧中的g1和第2帧中的g2为什么?

public class DRAWINGS_COMPONENT extends JComponent{
/**
 * 
 */
public void draw1(Graphics g1) {
    Graphics2D g = (Graphics2D) g1.create();
    g.drawRect(0, 0, 60, 60);       
}
/**
 * 
 */
public void draw2(Graphics g1) {
    Graphics2D g = (Graphics2D) g1.create();
    g.drawOval(0, 0, 60, 60);
}

和有问题的班级

@SuppressWarnings("serial")
public class GRAPHICS_REPRESENTATION extends JFrame {
private DRAWINGS_COMPONENT dc;
private DRAWINGS_COMPONENT dc2;

public GRAPHICS_REPRESENTATION() {
    dc=new DRAWINGS_COMPONENT();
    dc.draw1(getGraphics());
    JFrame f=new JFrame("graphic global");      
    f.add(dc);
    f.setSize(800, 800);
    f.setVisible(true); 
}

/**
 * @param x1 
 * @param y1 
 * @param g 
 * @param scene_index 
 * 
 */
public void regenerateGraphics(int x1, int y1) {
    dc2=new DRAWINGS_COMPONENT();
    dc2.draw2(getGraphics());
    JFrame f=new JFrame("graphic local");       
    f.add(dc2);     
    f.setSize(800, 800);
    f.setLocation(x1, y1);      
    f.setVisible(true); 
}

和主

public static void main(String[] args) {
GRAPHICS_REPRESENTATION g = new GRAPHICS_REPRESENTATION();
g.regenerateGraphics(50, 50);
}

0 个答案:

没有答案