?
Solved

Swing paint method

Posted on 2000-03-12
5
Medium Priority
?
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …
Suggested Courses

752 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