-1

Java で 2 つの異なる JFrame を作成しようとしています。1 つのフレームには色が付けられていない形状 (アウトラインのみ) が表示され、もう 1 つのフレームには色付きの形状が表示されます。私の問題は、2番目の(色付きの)フレームをペイントすることと、この他のフレームに一致するように別のペイントメソッドを呼び出す方法です。コードは次のとおりです。

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Color;

public class Shapes extends JFrame
{
    double diameter;
    double radius;    

    public Shapes()
    {
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }       

    public void getDiameter()
    {
        String input = JOptionPane.showInputDialog("What is the diameter of the circle?");
        diameter = Double.parseDouble(input);
        radius = diameter / 2;    
        /** ignore this stuff
        double area = Math.PI * (radius * radius);          
        double circum = Math.PI * diameter;
        double square_enclosing = diameter * diameter;
        double square_enclosed = 2 * (radius * radius);         
        JOptionPane.showMessageDialog(null, "The diameter of the circle is " + diameter + "\nThe radius of the cricle is " + radius + "\nThe area of the cirlce is " + area + "\nThe circumference of the circle is " + circum);
        JOptionPane.showMessageDialog(null, "The area of the smallest square enclosing this circle is " + square_enclosing + "\nThe area of the largest square enclosed in this circle is " + square_enclosed);
        */          
    }

    public static void main(String[] args) 
    {       
        Shapes app = new Shapes();
        app.getDiameter();
        app.setVisible(true);
        Shapes app2 = new Shapes();
        app2.setVisible(true);
    }

    public void paint(Graphics canvas)
    {
            double inner_square_side = Math.sqrt(2 * ((radius) * (radius)));
            canvas.drawRect(50,  50, (int)diameter, (int)diameter);
            canvas.drawOval(50, 50, (int)diameter, (int)diameter);
            canvas.drawRect((int)(50 + (.1475 * diameter)), (int)(50 + (.1475 * diameter)), (int)inner_square_side, (int)inner_square_side);
    }

    public void paint2(Graphics colored)
    {
            double inner_square_side = Math.sqrt(2 * ((radius) * (radius)));
            colored.setColor(Color.BLUE);
            colored.drawRect(50,  50, (int)diameter, (int)diameter);
            colored.drawOval(50, 50, (int)diameter, (int)diameter);
            colored.drawRect((int)(50 + (.1475 * diameter)), (int)(50 + (.1475 * diameter)), (int)inner_square_side, (int)inner_square_side);
    }       
}

別の描画方法を使用して別の Jframe を描画する方法についてのアイデアはありますか? ありがとう。

4

1 に答える 1