-1

我正在尝试在 Java 中创建 2 个不同的 JFrame。一帧显示未着色的形状(只是轮廓),而另一帧显示彩色形状。我的问题是绘制第二个(彩色)帧以及如何调用不同的绘制方法来匹配另一个帧。这是代码:

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

0

paint()您的方法和方法之间的唯一区别paint2()是,在第二个方法中,您还有第一个方法中colored.setColor(Color.BLUE)缺少的行。

因此,您可以将所有内容放入paint()并在类上创建一个参数,以Shapes确定您是否要设置颜色:

public class Shapes extends JFrame {
    private double diameter;
    private double radius;
    private boolean drawColored;

    public Shapes(double diameter, boolean drawColored) { // Parameters in constructor
        setSize(600, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.diameter = diameter;
        this.drawColored = drawColored;

        this.radius = diameter / 2;
    }

    // Made this static and have it return the diameter the user entered
    // I also renamed it because in Java methods starting with 'get' are usually getters
    public static double promptDiameter() {
        String input = JOptionPane.showInputDialog("What is the diameter of the circle?");
        return Double.parseDouble(input);
    }

    public static void main(String[] args) {
        double diameter = promptDiameter();
        Shapes appUncolored = new Shapes(diameter, false);
        appUncolored.setVisible(true);
        Shapes appColored = new Shapes(diameter, true);
        appColored.setVisible(true);
    }

    @Override
    public void paint(Graphics canvas) {
        double inner_square_side = Math.sqrt(2 * ((radius) * (radius)));
        if (drawColored) {
            canvas.setColor(Color.BLUE);
        }
        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);
    }
}

请注意,我还将提示用户输入直径的职责从JFramemain 方法中移出。我这样做是因为我们希望用户设置一次直径,然后显示多个具有相同直径形状的 JFrame。

现在JFramediameterdrawColored参数。因此,您现在可以创建多个具有不同行为的 JFrame。

请注意:如果行为差异不仅仅是“设置颜色或不设置颜色”,那么您可能应该创建多个类!

于 2013-09-10T18:50:56.837 回答