使用Java中的绘制方法绘制2个不同的框架

时间:2013-09-10 17:57:31

标签: java swing jframe paint

我正在尝试用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的任何想法?感谢。

1 个答案:

答案 0 :(得分: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);
    }
}

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

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

小心:如果行为上的差异大于“设置颜色与否”,您应该创建多个类!