Solved

Swing paint method

Posted on 2000-03-12
5
573 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

775 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