Advertisement
Guest User

OvalPlus

a guest
Sep 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. // Author: Eric Pogue
  2. // Note: Drawing an oval in red.
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import java.awt.Graphics;
  7. import java.awt.Container;
  8. import java.awt.Color;
  9.  
  10. class Oval extends JPanel {
  11. private Color myColor;
  12. public void setColor(int red, int green, int blue) {
  13. myColor = new Color(red,green,blue);
  14. }
  15. public Color getColor() {
  16. return myColor;
  17. }
  18.  
  19. Oval() {
  20. setColor(255,0,0);
  21. }
  22.  
  23. Oval(int red, int green, int blue) {
  24. setColor(red,green,blue);
  25. }
  26.  
  27. public void paintComponent(Graphics g) {
  28. super.paintComponent(g);
  29.  
  30. int panelWidth = getWidth();
  31. int panelHeight = getHeight();
  32. g.setColor(Color.blue); // Setting color for rectangle
  33. g.fillRect(0,0,panelWidth,panelHeight/2); // Drawing the rectangle
  34. g.setColor(myColor); // Setting color for oval
  35. g.fillOval(0,0,panelWidth,panelHeight); // Draweing the oval
  36. }
  37. }
  38.  
  39. class Rectangle extends JPanel {
  40. private Color myColor;
  41. public void setColor(int red, int green, int blue) {
  42. myColor = new Color(red,green,blue);
  43. }
  44. public Color getColor() {
  45. return myColor;
  46. }
  47.  
  48. Rectangle() {
  49. setColor(255,0,0);
  50. }
  51.  
  52. Rectangle(int red, int green, int blue) {
  53. setColor(red,green,blue);
  54. }
  55. public void paintComponent(Graphics g) {
  56. super.paintComponent(g);
  57.  
  58. int panelWidth = getWidth();
  59. int panelHeight = getHeight();
  60.  
  61. g.setColor(myColor);
  62. g.fillRect(0, 0, panelWidth, panelHeight/2);
  63.  
  64. }
  65. }
  66.  
  67. class OvalFrame extends JFrame {
  68. OvalFrame() {
  69. setTitle("OvalDraw");
  70. setBounds(200,200,300,400);
  71.  
  72. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73.  
  74. Oval myOval = new Oval(100, 200, 100);
  75. Rectangle myRectangle = new Rectangle(0, 0, 150);
  76. Container contentPane = getContentPane();
  77. contentPane.add(myRectangle);
  78. contentPane.add(myOval);
  79. }
  80. }
  81.  
  82. public class OvalDrawPlus {
  83. public static void main(String[] args) {
  84. System.out.println("OvalDraw Starting...");
  85. OvalFrame myFrame = new OvalFrame();
  86.  
  87. myFrame.setVisible(true);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement