Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:18 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 5 posts ] 
Author Question
 Question subject: need help in changing the color of a graph
PostPosted: Sun Oct 31, 2010 7:39 pm 
Offline
Newbie
User avatar

Joined: Sun Oct 31, 2010 7:17 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time

Hi,
I really need help in changing the color of a 3d graph. We were given the code of the graph and asked to change its color and also shape by adding checkboxes. I am a beginner at java so I learned how to use checkboxes by example and did a program that give different shape and different colors using checkboxes and it work. Unfortunately, I couldn’t make it work for the given code. I don’t know what is the problem. Here is the given code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
//import java.awt.BorderLayout;
//import javax.swing.*;

class Point3D
{
    public int x, y, z;
    public Point3D( int X, int Y, int Z )
    {
        x = X; y = Y; z = Z;
    }
}

class Edge
{
    public int a, b;
    public Edge( int A, int B )
    {
        a = A; b = B;
    }
}

public class WireframeViewer extends Applet
implements MouseListener, MouseMotionListener
{
    int width, height;
    int mx, my; // the most recently recorded mouse coordinates
    Image backbuffer;
    Graphics backg;
    int azimuth = 35, elevation = 30;
    Point3D[] vertices;
    Edge[] edges;
    Button okButton;
    //Button okButton1;
    CheckboxGroup radioGroup;
    Checkbox radio1;
    Checkbox radio2;
    Checkbox radio3;
    CheckboxGroup shapeGroup;
    Checkbox shape1;
    Checkbox shape2;
    Checkbox shape3;
    //Image backbuffer;
    //Graphics backg;
    @Override
public void init() {
    width = getSize().width;
    height = getSize().height;
    vertices = new Point3D[ 8 ];
    vertices[0] = new Point3D( -1, -1, -1 );
    vertices[1] = new Point3D( -1, -1, 1 );
    vertices[2] = new Point3D( -1, 1, -1 );
    vertices[3] = new Point3D( -1, 1, 1 );
    vertices[4] = new Point3D( 1, -1, -1 );
    vertices[5] = new Point3D( 1, -1, 1 );
    vertices[6] = new Point3D( 1, 1, -1 );
    vertices[7] = new Point3D( 1, 1, 1 );
    edges = new Edge[ 12 ];
    edges[ 0] = new Edge( 0, 1 );
    edges[ 1] = new Edge( 0, 2 );
    edges[ 2] = new Edge( 0, 4 );
    edges[ 3] = new Edge( 1, 3 );
    edges[ 4] = new Edge( 1, 5 );
    edges[ 5] = new Edge( 2, 3 );
    edges[ 6] = new Edge( 2, 6 );
    edges[ 7] = new Edge( 3, 7 );
    edges[ 8] = new Edge( 4, 5 );
    edges[ 9] = new Edge( 4, 6 );
    edges[10] = new Edge( 5, 7 );
    edges[11] = new Edge( 6, 7 );
    backbuffer = createImage( width, height );
    backg = backbuffer.getGraphics();
    drawWireframe( backg );
    addMouseListener( this );
    addMouseMotionListener( this );
    //setLayout(new FlowLayout());
    okButton = new Button("Color");
    //okButton1 = new Button("Shape");
    radioGroup = new CheckboxGroup();
    radio1 = new Checkbox("Red", radioGroup,false);
    radio2 = new Checkbox("Blue", radioGroup,true);
    radio3 = new Checkbox("Green", radioGroup,false);
    shapeGroup = new CheckboxGroup();
    shape1 = new Checkbox("Rectangle", shapeGroup,false);
    shape2 = new Checkbox("Circle", shapeGroup,true);
    shape3 = new Checkbox("Square", shapeGroup,false);
    add(okButton);
   // add(okButton1);
    add(shape1);
    add(shape2);
    add(shape3);
    add(radio1);
    add(radio2);
    add(radio3);
//   okButton.addActionListener((ActionListener) this);
   // okButton1.addActionListener((ActionListener) this);
    //okButton.setBounds(20,20,100,30);
    //okButton1.setBounds(20,40 , 100,30);
}

    void drawWireframe( Graphics g ) {
// compute coefficients for the projection

double theta = Math.PI * azimuth / 180.0;
double phi = Math.PI * elevation / 180.0;
float cosT = (float)Math.cos( theta ), sinT = (float)Math.sin( theta );
float cosP = (float)Math.cos( phi ), sinP = (float)Math.sin( phi );
float cosTcosP = cosT*cosP, cosTsinP = cosT*sinP,
sinTcosP = sinT*cosP, sinTsinP = sinT*sinP;
// project vertices onto the 2D viewport
Point[] points;
points = new Point[ vertices.length ];
int j;
int scaleFactor = width/4;
float near = 3; // distance from eye to near plane
float nearToObj = 1.5f; // distance from near plane to center of object
for ( j = 0; j < vertices.length; ++j ) {
int x0 = vertices[j].x;
int y0 = vertices[j].y;
int z0 = vertices[j].z;
// compute an orthographic projection
float x1 = cosT*x0 + sinT*z0;
float y1 = -sinTsinP*x0 + cosP*y0 + cosTsinP*z0;
// now adjust things to get a perspective projection
float z1 = cosTcosP*z0 - sinTcosP*x0 - sinP*y0;
x1 = x1*near/(z1+near+nearToObj);
y1 = y1*near/(z1+near+nearToObj);
// the 0.5 is to round off when converting to int
points[j] = new Point(
(int)(width/2 + scaleFactor*x1 + 0.5),
(int)(height/2 - scaleFactor*y1 + 0.5)
);
}

// draw the wireframe

g.setColor( Color.white );
g.fillRect( 0, 0, width, height );
//if (radio1.getLabel()=="Red") g.setColor( Color.red );
//else if (radio2.getLabel()=="Blue") g.setColor(Color.BLUE);
//else g.setColor(Color.green);
g.setColor(Color.MAGENTA);

for ( j = 0; j < edges.length; ++j ) {
g.drawLine(
points[ edges[j].a ].x, points[ edges[j].a ].y,
points[ edges[j].b ].x, points[ edges[j].b ].y
);
}
    }
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) {
mx = e.getX();
my = e.getY();
e.consume();
}
public void mouseReleased( MouseEvent e ) { }
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent e ) {
// get the latest mouse position
int new_mx = e.getX();
int new_my = e.getY();
// adjust angles according to the distance travelled by the mouse
// since the last event
azimuth -= new_mx - mx;
elevation += new_my - my;
// update the backbuffer
drawWireframe( backg );
// update our data
mx = new_mx;
my = new_my;
repaint();
e.consume();
}
   
    @Override
public void update( Graphics g ) {
g.drawImage( backbuffer, 50, 50, this );
showStatus("Elev: "+elevation+" deg, Azim: "+azimuth+" deg");
}

    @Override
public void paint( Graphics g ) {
update( g );
}

public void paint1(Graphics g)
{
    if (radio1.getState()) g.setColor(Color.red);
    else if (radio2.getState()) g.setColor(Color.blue);
    else g.setColor(Color.green);
    if (shape1.getState()) g.drawRect(70,70 , 80, 100);
    else if (shape2.getState()) g.drawOval(70, 70, 100, 100);
    else g.drawRect(60,60,100,100);
}
public void actionPerformed(ActionEvent evt, Graphics g)
   {
       if (evt.getSource() == okButton)
      paint1(g);
   }
}


I added the paint1 and actionperformed methods to the code. Please let me know how to fix it. Thank you and take care.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: need help in changing the color of a graph
PostPosted: Mon Nov 01, 2010 11:32 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
you all ready changing it here :
Code:
if (radio1.getState()) g.setColor(Color.red);
    else if (radio2.getState()) g.setColor(Color.blue);
    else g.setColor(Color.green);

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: need help in changing the color of a graph
PostPosted: Mon Nov 01, 2010 5:03 pm 
Offline
Newbie
User avatar

Joined: Sun Oct 31, 2010 7:17 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
Hi there, thanks for your response. It should work but it is not! I dont know y I still cant change the color of the graph. Did u try it? If yes then does the color change?
Thank you again and take care.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: need help in changing the color of a graph
PostPosted: Tue Nov 02, 2010 10:51 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
Are you sure from the conditions like

if (radio1.getState())

because my be
else g.setColor(Color.green);

is the case always running . :)

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: need help in changing the color of a graph
PostPosted: Wed Nov 03, 2010 12:01 am 
Offline
Newbie
User avatar

Joined: Sun Oct 31, 2010 7:17 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
Yes I am sure cause I tried the following code and it did work:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Buttons extends Applet implements ActionListener
{

Button okButton;
//Button okButton1;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Checkbox radio3;
CheckboxGroup shapeGroup;
Checkbox shape1;
Checkbox shape2;
Checkbox shape3;
Image backbuffer;
Graphics backg;
public void init()
{

setLayout(new FlowLayout());
okButton = new Button("Action");
// okButton1 = new Button("Shape");
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("Red", radioGroup,false);
radio2 = new Checkbox("Blue", radioGroup,true);
radio3 = new Checkbox("Green", radioGroup,false);
shapeGroup = new CheckboxGroup();
shape1 = new Checkbox("Rectangle", shapeGroup,false);
shape2 = new Checkbox("Circle", shapeGroup,true);
shape3 = new Checkbox("Square", shapeGroup,false);
add(okButton);
//add(okButton1);
add(shape1);
add(shape2);
add(shape3);
add(radio1);
add(radio2);
add(radio3);
okButton.addActionListener(this);
//okButton1.addActionListener(this);
}
public void paint(Graphics g )
{
if (radio1.getState()) g.setColor(Color.red);
else if (radio2.getState()) g.setColor(Color.blue);
else g.setColor(Color.green);


if (shape1.getState()) g.drawRect(70,70 , 80, 100);
else if (shape2.getState()) g.drawOval(70, 70, 100, 100);
else g.drawRect(60,60,100,100);
}

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton)
repaint();

//if (evt.getSource() == okButton1)
//repaint();
};

}

If you run it then u can see that both shapes and the colors are changing but when using the same code for the given 3d graph it did not? If you notice in this part:
public class Buttons extends Applet implements ActionListener

There is an ActionListener but in the first code I coudlnt add it to the public class WireframeViewer, if I did then i will get some error.

Thanks again


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 5 posts ] 
Quick reply


  

 Similar topics
 gradient background color using CSS
 Changing the line height of the paragraph
 Change the border color of image and its link
 Changing the color and properties of the header tag
 changing the background color for site main menu.
 inherit background color from the parent element
 Set border bottom color
 Follow window background color
 change background color with RGB
 Background color - mile200

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team