View Single Post
  #2 (permalink)  
Old 08-06-2007, 09:40 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class L6D{ public static void main(String[] args) throws Exception{ Grid frame = new Grid(); frame.setTitle("Lab6 - Application #1"); frame.setSize(400,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); } } class Grid extends JFrame{ public Grid(){ Color[] bgColors = { new Color(50,100,75), Color.red, Color.green.darker(), Color.blue }; Color[] fgColors = { Color.orange, Color.blue, Color.yellow, Color.red }; JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2,2,1,1)); for(int j = 0; j < bgColors.length; j++) { panel.add(new NewPanel(bgColors[j], fgColors[j])); } this.add(panel); } } class NewPanel extends JPanel implements MouseListener{ Color color; public int x1= 0; public int y1= 0; public int x2= -1; public int y2= -1; public NewPanel(Color bg, Color fg){ setBackground(bg); color = fg; // Register listener for the mouse event addMouseListener(this); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) { if (x1 != x2 || y1 != y2){ x1 = e.getX(); y1 = e.getY(); x2 = x1; y2 = y1; } else { x2 = e.getX(); y2 = e.getY(); } repaint(); } protected void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(color); g.drawLine(x1,y1,x2,y2); } }
Reply With Quote