Results 1 to 12 of 12
- 11-23-2010, 02:15 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
How to display the black Polygon that I draw on the panel?
Hi guys
I have 2 problems here:
1. Below code does not show a black Polygon that I draw on the panel, only shows an empty Frame named "Poppy"
2. Once the frame shows, I can't close it by clicking "x" at the right top corner.
Does anyone know why?
Thanks heaps
Java Code:package brainydraw; import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; import javax.swing.JFrame; public class OctagonDrag extends JFrame implements MouseMotionListener { private int[] x = {150, 175, 175, 150, 100, 75, 75, 100}; private int[] y = {150, 175, 175, 150, 100, 75, 75, 100}; private Polygon p = new Polygon(x, y, 8); private int oldx; //Saves previous Polygon position private int oldy; private JFrame f = new JFrame(); public void init() { setTitle("Poppy"); setSize(300, 300); setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addMouseListener(new MousePressListener()); addMouseMotionListener(this); MyPanel panel = new MyPanel(); f.getContentPane().add(panel); panel.repaint(); } class MyPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.black);g.fillPolygon(p); } } public void mouseMoved(MouseEvent event) { } public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); if (p.contains(x, y)) { p.translate(x - oldx, y - oldy); oldx = x; oldy = y; repaint(); } } class MousePressListener extends MouseAdapter { public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); if (p.contains(x, y)) { oldx = x; oldy = y; } } } class CloseWindow implements WindowListener { public void windowClosing(WindowEvent event) { System.exit(0); } public void windowOpened(WindowEvent event) { } public void windowClosed(WindowEvent event) { } public void windowIconified(WindowEvent event) { } public void windowActivated(WindowEvent event) { } public void windowDeactivated(WindowEvent event) { } public void windowDeiconified(WindowEvent event) { } } public static void main(String[] args) { OctagonDrag test = new OctagonDrag(); test.init(); } }Last edited by BeijingDuck; 11-23-2010 at 02:39 AM.
-
You've got too many JFrames in your program, the first one being the program itself, or this, which extends JFrame and which is displayed, and the second being the variable f, which is null and is throwing a NPE when you call methods on it.
Your next problem is that your Polygon's x and y points are all the same, and this will create a straight line, not a polygon.
- 11-23-2010, 02:50 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
Hi Fubarable
Thanks for your advice, I have just initialised "f", now the frame can be closed.(second problem is soloved)
As for my first problem, how did I make "Polygon's x and y points are all the same", at moment I can't even display that straight line, I'd be happier at least if I can see something the panel.
Any more solutions?
-
If you are going to be using f as your JFrame, then your class itself should not extend JFrame and in fact should probably not extend anything. I see you've modified your code above, and I suggest you don't do that. Leave it as is and post your new code in a new post so folks later can see what changes have been made. I also see that you're adding a MouseListener to the main class, not f, and also setting the size of the main class, not f, which is confusing to me and the JVM. Again, the main class should not be a JFrame, and you shouldn't be doing these things to it.
For each pair of x's and y's, x equals y, which is the equation of a straight line. Sure it's a polygon, but it has no area and so can't be displayed. I suggest you vary your x's and your y's so they are not the same.As for my first problem, how did I make "Polygon's x and y points are all the same", at moment I can't even display that straight line, I'd be happier at least if I can see something the panel.
Any more solutions?
- 11-25-2010, 04:13 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
Hi Fubarable
Thanks for your advice. Now I got more to ask.
You mean the header "public class OctagonDrag extends JFrame implements MouseMotionListener" should not extend anything - should be like "public class OctagonDrag"?
You said I should add a MouseListener to f instead of the main class, how to achieve that? change "addMouseListener(new MousePressListener());" to "f.addMouseListener(new MousePressListener());"
You said the main class should not be a JFrame, but the main class has been OctagonDrag, has it?
So I need to vary each pair of x's and y's, if I have this :
, have I varied them?private int[] x = {150, 175, 175, 150, 100, 75, 75, 100};and private int[] y = {100, 140, 130, 130, 110, 100, 30, 160};
Thanks heaps
-
What happens when you try these changes?
- 11-25-2010, 05:00 AM #7
Cross posted!
How to fix this code? - Java Programming Forums
db
-
- 11-25-2010, 07:14 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
Hi Fubarable
After these changes, it has two errors on line 25 f.addMouseMotionListener(this);and line 54 repaint();
error on line 25 says:"method addMouseMotionListener in class java.awt.Component cannot be applied to given types required: java.awt.event.MouseMotionListener"
line 54 says cannot find symbol method repaint()
Any more thought on how to fix it?
Java Code:package brainydraw; import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; import javax.swing.JFrame; public class OctagonDrag //extends JFrame implements MouseMotionListener { private int[] x = {150, 175, 175, 150, 100, 75, 75, 100}; private int[] y = {100, 140, 130, 130, 110, 100, 30, 160}; private Polygon p = new Polygon(x, y, 8); private int oldx; //Saves previous Polygon position private int oldy; private JFrame f = new JFrame(); // you need to initialize it before you can call methods on it, eg f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); that was why the frame couldn't be closed. public void init() { f.setTitle("Poppy"); f.setSize(300, 300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.addMouseListener(new MousePressListener()); f.addMouseMotionListener(this); MyPanel panel = new MyPanel(); f.getContentPane().add(panel); //panel.repaint(); f.setVisible(true); } class MyPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.black);g.fillPolygon(p); } } public void mouseMoved(MouseEvent event) { } public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); if (p.contains(x, y)) { p.translate(x - oldx, y - oldy); oldx = x; oldy = y; repaint(); } } class MousePressListener extends MouseAdapter { public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); if (p.contains(x, y)) { oldx = x; oldy = y; } } } class CloseWindow implements WindowListener { public void windowClosing(WindowEvent event) { System.exit(0); } public void windowOpened(WindowEvent event) { } public void windowClosed(WindowEvent event) { } public void windowIconified(WindowEvent event) { } public void windowActivated(WindowEvent event) { } public void windowDeactivated(WindowEvent event) { } public void windowDeiconified(WindowEvent event) { } } public static void main(String[] args) { OctagonDrag test = new OctagonDrag(); test.init(); } }
- 11-29-2010, 02:46 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
Hi Guys
The problem is still not solved, anyone got more ideas?
-
Please see Darryl's post above. Are there any other places that you've cross-posted this question? Most of us avoid answering cross-posted questions for fear of performing effort (volunteer effort mind you) for no benefit, as the post has been answered or well discussed in cross-posts. If you do have to cross-post, you should notify all threads involved about all cross-posts.
Last edited by Fubarable; 11-29-2010 at 04:05 AM.
- 11-29-2010, 09:40 PM #12
Member
- Join Date
- Nov 2010
- Posts
- 23
- Rep Power
- 0
Hi Fubarable, glad you have said something. I am courious how does this thing operates.
I thought the forum websites were non-profits and volunteers, all helpers here were volunteers, it's their kindness or hobby to heplp others here, they certainly won't have benefits from answering others'questions. So what benefits are you talkiing about here? - (If you involve in cross posts, you will not have benefits?)
I sometimes post my questions in different websites is because I want to get more information or ideas from different thinkers or the question got stucked in one site.
Thanks
Similar Threads
-
red black tree
By ahmakki in forum New To JavaReplies: 1Last Post: 03-21-2010, 06:49 AM -
How to display image in a panel with JFileChooser
By srisar in forum AWT / SwingReplies: 14Last Post: 10-11-2009, 06:06 PM -
How do i display foreign languages in the IDE console panel ?
By BobZ_Annapolis in forum EclipseReplies: 1Last Post: 02-20-2009, 04:16 PM -
Display graph in tabbed panel
By Laura Warren in forum New To JavaReplies: 3Last Post: 01-12-2009, 11:34 PM -
How to Draw a Polygon in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks