import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Title: Bresenham's line algorithm * Description: * Copyright: Copyright (c) 2000 * Company: * @author Glenn Rowe * @version 1.0 */ public class MainFrame extends JFrame { JPanel contentPane; JPanel linePanel = new JPanel(); LineCanvas lineCanvas = new LineCanvas(); JLabel instructionLabel = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); /** * Construct the frame */ public MainFrame() { // Allows main frame to respond to window closing // and resizing events enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.COMPONENT_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /** * Component initialization */ private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); this.getContentPane().setBackground(new Color(255, 200, 122)); this.setFont(new java.awt.Font("SansSerif", 1, 12)); this.setSize(new Dimension(600, 400)); this.setTitle("Bresenham's line algorithm"); contentPane.setLayout(borderLayout1); linePanel.setBackground(Color.yellow); instructionLabel.setFont(new java.awt.Font("SansSerif", 1, 12)); instructionLabel.setForeground(Color.blue); instructionLabel.setHorizontalAlignment(SwingConstants.CENTER); instructionLabel.setHorizontalTextPosition(SwingConstants.CENTER); instructionLabel. setText("Click and drag the mouse to draw a line using Bresenham\'s algorithm"); instructionLabel.setVerticalAlignment(SwingConstants.TOP); contentPane.setBackground(Color.yellow); // The LineCanvas class handles the drawing of the line linePanel.add(lineCanvas); contentPane.add(instructionLabel, BorderLayout.SOUTH); lineCanvas.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(MouseEvent e) { lineCanvas_mouseReleased(e); } public void mousePressed(MouseEvent e) { lineCanvas_mousePressed(e); } }); lineCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { lineCanvas_mouseDragged(e); } }); contentPane.add(linePanel, BorderLayout.NORTH); } /** * Overridden so we can exit when window is closed */ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } /** * Resizes the panel and lineCanvas to fit a resized frame */ protected void processComponentEvent(ComponentEvent e) { super.processComponentEvent(e); if (e.getID() == ComponentEvent.COMPONENT_RESIZED || e.getID() == ComponentEvent.COMPONENT_SHOWN) { linePanel.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight() - instructionLabel.getHeight()); lineCanvas.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight() - instructionLabel.getHeight()); } } /** * The mouse event handlers call LineCanvas methods to * draw the line using Bresenham's algorithm */ void lineCanvas_mouseReleased(MouseEvent event) { lineCanvas.setPoint(LineCanvas.END_POINT, event.getPoint()); lineCanvas.repaint(); } void lineCanvas_mousePressed(MouseEvent event) { lineCanvas.setPoint(LineCanvas.START_POINT, event.getPoint()); } void lineCanvas_mouseDragged(MouseEvent event) { lineCanvas.rubberBand(event.getPoint()); } }