120 likes | 133 Views
Learn how to integrate graphics into Java applications using JPanel, JFrame, and layout managers for dynamic visual experiences.
E N D
Adding Graphics to a Frame Application • Applets: Can generate drawings by overriding paint • Frame: Do not draw directly on a frame . Draw graphics on a JPanel object, which can paint itself • To do this: 1. Create a JPanel type class which can display your graphic 2. Add an object of this type to your frame, and show it
Create your JPanel type class by: 1. extending JPanel, so your class will BE a JPanel 2. set up similar to an applet… * constructor handles initialization and definition of any event handlers * override paintComponent instead of paint for drawing (NOTthe paint method !!) Important: Your method paintComponent must call super.paintComponent to paint the background.
public class TriPanel extends JPanel{ //design my own JPanel Triangle mytri = new Triangle(10,10); //one triangle drawn and redrawn public TriPanel () { //set up panel and listener setPreferredSize( new Dimension(200,200)); class MListen extends MouseAdapter { //redraw when mouse clicks public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); mytri.setTri(x,y); repaint(); } } MListen mousel = new MListen(); addMouseListener(mousel); }
//continued public void paintComponent(Graphics g) { //override JPanel method super.paintComponent(g); //important!! Graphics2D gg = (Graphics2D) g; mytri.draw(gg); //triangle redraws itself } }
//a JFrame can now use the class we have designed public class TriFrame{ public static void main (String [] args) { TriPanel mypanel = new TriPanel(); //create the panel JFrame myframe = new JFrame(); //put it in frame and show myframe.setContentPane(mypanel); //put it in frame myframe.pack(); myframe.show(); } }
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout ( layoutmanager object); By default a panel’s layout is being managed by a FlowLayout() object. FLOW LAYOUT - components are added left to right, starting a new row if needed Different layouts: flow layout , border layout , grid layout
Border Layout Border layout places components into positions (center, north, west, south, eas Set up and use of Border layout panel.setLayout(new BorderLayout()); panel.add(myBtn, BorderLayout.SOUTH); *Border layout grows components to fit area(Flow layout retains size) *To avoid growth, place component into another panel (with flow layout), then add panel to content pane
Grid Layout • Lays out components in rows and columns • All components have the same size • Add components left to right (top row first, then second row, etc.) • panel.setLayout(new GridLayout(4, 3));panel.add(button7);panel.add(button8);panel.add(button9);panel.add(button4);
The default layout of a frame is Border Layout. This layout provides for many panels to be placed on the frame. as Frames can begin to get complicated when layouts are added, it is a good idea to have methods which create the individual panels .. see Comp.java on website and a Frame itself is a component, whose code should not complicate an application… When a frame class extends JFrame, the main method of an application can just instantiate the frame…… (or create multiple instances if applicable) see FinalComp.java on website