Note: This uses Graphics2D to get better graphics.
Buttons are added to the south panel, but no event listeners have been implemented, so the buttons do nothing.
import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingGraphics { public static void main(String[] args) { new SwingGraphics(); } //Constants //set the size of the drawing panel final static int panW = 800; final static int panH = 800; //Global variables GraphicsPanel panel; //Constructor - we'll put the main organization here SwingGraphics(){ JFrame window = new JFrame("Drawing on a JPanel"); window.setResizable(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new GraphicsPanel(); window.add(panel); //defaults to BorderLayout.CENTER JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GRAY.darker()); buttonPanel.add(new JButton("Jump")); buttonPanel.add(new JButton("Run")); buttonPanel.add(new JButton("Swing")); window.add(buttonPanel, BorderLayout.SOUTH); window.pack(); //needed in order to use the GraphicsPanel's size to set the JFrame size window.setLocationRelativeTo(null); //centre the window, must be after pack() window.setVisible(true); } //We extend JPanel so we can customize it. This is where the magic happens... private class GraphicsPanel extends JPanel { GraphicsPanel(){ this.setBackground(Color.BLACK); this.setPreferredSize(new Dimension(panW, panH)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); //erases screen and draws background colour Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(new BasicStroke(3)); //Do all your drawing here g2.setColor(Color.CYAN); g2.drawOval(100,100,100,100); } } }
This just has a GraphicsPanel that you can draw on
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingGraphics { public static void main(String[] args) { new SwingGraphics(); } //Constants //set the size of the drawing panel final static int panW = 800; final static int panH = 700; //Global variables GraphicsPanel panel; SwingGraphics(){ JFrame window = new JFrame("Drawing on a JPanel"); window.setResizable(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel = new GraphicsPanel(); window.add(panel); //defaults to BorderLayout.CENTER window.pack(); //needed in order to use the GraphicsPanel's size to set the JFrame size window.setLocationRelativeTo(null); //centre the window, must be after pack() window.setVisible(true); } private class GraphicsPanel extends JPanel { GraphicsPanel(){ this.setBackground(Color.WHITE); this.setPreferredSize(new Dimension(panW, panH)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); //Do all your drawing here g.setColor(Color.BLUE); g.fillOval(100,100,100,100); } } }