Trying to create a simple paint program. I'm trying to make objects from other classes and incorporate them to my method class. So far the program runs on the actions performed by the mouse. It will "draw" whatever the user wants when the mouse is clicked(2nd class). I'm trying to get the 3rd class to be come part of my main method. I was recommended to create different objects, for example; one class for shapes, other for colors. If you run the main method + 2nd class. You should be able to see a menubar with an exit "option", but I want to be able to create a class or just make all the shapes and colors within my mainmethod.
Which is a better choice? I think different classes will be a more "organized" approach, but I don't know how I can add it to my mainmethod class. Can you show a simple example of how to add a class [ which has the option to create a circle from the menubar]. Or, just make 3rd class work on my main method.
Hope this helps understand what I'm trying to do. I'd really appreciate your help.
PHP Code:
// main method class
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.*;
public class Painter{
public static void main (String[] args)
{
JFrame application = new JFrame ("Paint Program");
PaintPanel paintPanel = new PaintPanel ();
application.add (paintPanel, BorderLayout.CENTER);
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
application.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction() );
application.setSize(800,400);
application.setVisible(true);
}
}
// 2nd class
import java.awt.Point;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;
public class PaintPanel extends JPanel {
private int pointCount = 0;
private Point[] points = new Point[10000];
public PaintPanel()
{
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged (MouseEvent event)
{
if (pointCount <points.length)
{
points [ pointCount ] = event.getPoint();
pointCount++;
repaint();
}
}
}
);
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
for (int i=0; i < pointCount; i++)
g.fillOval (points [i].x, points[i].y, 4, 4);
}
}
// [COLOR="yellow"]3rd class[/COLOR]
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
public class ListFrame extends JFrame {
private JList colorJList;
private static final String[] colorNames = {"Black", "Blue", "Cyan",
"Dark Gray", "Gray", "Green", "Light Gray", "Magenta",
"Orange", "Pink", "Red", "White", "Yellow"};
private static final Color[] colors = {Color.BLACK, Color.BLUE,
Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN,
Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK,
Color.RED, Color.WHITE, Color.YELLOW };
public ListFrame(){
super("List Test");
setLayout(new FlowLayout() );
colorJList = new JList(colorNames);
colorJList.setVisibleRowCount(5);
colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(colorJList));
colorJList.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged( ListSelectionEvent event ){
getContentPane().setBackground(
colors[colorJList.getSelectedIndex() ]);
}
});
}
}