Solved

Swing paint method

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

14 Experts available now in Live!

Get 1:1 Help Now