473,664 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NEED HELP FAST please

109 New Member
i have a menu bar, when i run the code the first time everything is fine. If i click on the screen i print a Circle or a Square, store the shape in an arraylist and repaint the frame. When the frame repaints the menuBar gets painted again under the orignal.
Any ideas?

Expand|Select|Wrap|Line Numbers
  1. public class X2Frame extends JFrame {
  2.    private JMenuBar menuBar = new JMenuBar();
  3.    private JMenu menuTool = new JMenu();
  4.    private JMenuItem menuToolSquare = new JMenuItem();
  5.    private JMenuItem menuToolCircle = new JMenuItem();
  6.    private BorderLayout borderLayout1 = new BorderLayout();
  7.    private CanvasPanel canvasPanel = new CanvasPanel();
  8.  
  9.    public X2Frame() {
  10.       try {
  11.          jbInit();
  12.       } catch (Exception e) {
  13.          e.printStackTrace();
  14.       }
  15.    }
  16.  
  17.    private void jbInit() throws Exception {
  18.       this.setJMenuBar( menuBar );
  19.       this.getContentPane().setLayout(borderLayout1);
  20.       this.setSize( new Dimension(400, 300) );
  21.       menuTool.setText("Tool");
  22.       menuTool.setMnemonic('T');
  23.       menuToolSquare.setText("Square");
  24.       menuToolSquare.setMnemonic('S');
  25.       menuToolSquare.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {
  26.                   menuToolSquare_ActionPerformed(e);
  27.                } } );
  28.       menuToolCircle.setText("Circle");
  29.       menuToolCircle.setMnemonic('C');
  30.       menuToolCircle.addActionListener(new ActionListener() {
  31.                public void actionPerformed(ActionEvent e) {
  32.                   menuToolCircle_actionPerformed(e);
  33.                }
  34.             });
  35.       menuTool.add( menuToolSquare );
  36.       menuTool.add(menuToolCircle);
  37.       menuBar.add(menuTool);
  38.       this.getContentPane().add(canvasPanel, BorderLayout.CENTER);
  39.    }
  40.  
Apr 18 '08 #1
21 1637
sukatoa
539 Contributor
Ahhh!!! so you are using JDeveloper ...am i right?
What jdk did you use?
it is recommended that you use jdk 5.0.... try it again, and update us....

sukatoa
Apr 18 '08 #2
SpecialKay
109 New Member
Whats a JDK, and how do i change it?
Apr 18 '08 #3
SpecialKay
109 New Member
Update to problem:
Not only is the menubar being repainted, but if i draw a square, then change tools and draw something else, the first square and the menubar gets doubled directly under the first. If i resize the window it goes back to normal. Paint again and it doubles again.
Apr 18 '08 #4
sukatoa
539 Contributor
Whats a JDK, and how do i change it?
You can download it at Sun Microsystems... .

jdk(Java development kit) 5.0.... i can't remember the procedure... that was last 3 month's ago when i tried to use it.... but seems, the code generator fail sometimes....
I prefer to use netbeans if making GUI in a hurry.....

So, i came back to JCreator Lite....


I like to test your code, can you post the whole code?

sukatoa...
Apr 18 '08 #5
SpecialKay
109 New Member
its split up in 6 classes,
not sure that posting it is the easiest way.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.Graphics2D;
  2. import java.awt.geom.Ellipse2D;
  3.  
  4. public class Circle extends Shape {
  5.  
  6.    public Circle(int x, int y) {
  7.       super.setX(x);
  8.       super.setY(y);
  9.    }
  10.    public void draw( Graphics2D g2 )
  11.    {
  12.       int CircleX = super.getX() - (super.SHAPE_WIDTH/2);
  13.       int CircleY = super.getY() - (super.SHAPE_WIDTH/2);
  14.       //Problem Area
  15.       Ellipse2D.Double circle = new Ellipse2D.Double(CircleX,CircleY,super.SHAPE_WIDTH,super.SHAPE_WIDTH);
  16.       g2.draw(circle);
  17.    }
  18. }
  19.  
  20. import java.awt.BorderLayout;
  21. import java.awt.Dimension;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24.  
  25. import javax.swing.JFrame;
  26. import javax.swing.JMenu;
  27. import javax.swing.JMenuBar;
  28. import javax.swing.JMenuItem;
  29.  
  30. public class X2Frame extends JFrame {
  31.    private JMenuBar menuBar = new JMenuBar();
  32.    private JMenu menuTool = new JMenu();
  33.    private JMenuItem menuToolSquare = new JMenuItem();
  34.    private JMenuItem menuToolCircle = new JMenuItem();
  35.    private BorderLayout borderLayout1 = new BorderLayout();
  36.    private CanvasPanel canvasPanel = new CanvasPanel();
  37.  
  38.    public X2Frame() {
  39.       try {
  40.          jbInit();
  41.       } catch (Exception e) {
  42.          e.printStackTrace();
  43.       }
  44.    }
  45.  
  46.    private void jbInit() throws Exception {
  47.       this.setJMenuBar( menuBar );
  48.       this.getContentPane().setLayout(borderLayout1);
  49.       this.setSize( new Dimension(400, 300) );
  50.       this.setTitle( "Kevin Cameron" );
  51.       menuTool.setText("Tool");
  52.       menuTool.setMnemonic('T');
  53.       menuToolSquare.setText("Square");
  54.       menuToolSquare.setMnemonic('S');
  55.       menuToolSquare.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) {
  56.                   menuToolSquare_ActionPerformed(e);
  57.                } } );
  58.       menuToolCircle.setText("Circle");
  59.       menuToolCircle.setMnemonic('C');
  60.       menuToolCircle.addActionListener(new ActionListener() {
  61.                public void actionPerformed(ActionEvent e) {
  62.                   menuToolCircle_actionPerformed(e);
  63.                }
  64.             });
  65.       menuTool.add( menuToolSquare );
  66.       menuTool.add(menuToolCircle);
  67.       menuBar.add(menuTool);
  68.       this.getContentPane().add(canvasPanel, BorderLayout.CENTER);
  69.    }
  70.  
  71.    private void menuToolSquare_ActionPerformed(ActionEvent e) {
  72.       canvasPanel.setCircle(false);
  73.    }
  74.  
  75.    private void menuToolCircle_actionPerformed(ActionEvent e) {
  76.       canvasPanel.setCircle(true);
  77.    }
  78. }
  79.  
  80.  
  81. import java.awt.Graphics2D;
  82.  
  83. public class Square extends Shape {
  84.  
  85.    public Square(int x, int y) {
  86.       super.setX(x);
  87.       super.setY(y);
  88.    }
  89.  
  90.    public void draw( Graphics2D g2 )
  91.    {
  92.          g2.drawRect(super.getX(), super.getY(), super.SHAPE_WIDTH, super.SHAPE_WIDTH);
  93.    }
  94. }
  95.  
  96.  
Apr 18 '08 #6
SpecialKay
109 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.awt.Graphics2D;
  2.  
  3. public abstract class Shape {
  4.    static int SHAPE_WIDTH = 20;
  5.    int x;
  6.    int y;
  7.  
  8.    abstract public void draw( Graphics2D g2 );
  9.  
  10.    public Shape() {
  11.    }
  12.  
  13.    public void setX(int x) { this.x = x; }
  14.    public int getX() { return x; }
  15.    public void setY(int y) { this.y = y; }
  16.    public int getY() { return y; }
  17. }
  18.  
  19.  
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Ellipse2D;
  22.  
  23. public class Circle extends Shape {
  24.  
  25.    public Circle(int x, int y) {
  26.       super.setX(x);
  27.       super.setY(y);
  28.    }
  29.    public void draw( Graphics2D g2 )
  30.    {
  31.       int CircleX = super.getX() - (super.SHAPE_WIDTH/2);
  32.       int CircleY = super.getY() - (super.SHAPE_WIDTH/2);
  33.       //Problem Area
  34.       Ellipse2D.Double circle = new Ellipse2D.Double(CircleX,CircleY,super.SHAPE_WIDTH,super.SHAPE_WIDTH);
  35.       g2.draw(circle);
  36.    }
  37. }
  38.  
  39.  
  40. import java.awt.Graphics;
  41. import java.awt.Graphics2D;
  42. import java.awt.event.MouseAdapter;
  43.  
  44. import java.awt.event.MouseEvent;
  45.  
  46. import java.util.ArrayList;
  47.  
  48. import javax.swing.JPanel;
  49.  
  50. public class CanvasPanel extends JPanel {
  51.  
  52.    private ArrayList shapeList = new ArrayList();
  53.    private boolean circle = false;
  54.  
  55.    public CanvasPanel() {
  56.       try {
  57.          jbInit();
  58.       } catch (Exception e) {
  59.          e.printStackTrace();
  60.       }
  61.    }
  62.  
  63.    public void setCircle(boolean circle) {
  64.       this.circle = circle;
  65.    }
  66.  
  67.    private void jbInit() throws Exception {
  68.       this.addMouseListener(new CanvasPanel_this_mouseAdapter(this));
  69.    }
  70.  
  71.    void this_mouseClicked(MouseEvent e) {
  72.       if(circle == false) {
  73.          Square square = new Square(e.getX(), e.getY());
  74.          shapeList.add(square);
  75.       }
  76.       else {
  77.          Circle circle = new Circle(e.getX(), e.getY());
  78.          shapeList.add(circle);
  79.       }
  80.    repaint();
  81.    }
  82.    public void paintComponent ( Graphics g) {
  83.       for ( int i = 0; i < shapeList.size(); i++)
  84.       {
  85.          Graphics2D g2 = (Graphics2D)g;
  86.          Shape shape = (Shape)shapeList.get(i);
  87.          shape.draw(g2);
  88.       }
  89.    }
  90.  
  91. }
  92. final class CanvasPanel_this_mouseAdapter extends MouseAdapter {
  93.    private CanvasPanel adaptee;
  94.  
  95.    CanvasPanel_this_mouseAdapter(CanvasPanel adaptee) {
  96.       this.adaptee = adaptee;
  97.    }
  98.  
  99.    public void mouseClicked(MouseEvent e) {
  100.       adaptee.this_mouseClicked(e);
  101.    }
  102. }
  103.  
  104.  
Apr 18 '08 #7
sukatoa
539 Contributor
store the shape in an arraylist and repaint the frame. When the frame repaints the menuBar gets painted again under the orignal.
Where is it? sorry if i can't find it....

Is this your complete code? only to choose shape then click to show those shapes? what about the quotes above?

I've test your code, no problem.... but i have doubt the quotes above.....
can you please add more details?

You didn't even set the close operation of your frame.... if you are using Windows, have a look at your task manager... many java process might still running.... they consumes your temp memory..... maybe anytime your system will crash....

sukatoa
Apr 18 '08 #8
SpecialKay
109 New Member
Nope nothing is running, yes that is all my code.
It is suspost to be 6 classes. All the code does is allow you to chose a tool (Square, Circle) then paint that shape on the frame. It is all working fine except the menubar, and the shapes are being doubled under the orignal. If i re-size the window the doubles disapear, untill i draw another shap, then everything on the frame is being doubled.
Apr 18 '08 #9
SpecialKay
109 New Member
i have set the exit
frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
Apr 18 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1836
by: Matt Stanley | last post by:
I am trying to build a page that scales to fit the browser window regardless of size or resolution. The navigation on the top of the page is framed in dark red/brown using CSS with a background color specified in a custom class tag. I only want those cells that are using this custom class to be 1 pixel wide. In the code I did not specify the <td> width but instead inserted a single-pixel, transparent GIF and specified the dimensions of...
7
1539
by: borges2003xx | last post by:
hi everyone can someone suggest me where find a lot programming tricks for achieving the top speed in python? thanks everyone for patience
31
2626
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
1
1963
by: batista | last post by:
Hello all, I have a third praty grid control...named C1grid. Im using it in one of my apps.. Now, I have bind this grid to a custom dataset class named "DataViewEx". The code of the class is below... Now what happens is that im updating this dataviewex class from a separate thread.....
10
2998
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: #include <stdlib.h> #ifndef __LIST_H__ #define __LIST_H__
0
3946
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
3
1246
by: Andy011 | last post by:
Hi, I wanna know how to avoid the program to scan only the first letter of a string... I know it sounds real n00bish, but if you could post a simple example (SIMPLE, not like the help menu from c++) through C++ it'd be awesome :D. Also, I would like to know how to color a text (What library should be used, how do you use a variable to color the text, and so on...). P.S: The reason I need it fast is because I need to deliver a work...
12
1192
by: Boris Ozegovic | last post by:
Hi, I am working on some system, and the communication will take place through the chatterbot which will be written in AIML (interpreter is written in Python). English is not my mother tongue, so I need huge favor: if all of you can write two sentences, so that I could have greater pattern base. The first sentence is finding someone's heart rate, example: "Please, can you tell me John's heart rate", and the other one is setting the...
0
8003
by: Vinod Sadanandan | last post by:
Fast-Start Failover An Overview In Dataguard Environment ============================================================================= This article describes the automatic fast start failover configuration and the conditions for trigerring a fast start failover in dataguard environment . In Faststart failover dataguard configuration if the primary database becomes unavailable, the...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.