The above output is produced by the following Java code:
import javax.swing.*; import java.awt.*; public class Graph2a { public static void main(String[] args) { SwingApp app = new SwingApp(450, 250); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class SwingApp extends JFrame { private final DrawPanel panel; // class variable public SwingApp( int width, int height) // class constructor { super(); // call to super class constructor Container pane = super.getContentPane(); // create layout, set colors panel = new DrawPanel(); // add GUI components panel.setBackground(Color.yellow); pane.add(panel); // add here more code for GUI components Toolkit toolkit = Toolkit.getDefaultToolkit(); // optionally position JFrame Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2); super.setTitle("Swing app"); // set desired window title super.setSize(width, height); // set desired window size super.setVisible(true); } } class DrawPanel extends JPanel // main window panel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // must be the 1st line // add here more code for drawing on panel // setting background and foreground colors g.setColor(new Color(220, 220, 220)); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.red); // draw filled arcs g.fillArc(10, 50, 100, 100, 20, 320); g.fillArc(150, 50, 100, 100, 340, 40); // draw nonfilled arc g.drawArc(300, 50, 100, 100, 20, 320); } } |
The above output is produced by the following Java code:
import javax.swing.*; import java.awt.*; public class Graph2b { public static void main(String[] args) { SwingApp app = new SwingApp(450, 250); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class SwingApp extends JFrame { private final DrawPanel panel; // class variable public SwingApp( int width, int height) // class constructor { super(); // call to super class constructor Container pane = super.getContentPane(); // create layout, set colors panel = new DrawPanel(); // add GUI components panel.setBackground(Color.yellow); pane.add(panel); // add here more code for GUI components Toolkit toolkit = Toolkit.getDefaultToolkit(); // optionally position JFrame Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2); super.setTitle("Swing app"); // set desired window title super.setSize(width, height); // set desired window size super.setVisible(true); } } class DrawPanel extends JPanel // main window panel { @Override public void paintComponent(Graphics g) { super.paintComponent(g); // must be the 1st line // add here more code for drawing on panel int xPts[] = {42, 52, 72, 52, 60, 40, 15, 28, 9, 32}; int yPts[] = {38, 62, 68, 80, 105, 85, 102, 75, 58, 60}; int XPts[] = {142, 152, 172, 152, 160, 140, 115, 128, 109, 132}; // setting background and foreground colors g.setColor(new Color(220, 220, 220)); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.red); // draw a non-filled polygon g.drawPolygon(xPts, yPts, xPts.length); // draw a filled polygon g.setColor(Color.blue); g.fillPolygon(XPts, yPts, xPts.length); // alternative way for drawing the above polygon // Polygon star = new Polygon(XPts, yPts, xPts.length); // g.fillPolygon(star); // copying an area g.copyArea(0, 0, 75, 105, 200, 50); g.copyArea(0, 0, 75, 105, 300, 50); } } |