Painting 2 different Frames using paint methods in Java
I am trying to create 2 different JFrames in Java. One frame displays
shapes which are uncolored (just the outlines) while the other frame has
the colored shapes. My issue is painting the 2nd (colored) frame and how I
can call a different paint method to match this other frame. Here is the
code:
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);
}
}
Any ideas on how to paint another Jframe using a separate painting method?
Thanks.
No comments:
Post a Comment