The Artima Developer Community
Sponsored Link

Java Answers Forum
Creating basic painting application

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Imran

Posts: 1
Nickname: gizmoc
Registered: Nov, 2003

Creating basic painting application Posted: Nov 13, 2003 12:49 AM
Reply to this message Reply
Advertisement
Hello, I am sophomore Computer Science student and I am stuck on my project.

My problem is very basic.

Heres the project question:

Create an application that enables to user to paint a picture. The user should be able to choose the shape to draw, the color in which the shape should appear and whether the shape should be filled with color. Use the graphical user interface components we discussed in this chapter, such as JComboBoxes, JRadioButtons and JCheckBoxes, to allow the user to select various options. The program should provide a JButton object that allows the user to erase the window.


now, I've created the framework of the program. That is, how the program looks and stuff. But there is one annying problem!


How do I paint?!
As soon as I put the paint method inside my program, it completely overwrites the GUI I've created. Copy-paste the following code in your compiler (and read the comments towards the end):

========






import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Project extends JFrame {

public static void main (String args[]) {
Project app = new Project();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



// Initiating values
Container c;
JPanel p = new JPanel();
JRadioButton oval=new JRadioButton("Oval");
JRadioButton line=new JRadioButton("Line");
JRadioButton rect = new JRadioButton("Rect");
ButtonGroup b = new ButtonGroup();
JCheckBox filled = new JCheckBox("Filled");
JButton clear = new JButton("Clear");
String colorNames[] = {"Red","Green","Blue","Pink","Magenta","Orange"};
Color colors[] = {Color.red,Color.green,Color.blue,Color.pink,Color.magenta,Color.orange};
JList colorList = new JList(colorNames);




public Project() {
//Starting stuff..
super("Microsoft Paint 2004");
setSize(600,600);
c=getContentPane();
c.setLayout(new BorderLayout(5,5));
c.setBackground(Color.white);

//Defining ButtonGroup
b.add(oval);
b.add(line);
b.add(rect);

// Adding stuff to JPanel object. All JPanel stuff goes in here
p.setLayout(new FlowLayout()); // Setting Layout for JPanel
p.add(oval);
p.add(line);
p.add(rect);
p.add(filled);
p.add(clear);
p.add(new JScrollPane(colorList));
p.setBackground(Color.gray);


// Defining JList
colorList.setVisibleRowCount(3);
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// Adding JPanel to Container
c.add(p,BorderLayout.SOUTH);


// Adding Mouse Listeners
addMouseListener(new MyMouseHandler());
addMouseMotionListener(new MyMouseMotionHandler());

//Adding listeners for the Radio Buttons
oval.addItemListener(new RadioButtonListener());
line.addItemListener(new RadioButtonListener());
rect.addItemListener(new RadioButtonListener());





show();
}



private class MyMouseHandler extends MouseAdapter {
public void mouseClicked (MouseEvent e)
{
repaint(); // Calling the paint method

}


}


private class MyMouseMotionHandler extends MouseMotionAdapter {

}


private class RadioButtonListener implements ItemListener {
public void itemStateChanged(ItemEvent e)
{


}
}

//public void paint (Graphics g) { // Run the program without the paint method, and then run it with the paint method. You'll see what I mean.
// g.drawString("Hello",160,160);
//}





}

Topic: help me Previous Topic   Next Topic Topic: TCP/IP programming using sliding window

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use