Results 1 to 20 of 53
Thread: Paint and actionlistener!!!!
- 01-22-2012, 09:07 AM #1
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Paint and actionlistener!!!!
hi everyone, sorry for the exclamation point
i've been just getting very frustrated with this. so essentially, i've been trying to make a program that allows you to input coordinates and a matrices via a gui, and then those integers multiplies and the results are displayed on a separate popup window (the coordinates are then transformed into a three-sided polygon).
here is the code, and please reply ASAP!:
(Separate pop-up triangle showing class)
Java Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import javax.swing.JFrame; public class JTrianglePanel extends Cool{ /** * */ private static final long serialVersionUID = 1L; public JTrianglePanel(){ JFrame f = new JFrame(); f.setSize(380, 360); f.setVisible(true); paint(g); } public void paint(Graphics g){ super.paint(g); int [] xcoordinates = { matrix1, matrix2, matrix3}; int [] ycoordinates = { matrix4, matrix5, matrix6}; Polygon pol = new Polygon(xcoordinates, ycoordinates, 3); g.drawPolygon(pol); } } }
(ActionListener class, with everything else)
Java Code:JButton btnOk = new JButton("OK"); btnOk.setBackground(Color.BLACK); btnOk.setForeground(Color.GRAY); btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11)); btnOk.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ JTrianglePanel p = new JTrianglePanel(); p.show(); } });
please note that i've tried invoking "paint(g)" and other graphics methods exclusively in actionlistener, but it asks to create a local variable nearly all the time, and when it doesnt it doesnt work!!
KHELP!!
- 01-22-2012, 09:27 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,395
- Blog Entries
- 7
- Rep Power
- 17
Re: Paint and actionlistener!!!!
Oh dear, your program must be able to do everything; must it be able to fry eggs too? First try to decompose your problem a bit, e.g.
1) read coordinates;
2) read matrices;
3) do dirty deeds with the coordinates and matrices;
4) find the polygon given the previous steps;
5) display the entire hoopla.
As it is now, it is just a mess of (unrelated?) problems.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2012, 09:31 AM #3
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
no it isnt! its organized. im just trying to invoke the paint method into actionlistener. whenever i do, it just displays a blank popup window. this is what im trying to do:
1) read coordinates;
2) read matrices;
3) do dirty deeds with the coordinates and matrices;
4)draw result of dirty deed on new JFrame window that pops up when i press OK on the gui i made.
its THAT simple. please HELP me....HELP.
please help!Last edited by rowrowrowyourboat; 01-22-2012 at 09:33 AM.
- 01-22-2012, 09:44 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,395
- Blog Entries
- 7
- Rep Power
- 17
Re: Paint and actionlistener!!!!
You don't call the paint( ... ) method; a separate Thread (the EDT == Event Dispatch Thread) calls it when certain components need to be repainted. Read the Swing tutorial for that. You can call the repaint() method on a component so that the EDT will call the paint( ... ) method on it in the near future. Read the API documentation for details.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2012, 09:47 AM #5
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
thanks, so i have to create a thread? these are the errors im receiving (if it will make any difference):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JTrianglePanel.paint(JTrianglePanel.java:50)
at JTrianglePanel.<init>(JTrianglePanel.java:28)
at Cool$2.actionPerformed(Cool.java:191)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 01-22-2012, 09:52 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,395
- Blog Entries
- 7
- Rep Power
- 17
- 01-22-2012, 10:02 AM #7
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
this is what i already had(i read the api):
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cool frame = new Cool();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
but i have to make this work:
JButton btnOk = new JButton("OK");
btnOk.setBackground(Color.BLACK);
btnOk.setForeground(Color.GRAY);
btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11));
btnOk.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
JTrianglePanel p = new JTrianglePanel();
p.repaint(); //IT STILL DOES NOT WORK//
}
});
AND MORE OF THE SAME ERRORS I RECEIVED PREVIOUSLY
im completely oblivious as to what i have to do to make this function operate...
Last edited by rowrowrowyourboat; 01-22-2012 at 10:06 AM. Reason: edit
- 01-22-2012, 10:22 AM #8
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
please anyone.
- 01-22-2012, 12:01 PM #9
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
ANYONE!? please
- 01-22-2012, 12:34 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,395
- Blog Entries
- 7
- Rep Power
- 17
Re: Paint and actionlistener!!!!
As far as I can see from your code you're not adding that JButton to anything so it won't be visible. Please read a Swing tutorial first before you attempt to code something because your code, as it is, doesn't make much sense.
kind regards,
Jos
ps. don't PM me for this and be patient a bit; it is Sunday overhere and in large parts of the world.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2012, 01:10 PM #11
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
no, the jbutton is visible, but the display is just a blank frame. i need it to display a triangle based on the coordinates i provided.
- 01-22-2012, 01:24 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,395
- Blog Entries
- 7
- Rep Power
- 17
Re: Paint and actionlistener!!!!
The same reasoning applies to your JTrianglePanel p; you're not adding it to anything (in your ActionListener) so it won't be visible; adding components to other components that are already visible takes a bit more code: read a Swing tutorial please.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-22-2012, 05:09 PM #13
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
i read everything but nothing constructive! i know this sounds "i dont know the word for it" but please just tell me what to do!
-
Re: Paint and actionlistener!!!!
Please clarify your problem if possible:
- Where specifically are you stuck now?
- Can you post your latest code. When doing so, please take care that it is formatted correctly and with enough but not too much white space. The easier your code is to read, the easier it is for us to understand and the easier it is for us to help you.
- You've got other classes that you haven't shown us it seems such as the Cool class. Please show all pertinent code if it's not too large.
- When posting code, please use [code] [/code] tags so that your code retains its formatting and is readable. Again, if it's easy to read, it's easier to understand.
- Show any and all error messages and/or exceptions. Please indicate with comments in your code the line(s) that are causing the error messages or exceptions.
- The more specific your question, usually the more helpful our answers will be. The more general the question, usually the more frequently we refer you to the tutorials.
Luck!
- 01-22-2012, 05:43 PM #15
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
Please.
this is supposed to be the class that draws the triangle to the another window that is commenced as a result of clicking OK.
in the paint method are the x and y coordinates used to display the triangle. i call it in the method, but nothing. i tried glass pane aswell.
in the paint method are the x and y coordinates used to display the triangle. i call it in the method, but nothing. i tried glass pane aswell.Java Code:import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.*; public class JTrianglePanel extends Cool{ /** * */ private static final long serialVersionUID = 1L; public JTrianglePanel(){ JPanel f = new JPanel(); Container contentPane = f.getRootPane(); TriangleDraw d = new TriangleDraw(); contentPane.add(d); f.setSize(380, 360); f.setVisible(true); paint(g); } public void paint(Graphics g){ super.paint(g); int [] xcoordinates = { matrix1, matrix2, matrix3}; int [] ycoordinates = { matrix4, matrix5, matrix6}; Polygon pol = new Polygon(xcoordinates, ycoordinates, 3); g.drawPolygon(pol); } }
here is assigning it to the actionlistener of the button (the real challenge). the frame shows up but its blank. not a triangle in sight.
Java Code:JButton btnOk = new JButton("OK"); btnOk.setBackground(Color.BLACK); btnOk.setForeground(Color.GRAY); btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11)); btnOk.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ JTrianglePanel p = new JTrianglePanel(); p.repaint(g); } });
the plan of the program was listed before.
enter x coordinates,
enter y coordinates,
enter matrices coordinates
multiply the elements to recieve 6 more integers which translate to a triangle projected in a new window.
(both of them are in seperate classes, this project is comprised in two [ Cool : main, and JTrianglePanel which is the one the is supposed to display)Last edited by rowrowrowyourboat; 01-22-2012 at 05:44 PM. Reason: asdf
-
Re: Paint and actionlistener!!!!
Again, you've got missing code that limits our understanding of the problem (a major issue), again, you've got too much white space (a minor quibble, but since you're asking us to put in unpaid effort to help you, it's not really asking you too much to not make it difficult for us to do, right?).
- 01-22-2012, 05:52 PM #17
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
you're right, im sorry. here is my entire project:
this is Cool (The main class)
Java Code:import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics2D; import java.awt.Polygon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.Toolkit; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.awt.Graphics; import javax.swing.JLabel; import javax.swing.SwingConstants; import javax.swing.JTextField; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import com.jgoodies.forms.factories.DefaultComponentFactory; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JButton; import java.awt.Font; import java.awt.Color; import javax.swing.border.SoftBevelBorder; import javax.swing.border.BevelBorder; public class Cool extends JFrame { private JPanel contentPane; private JTextField x1; private JTextField x2; private JTextField x3; private JTextField y1; private JTextField y2; private JTextField y3; /** * Launch the application. */ public int num1,num2,num3,num4,num5,num6; public int m1,m2,m3,m4; private JTextField mat1; private JTextField mat3; private JTextField mat2; private JTextField mat4; int matrix1 = num1 * m1 + num2 * m3; int matrix2 = num1 * m2 + num2 * m4; int matrix3 = num3 * m1 + num4 * m3; int matrix4 = num3 * m2 + num4 * m4; int matrix5 = num5 * m1 + num6 * m3; int matrix6 = num5 * m2 + num6 * m4; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Cool frame = new Cool(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } Graphics g; /** * Create the frame. */ public Cool() { setTitle("GraphingApp 1.0"); setIconImage(Toolkit.getDefaultToolkit().getImage(Cool.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif"))); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBackground(Color.WHITE); contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null)); setContentPane(contentPane); JLabel lblNewJgoodiesLabel = DefaultComponentFactory.getInstance().createLabel("Enter your X coordinates:"); lblNewJgoodiesLabel.setFont(new Font("Aharoni", Font.PLAIN, 11)); JLabel lblEnterYourY = DefaultComponentFactory.getInstance().createLabel("Enter your Y coordinates:"); lblEnterYourY.setFont(new Font("Aharoni", Font.PLAIN, 11)); x1 = new JTextField(); x1.setText("x1"); x1.setColumns(10); try{ num1 = Integer.parseInt(x1.getText()); }catch(Exception e){} x2 = new JTextField(); x2.setText("x2"); x2.setColumns(10); try{ num2 = Integer.parseInt(x2.getText()); }catch(Exception e){} x3 = new JTextField(); x3.setText("x3"); x3.setColumns(10); try{ num3 = Integer.parseInt(x3.getText()); }catch(Exception e){} y1 = new JTextField(); y1.setText("y1"); y1.setColumns(10); try{ num4 = Integer.parseInt(y1.getText()); }catch(Exception e){} y2 = new JTextField(); y2.setText("y2"); y2.setColumns(10); try{ num5 = Integer.parseInt(y2.getText()); }catch(Exception e){} y3 = new JTextField(); y3.setText("y3"); y3.setColumns(10); try{ num6 = Integer.parseInt(y3.getText()); }catch(Exception e){} JTrianglePanel p = new JTrianglePanel(); JButton btnOk = new JButton("OK"); btnOk.setBackground(Color.BLACK); btnOk.setForeground(Color.GRAY); btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11)); btnOk.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ JTrianglePanel p = new JTrianglePanel(); p.repaint(); } }); btnOk.add(p); mat1 = new JTextField(); mat1.setText("mat1"); mat1.setColumns(10); try{ m1 = Integer.parseInt(mat1.getText()); }catch(Exception e){} mat3 = new JTextField(); mat3.setText("mat3"); mat3.setColumns(10); try{ m3 = Integer.parseInt(mat3.getText()); }catch(Exception e){} mat2 = new JTextField(); mat2.setText("mat2"); mat2.setColumns(10); try{ m2 = Integer.parseInt(mat2.getText()); }catch(Exception e){} mat4 = new JTextField(); mat4.setText("mat4"); mat4.setColumns(10); try{ m4 = Integer.parseInt(mat4.getText()); }catch(Exception e){} GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(67) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(x3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createSequentialGroup() .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(lblNewJgoodiesLabel) .addComponent(x1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(73) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(y1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(y2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(y3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addGroup(gl_contentPane.createSequentialGroup() .addGap(50) .addComponent(lblEnterYourY)))) .addComponent(x2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addGroup(gl_contentPane.createSequentialGroup() .addGap(185) .addComponent(btnOk)) .addGroup(gl_contentPane.createSequentialGroup() .addGap(162) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(mat1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE) .addComponent(mat3, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addComponent(mat4, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE) .addComponent(mat2, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)))) .addContainerGap(69, Short.MAX_VALUE)) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addGap(29) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewJgoodiesLabel) .addComponent(lblEnterYourY)) .addGap(33) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(x1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(y1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(x2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(y2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(x3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(y3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(mat2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(mat1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(mat4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(mat3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addComponent(btnOk) .addContainerGap()) ); contentPane.setLayout(gl_contentPane); } }
(this is the JTriangle displaying class)
Java Code:import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.*; public class JTrianglePanel extends Cool{ /** * */ private static final long serialVersionUID = 1L; public JTrianglePanel(){ JPanel f = new JPanel(); Container contentPane = f.getRootPane(); TriangleDraw d = new TriangleDraw(); contentPane.add(d); f.setSize(380, 360); f.setVisible(true); paint(g); } public void paint(Graphics g){ super.paint(g); int [] xcoordinates = { matrix1, matrix2, matrix3}; int [] ycoordinates = { matrix4, matrix5, matrix6}; Polygon pol = new Polygon(xcoordinates, ycoordinates, 3); g.drawPolygon(pol); } }
-
Re: Paint and actionlistener!!!!
You're missing the TriangleDraw class, but my biggest question is why you have the JTrianglePanel extend the Cool class as this makes no sense and will only lead to errors. Don't do this but instead have JTrianglePanel extend a JPanel. If you need to call Cool methods, then pass an instance of Cool into the JTrianglePanel, perhaps via the latter's constructor.
- 01-22-2012, 06:03 PM #19
Member
- Join Date
- Jan 2012
- Location
- hungary
- Posts
- 27
- Rep Power
- 0
Re: Paint and actionlistener!!!!
ok, see, but when i do that, my x and y coordinate array's..their elements are displayed as errors (red squiggly line). am i supposed to make an entirely seperate class for drawing the triangle, why cant JTrianglePanel just do that. i called the method in it's constructor.
-
Re: Paint and actionlistener!!!!
You're trying to use inheritance to solve a non-inheritance type problem. Before you use inheritance, you have to ask yourself, is this new class a more specialized version of the parent class. For instance, is the JTrianglePanel a more specialized version of Cool? Answer, no, not at all. Cool is the main GUI and a JFrame, while JTrianglePanel is a JPanel and a component of Cool, so it does not satisfy the "is-a" relationship -- JTrianglePanel is not a Cool (while on the other hand a Dog class does satisfy the "is-a" condition with an Animal class).
So having JTrianglePanel extend Cool will give JTrianglePanel access to Cool's methods and variables, but they will not be the same methods and variables of the Cool JFrame that is displayed. Instead they will be methods and variables owned by the JTrianglePanel object alone, so using inheritance for this is bound to fail. Instead, give JTrianglePanel a Cool variable (a class field), and assign it to the current Cool object by giving JTrianglePanel's constructor a Cool parameter.
I have no idea what you're asking here. All I know is that you're code has a TriangleDraw variable, but there's no TriangleDraw class code....am i supposed to make an entirely seperate class for drawing the triangle, why cant JTrianglePanel just do that. i called the method in it's constructor.
Similar Threads
-
Paint
By ninjaturtlez in forum AWT / SwingReplies: 4Last Post: 12-17-2011, 04:15 AM -
Please help with actionlistener
By ADustedEwok in forum New To JavaReplies: 5Last Post: 12-08-2011, 10:04 PM -
ActionListener
By jaylimix in forum Java AppletsReplies: 5Last Post: 11-06-2011, 06:05 PM -
Paint????
By seanfmglobal in forum New To JavaReplies: 3Last Post: 02-15-2011, 08:00 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks