Vous êtes sur la page 1sur 8

RIGA TECHNICAL UNIVERSITY

FACULTY OF COMPUTER SCIENCE AND INFORMATION


TECHNOLOGY

Fundamentals of Computer Graphics and Image Processing

Practical Task #2

Straight line algorithm

FCSIT
International Student
Gustavo Eduardo Garcia Sanchez
Stud. ID Nr. 171AMB136
1. Task of Work
During class you will be given the first half of the Bresenham algorithm that can draw
straight horizontal lines (where dx>dy), complete this algorithm so that it could draw
straight vertical lines (where dy>dx). The coordinates of the first and last point of the
line are defined using 4 text fields.

2. Programming Code

public class NewJFrame extends javax.swing.JFrame {


MyCanvas canvas;
int ckcount = 0;
int x1, x2, y1, y2;

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
canvas = new MyCanvas(this.jPanel1);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jButton1 = new javax.swing.JButton();
jTextFieldX1 = new javax.swing.JTextField();
jTextFieldY1 = new javax.swing.JTextField();
jTextFieldX2 = new javax.swing.JTextField();
jTextFieldY2 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(255, 255, 255));


jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jPanel1MouseClicked(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 430, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);

jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextFieldX1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldX1ActionPerformed(evt);
}
});

jLabel1.setText("X1:");

jLabel2.setText("Y1:");

jLabel3.setText("X2:");

jLabel4.setText("Y2:");

jButton2.setText("Clean");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jTextFieldY1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextFieldX2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextFieldY2)
.addComponent(jTextFieldX1)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldX1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldY1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldX2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextFieldY2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);

pack();
}// </editor-fold>

private void jTextFieldX1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
int x1 = Integer.parseInt(this.jTextFieldX1.getText());
int y1 = Integer.parseInt(this.jTextFieldY1.getText());
int x2 = Integer.parseInt(this.jTextFieldX2.getText());
int y2 = Integer.parseInt(this.jTextFieldY2.getText());

int xn = x1;
int yn = y1;
int xi, yi;

int dx = Math.abs(x2 - x1);


int dy = Math.abs(y2 - y1);
if(x1 < x2){
xi = 1;
}
else{
xi = -1;
}
if(y1 < y2){
yi = 1;
}
else{
yi = -1;
}
canvas.showBlackPixel(xn, yn);
int pn;

if(dx >= dy){


pn = 2*dy - dx;
while(xn != x2){
if(pn > 0){
xn = xn + xi;
yn = yn + yi;
pn = pn + 2*dy - 2*dx;
}
else{
xn = xn + xi;
pn = pn + 2*dy;
}
canvas.showBlackPixel(xn, yn);
this.jTextArea1.append("X:" + xn + "Y:" + yn + "P:" + pn + "\n");
}
}
else{
pn = 2*dx - dy;
while(yn != y2){
if(pn > 0){
xn = xn + xi;
yn = yn + yi;
pn = pn + 2*dx - 2*dy;
}
else{
yn = yn + yi;
pn = pn + 2*dx;
}
canvas.showBlackPixel(xn, yn);
this.jTextArea1.append("X:" + xn + "Y:" + yn + "P:" + pn + "\n");
}
}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
canvas.clear();
this.jTextFieldX1.setText(null);
this.jTextFieldX2.setText(null);
this.jTextFieldY1.setText(null);
this.jTextFieldY2.setText(null);
this.jTextArea1.setText(null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextFieldX1;
private javax.swing.JTextField jTextFieldX2;
private javax.swing.JTextField jTextFieldY1;
private javax.swing.JTextField jTextFieldY2;
// End of variables declaration
}
3. Screen Shots

Figure 1: Frame at the beginning of the program.

Figure 2. Drawing of one straight horizontal line and other with dx > dy. The coordinates are defined in the 4 text fields.
Figure 3. Drawing of one straight vertical line and other with dy > dx. The coordinates are defined in the 4 text fields.

Vous aimerez peut-être aussi