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