1 / 12

Adding Graphics to a Frame Application

Learn how to integrate graphics into Java applications using JPanel, JFrame, and layout managers for dynamic visual experiences.

Download Presentation

Adding Graphics to a Frame Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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); }

  4. //continued public void paintComponent(Graphics g) { //override JPanel method super.paintComponent(g); //important!! Graphics2D gg = (Graphics2D) g; mytri.draw(gg); //triangle redraws itself } }

  5. //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(); } }

  6. 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

  7. 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

  8. Components Expand to Fill BorderLayout Area

  9. 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);

  10. Combining Layout Managers

  11. 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

More Related