Solved

Swing paint method

Posted on 2000-03-12
5
572 Views
Last Modified: 2013-11-23
Whats wrong with this? All I want to do is display (anything!) on the JPanel area....
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class graph extends JFrame implements ActionListener
{
JFrame f;
JButton zoomin, zoomout, select, draw;
public JPanel mainpane, area, buttonpanel;
public JPanel keypane;

 public graph()
 {
 BorderLayout bord = new BorderLayout();
 f = new JFrame();
 f.setSize(300,250);
 f.setTitle("2D Graph");

 zoomin = new JButton("+");
 zoomout = new JButton("-");
 select = new JButton("Select");
 draw = new JButton("Draw");

 mainpane = new JPanel();
 area = new JPanel();
 buttonpanel = new JPanel();
 keypane = new JPanel();

 area.setBackground(Color.white);
 setContentPane(mainpane);
 mainpane.setLayout(bord);
 mainpane.add("North", buttonpanel);
 buttonpanel.add(zoomin);
 buttonpanel.add(zoomout);
 buttonpanel.add(select);
 buttonpanel.add(draw);
 mainpane.add("Center", area);
 f.getContentPane().add(mainpane);

 // adds the ActionListeners to the components
 zoomin.addActionListener(this);
 zoomout.addActionListener(this);
 select.addActionListener(this);
 draw.addActionListener(this);

 f.setVisible(true);

  // enables frame to be closed
       f.addWindowListener( new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                 System.exit(0);}});
 
 }

 public void actionPerformed(ActionEvent e)
 {
  if (e.getSource() == zoomin)
  {
  System.out.println("zoomin");
  repaint();
  }

  if (e.getSource() == zoomout)
  {
  System.out.println("zoomout");
  repaint();
  }

  if (e.getSource() == select)
  {
  System.out.println("select");
  repaint();
  }

  if (e.getSource() == draw)
  {
  System.out.println("draw");
  repaint();
  draw();
  }
 }

 public void paint(Graphics g)
 {
 g.setColor(Color.black);
 g.drawString("Working", 100, 100);
 g.fillOval(200, 200, 100, 110);
 System.out.println("in Paint method!!!!");
 }

 public void draw()
 {
 System.out.println("in draw method!!!!");
 }

 public void select()
 {
 }

 public void create_graph()
 {
 }

}
0
Comment
Question by:saivon
5 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2610224
so what's the problem,,,,,:)
Where is ur main method()
have u written it and called the object of ur class?
RJAckman
0
 
LVL 1

Expert Comment

by:kermit007
ID: 2610406
add this to your class.

public static void main( String args[] )
{
      graph app = new graph();
}
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 2611407
don't touch the paint() method - use paintComponent instead.
or at least call super.paint(g);
0
 

Author Comment

by:saivon
ID: 2611615
The main method is in a different class called grapheng that just makes objects of the graphs. It put all the components in the frame and everything but doesnt paint a thing. Ive managed to paint on a Canvas using the AWT, but cant do it in swing for some weird reason :)
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2611652
paintComponent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now