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
Solved

Swing paint method

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
eclipse buid path vs tomcat lib path 10 34
throw exception 21 58
going to wrong jsp page 2 33
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

856 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