The Artima Developer Community
Sponsored Link

Java Answers Forum
how to draw

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
yi

Posts: 3
Nickname: ching
Registered: Aug, 2002

how to draw Posted: Aug 5, 2002 6:22 PM
Reply to this message Reply
Advertisement
i do the programe which is about the draw line
but it doesn't work.
it just can draw one line but it doesn't continue draw next line. Also i don't know how to do the Level one

CCure.java
import java.awt.*;
import java.awt.event.*;

public class CCurve extends CloseableFrame
{
public static void main( String [ ] args )
{
Frame f = new CCurve( );

f.setLayout( new FlowLayout( ) );
f.add( new CCurveGUI( ) );

f.pack( );
f.show( );
f.setTitle("draw");
}
}
==================================================

import java.awt.*;

public class CCurveCanvas extends Canvas{
int[][] values;
String color;
int lastx, lasty;
boolean notClicked = false;
public CCurveCanvas(){
super();
}
public void setCoords(int x, int y){
if(notClicked){
lastx = x;
lasty = y;
notClicked = false;
}else{
values = new int[2][2];
values[0][0] = lastx;
values[0][1] = x;
values[1][0] = lasty;
values[1][1] = y;
lastx = x;
lasty = y;
}
}//setCoords
public void setParams(String color){
this.color = color;
repaint();
}
public void nextLevel(String colour){
makeNewValues();
setParams(colour);

}
public void paint(Graphics g){
if (values == null) {
return;
}
if(color.equals("Red")){
g.setColor(Color.red);
}else{
g.setColor(Color.blue);
}//if

int length = values[0].length;
for(int i=0;i<length-1;i++){
/*x1=value[0];
x2=value[0][i+1];
y1=value[1][j];
y2=value[1][j+1];
g.drawLine(x1,x2,y1,y2);*/
g.drawLine(values[0], values[1], values[0][i+1],values[1][i+1]);
}//for
}//paint

}//class

================================================
/**
* A class to draw specified shapes on a graphical user interface.
* The purpose of this class is to demonstrate the use of some of the GUI components form awt.
* @author Phil Sheridan
* @version 1.0 last update 19/7/1
*/

import java.awt.*;
import java.awt.event.*;

public class CCurveGUI extends Panel implements ActionListener,MouseListener
{
public CCurveCanvas theCanvas;
private TextField theLevel;
private TextField theMessage;
private Button theDrawButton;
private Button theDrawNextButton;
private Choice theColour;
private int curlevel =0;

public CCurveGUI( )
{
makeTheComponents( );
doTheLayout( );
addTheListeners();
}

// Make all the objects
private void makeTheComponents( )
{
theCanvas = new CCurveCanvas();
theCanvas.setBackground(Color.yellow);
theCanvas.setSize(500,500);
theLevel = new TextField("0",5);
theLevel.setEditable(false);
theMessage = new TextField(20);
theDrawButton = new Button("Draw");
theDrawNextButton = new Button("Draw Next");
theColour = new Choice();
theColour.add("Red");
theColour.add("Blue");
}

// Layout all the objects
private void doTheLayout( ){
setLayout(new GridLayout(1,2));
Panel left = new Panel();
Panel right = new Panel();
add(left);
add(right);
left.add(theCanvas);
right.setLayout(new FlowLayout());
right.add(theColour);
right.add(theDrawButton);
right.add(theDrawNextButton);
right.add(new Label("Level"));
right.add(theLevel);
right.add(new Label("Message"));
right.add(theMessage);
}

public void addTheListeners(){
theDrawButton.addActionListener(this);
theDrawNextButton.addActionListener(this);
theCanvas.addMouseListener(this);
}
public void actionPerformed( ActionEvent e )
{
if (e.getSource()==theDrawButton){
theCanvas.setParams(theColour.getSelectedItem());
}else{
theCanvas.nextLevel(theColour.getSelectedItem());
//increase leve counter
//theLevel.setText();
}

}
public void mouseReleased( MouseEvent e ) {}
public void mouseEntered( MouseEvent e ) {}
public void mouseClicked( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mouseMoved( MouseEvent e ) {}

public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
theCanvas.setCoords(x,y);
}

public void makeNewValues(int x1, int y1, int x2, int y2, int level, Graphics g){
int[][] newArray = new int[2][value[0].length * 2 - 1];
for(int i=0,j=0;i<array.length;i++,j+=2){
xm= (values[0]+ values[1]+ values[0][i+1]-values[1][i+1])/2;
ym= (values[0]+ values[1]+ values[0][i+1]+values[1][i+1])/2;
/*x1=value[0];
x2=value[0][i+1];
y1=value[1][j];
y2=value[1][j+1];
xm=(x1+x2+yi-yi)/2;

newArray[0][j] =xm;
newArray[1][j+1]=ym;*/

}

}
}
}
=============================================
import java.awt.*;
import java.awt.event.*;

// Class that implements a Window that closes on a window-close event

public class CloseableFrame extends Frame implements WindowListener
{
public CloseableFrame( )
{ addWindowListener( this ); }

public void windowClosing( WindowEvent event )
{ System.exit( 0 ); }

public void windowClosed( WindowEvent event )
{ }

public void windowDeiconified( WindowEvent event )
{ }

public void windowIconified( WindowEvent event )
{ }

public void windowActivated( WindowEvent event )
{ }

public void windowDeactivated( WindowEvent event )
{ }

public void windowOpened( WindowEvent event )
{ }
}

Topic: Newbie needs help! Previous Topic   Next Topic Topic: Server Side Forward Object

Sponsored Links



Google
  Web Artima.com   

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