Hm I have jdk 1.7.0_07
so What do you suggest?
Printable View
Hm I have jdk 1.7.0_07
so What do you suggest?
Calling super.paintComponent().Quote:
What do you suggest?
I tried and you keep saying to use an arraylist.
I dont know how, as i've said before. I know the calls and such, but I don't know where I'd put any of it.
Because the applet would need to redraw every point in the array like, everytime the mouse moved?
Time for some design work and programming:
Create the arraylist at the class level
Add points to it where you currently save x,y for the mouse's new location.
Get the points out of the list and draw lines between them
by at class level you mean, do something like:
No that is defining a class.
This statement in your program is at the class level:
BlankArea blankArea;
Look at the definition of scope.
OH ok , I already have an arraylist defined up there, i've kept it there incase.
is something like this correct?
I made it that size because I didnt know if its supposed to save every point, or just the most recent 4
Giving it a size is not too important, it will expand itself if needed.
You want to save every point that is used to draw segments of the lines you want drawn.
An advantage of having the points in a arraylist is that if you draw a number of shapes, the points for each shape could be saved in its own arraylist so that shapes could be manipulated individually.
They could be different colors, moved, deleted, drawn with wider lines, ...
Ok now my issue comes in, where exactly am I adding points to the arraylist AND how exactly do i make it so it draws everypoint?
I guess my question isnt where, its pretty much how.
I know the calls, Ijust don't know where to implement them
That was discussed in post#55Quote:
where to implement them
I guess what I really meant was, do I use al.add(x) and then al.add(y)
or should I give them specific locations in the array like al.add(1, x) ?
Look at using the Point class.
Now I'm just more confused.
I can't get a feel for it by looking at all the docs and functions of the class, I need to be able to find semi-relevant examples of the code, and I can't find that for Point class. I've been looking for a while as I came across that idea earlier today
I have
Code java:public class point{ protected int x, y; }
and that's as far as I got
Am i creating seperate instances of the class for every new xy coordinate?
EDIT: this is what I have so far, I'm confused now as to how to get the x,y from each point instance when calling drawLine
code:
Code java:package events; /* * MouseMotionEventDemo.java * */ import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.*; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import java.util.*; public class MouseMotionEventDemo extends JPanel implements MouseMotionListener { BlankArea blankArea; JTextArea textArea; point Point; int lastX=0, lastY=0, currX=0, currY=0; ArrayList al = new ArrayList(); Object ia[] = al.toArray(); static final String NEWLINE = System.getProperty("line.separator"); public static void main(String[] args) { /* Use an appropriate Look and Feel */ try { //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } /* Turn off metal's use of bold fonts */ UIManager.put("swing.boldMetal", Boolean.FALSE); //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ public class BlankArea extends JLabel { Dimension minSize = new Dimension(100, 50); public BlankArea(Color color) { setBackground(color); setOpaque(false); setBorder(BorderFactory.createLineBorder(Color.black)); } } public class point{ protected int x, y; public point() { setPoint(0,0); } public void setPoint(int coordx, int coordy){ x = coordx; y = coordy; } public Integer getX(){ return x; } public Integer getY(){ return y; } } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("MouseMotionEventDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new MouseMotionEventDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public MouseMotionEventDemo() { super(new GridLayout(0,1)); blankArea = new BlankArea(Color.BLUE); add(blankArea); textArea = new JTextArea(); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane.setPreferredSize(new Dimension(200, 75)); add(scrollPane); //Register for mouse events on blankArea and panel. blankArea.addMouseMotionListener(this); addMouseMotionListener(this); setPreferredSize(new Dimension(450, 450)); /*setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); */ } void eventOutput(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " (" + e.getX() + "," + e.getY() + ")" + " detected on " + e.getComponent().getClass().getName() + NEWLINE); textArea.setCaretPosition(textArea.getDocument().getLength()); } public void mouseMoved(MouseEvent e) { eventOutput("Mouse moved", e); record(e.getX(),e.getY()); } @Override public boolean mouseDown(Event e, int x, int y){ return(record(x,y)); } @Override public boolean mouseEnter(Event e, int x, int y) { return(record(x, y)); } public void mouseDragged(MouseEvent e) { eventOutput("Mouse dragged", e); currX = e.getX(); currY = e.getY(); Point = new point(); Point.setPoint(currX, currY); al.add(Point); repaint(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); for(int i = 0; i < (al.size()/2); i = i+2){ g.drawLine( ia[1],ia[1] ,ia[2] ,ia[2] ); } record(currX,currY); } protected boolean record(int x, int y){ lastX = x; lastY = y; Point.setPoint(lastX, lastY); al.add(Point); return(true); } }
You could use the Java SE Point class instead of making your own.
To get some experience using the classes, make a test program that creates an array list, adds several Point objects to it and then in a loop goes through the list of points and simulates drawing connected lines by printing out a line that says: drawing a line from x1,y1 to x2,y2
where x1,y1 etc are the points from the array list.
Edited post 67 to include my progress
as in post 67, just not sure how to call getX and getY from the point class while inside the drawline function
Did you write the simple program to learn how to use the ArrayList and Point classes?
When you get that to work you will know how to use the classes.
I don't see how that would really help me, as I don't know how to GET the x and y from the instances of the class within the array, so i can't write that simple program?
As each element in the array is an instance of Point, that means each element has an x and a y in that container; i just don't know how to access them
because ia[1].getX() doesn't work lol
You need to learn how to use the classes. Mixing a lot of new code into a larger program makes it very hard to work on fixing the problems. The smaller program will use some of the class's methods in a simpler environment.Quote:
how that would really help me,
Get rid of the array. The data is in the ArrayList. Writing the test program will show you most of what you need to know.
For me it won't help because I don't know the syntax?
all I need to figure out is how to get the x and y from the Point instance in the arraylist.
No matter how simple the program is, if I don't know the syntax it's not gonna matter.