Question: I'm currently working on Java: How to Program 10th edition, Exercise 13.31. The Java 2D drawing application assignment. This assignment includes filled, dashed and gradient
I'm currently working on "Java: How to Program" 10th edition, Exercise 13.31. The Java 2D drawing application assignment. This assignment includes filled, dashed and gradient shapes and lines. Below is the code that I have so far and the actual frame looks the way it should but I am having trouble figuring out how to actually draw lines, ovals, and rectangles on the panel using the mouse. I believe it has something to do with updating "State" when the user inputs information. But drawing on the main myPanel is really my main issue. Any help will be greatly appreciated!!
import java.awt.Color; //import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.Point; //import java.awt.BorderLayout; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JCheckBox; import javax.swing.JColorChooser; //import java.awt.FlowLayout; import javax.swing.JComboBox; import java.awt.BorderLayout; import java.awt.GridLayout; //import java.util.*; import java.util.ArrayList; import java.awt.*; //import javax.swing.*;
/** * * @author jackcash */ public class Drawingap extends JPanel implements MouseListener, MouseMotionListener { //private final JPanel colorJPanel; static ArrayList itemsDrawn; static JCheckBox filled; private JLabel mousePos; //Set all the defaults for the currentg State private static Color colorf = (Color.BLUE); private static Color colorb = (Color.BLUE); private static boolean boolGradient = false; private static boolean boolFilled = false; private static boolean boolDashed = false; private static int intStrokewidth = 5; // adjust this so it looks good private static int intDashlength = 5; private static int intShapeSelected = 0;
static ArrayList shapes; //this was interesting...lets talk private static int x1; private static int x2; private static int y1; private static int y2; private static State stateCurrent; public final JComboBox shapeChooser; // public DrawingAp.DrawPanel drawingPanel = new DrawPanel(); // public DrawingAp.ControlPanel controlPanel = new ControlPanel(drawingPanel); //private Color color = Color.WHITE; public static void main(String[] args) { new Drawingap(); } // end method main public Drawingap() { JFrame frame = new JFrame("Java 2D Drawings"); frame.setSize(700, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton undo = new JButton("Undo"); JButton clear = new JButton("Clear"); JLabel shapelabel = new JLabel("Shape:"); //listShapes = //JComboBox shapeChooser = new JComboBox<>(listShapes); shapeChooser = new JComboBox<>(new String[] {"Line", "Oval", "Rectangle"}); filled = new JCheckBox("Filled"); //filled.setEnabled(boolFilled); JCheckBox gradient = new JCheckBox("Use Gradient"); //gradient.setEnabled(boolGradient); JButton color1 = new JButton("1st Color..."); color1.setBackground(colorf); //color1.setForeground(Color.WHITE); JButton color2 = new JButton("2nd Color..."); color2.setBackground(colorb); //color2.setForeground(Color.WHITE); JLabel lineLabel = new JLabel("Line Width:"); JTextField strokewidth = new JTextField("2"); strokewidth.setText(Integer.toString(intStrokewidth)); JLabel dashLabel = new JLabel("Dash Length:"); JTextField dashlength = new JTextField("10"); dashlength.setText(Integer.toString(intDashlength)); JCheckBox dashed = new JCheckBox("Dashed"); //dashed.setEnabled(boolDashed); JPanel panel2 = new JPanel(); panel2.add(undo); panel2.add(clear); panel2.add(shapelabel); panel2.add(shapeChooser); panel2.add(filled); JPanel panel3 = new JPanel(); panel3.add(gradient); panel3.add(color1); panel3.add(color2); panel3.add(lineLabel); panel3.add(strokewidth); panel3.add(dashLabel); panel3.add(dashlength); panel3.add(dashed); final JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2,0,0,1)); panel.add(panel2); panel.add(panel3); frame.add(panel, BorderLayout.PAGE_START); final JPanel myPanel = new JPanel(); myPanel.setBackground(Color.WHITE); frame.add(myPanel, BorderLayout.CENTER); JPanel bottom = new JPanel(); bottom.setLayout(new BorderLayout()); mousePos = new JLabel("Mouse outside JPanel"); bottom.add(mousePos, BorderLayout.WEST); bottom.setVisible(true); frame.add(bottom, BorderLayout.SOUTH); MouseHandler handler = new MouseHandler(); myPanel.addMouseListener(handler); myPanel.addMouseMotionListener(handler); stateCurrent = new State(colorf,colorb,boolGradient,boolFilled,boolDashed,intStrokewidth,intDashlength,intShapeSelected); itemsDrawn = new ArrayList<>();
color1.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { colorf = JColorChooser.showDialog( Drawingap.this,"Pick a color",Color.BLACK); System.out.print("foreground color = " + colorf.toString()); stateCurrent.colorf = colorf; color1.setBackground(stateCurrent.colorf); } }); color2.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { colorb = JColorChooser.showDialog( null,"Pick a color",colorb); System.out.print("back ground color = " + colorb.toString()); stateCurrent.colorb = colorb; color2.setBackground(colorb); if(colorb == null) colorb = Color.BLACK; } }); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { itemsDrawn = new ArrayList<>(); myPanel.repaint(); }}); undo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (itemsDrawn.size() != 0){ itemsDrawn.remove(itemsDrawn.size() - 1); myPanel.repaint(); } } } ); shapeChooser.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stud JComboBox cb = (JComboBox) e.getSource(); stateCurrent.index = cb.getSelectedIndex(); }}); frame.setVisible(true); } // end class DrawingApp()
public class State { Color colorf; Color colorb; boolean gradient; boolean filled; boolean dashed; int strokewidth; int dashlength; int index; public State(Color _colorf, Color _colorb, boolean _gradient, boolean _filled, boolean _dashed, int _strokewidth, int _dashlength, int _index) { this.colorf = _colorf; this.colorb = _colorb; this.gradient = _gradient; this.filled = _filled; this.dashed = _dashed; this.strokewidth = _strokewidth; this.dashlength = _dashlength; this.index = _index; } public Color getForeground() { return colorf; } public Color getBackground() {return colorb;} public boolean isGradient() {return gradient; } public boolean isFilled() {return filled;} public boolean isDashed() {return dashed; } public int getLineWidth() {return strokewidth; } public int getDashLength() {return dashlength;} public int getIndex() {return index;} }// end class State public class Shape { private final int x1; private final int y1; private final int x2; private final int y2; private final State state; public Shape(int x1,int y1, int x2, int y2, State state ) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.state = state; // System.out.println("Created shape " + shapes[state.index]); } } private class MouseHandler implements MouseMotionListener, MouseListener { @Override public void mouseMoved(MouseEvent event) { mousePos.setText(String.format("(%d,%d)", event.getX(), event.getY())); } public void mousePressed(MouseEvent arg0) { x1 = arg0.getX(); y1 = arg0.getY(); System.out.println("Get the first point"+ Integer.toString(x1) + " y = "+ Integer.toString(y1)); } @Override public void mouseDragged(MouseEvent event) { } public void mouseExited(MouseEvent arg0) {
} public void mouseReleased(MouseEvent arg0) { Shape shape = null; x2 = arg0.getX(); y2 = arg0.getY(); System.out.println("2nd time the first point"+ Integer.toString(x1) + " y = "+ Integer.toString(y1)); System.out.println("Get the last point"+ Integer.toString(x2) + " y = "+ Integer.toString(y2));
shape = new Shape(x1,y1,x2,y2,stateCurrent); itemsDrawn.add(shape); Graphics g ; g = null; System.out.println("Insdie the lower overrides!!!"); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; g2D.drawLine(x1, y1, x2, y2); } public void mouseEntered(MouseEvent arg0) { } public void mouseClicked(MouseEvent arg0) { }
} public class PaintPanel extends JPanel { private final ArrayList points = new ArrayList<>(); public PaintPanel() { addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { points.add(e.getPoint()); repaint(); } } ); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // for (Point point : points) { // g.drawLine(point.x,point.y,4,4); // } } } // end class PaintPanel @Override public void mouseClicked(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mousePressed(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mouseReleased(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mouseEntered(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mouseExited(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mouseDragged(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
@Override public void mouseMoved(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }
} // end class DrawingApp() extends JPanel
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
