• Post Reply Bookmark Topic Watch Topic
  • New Topic

Draw the line in JPanel using variables from the database  RSS feed

suraya ya'acob
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to draw the line using class with extends JPanel. I 've tried my coding below using class with extends JFrame and it works! but when it comes to extends JPanel, paintComponent seems didn't get the variables that I passed. I attach my coding below
please help me!

import java.awt.*;
import java.awt.Container;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.border.*;
import java.awt.image.*;
import java.sql.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;
class carian23 extends JPanel implements ActionListener{
int xxmula,yymula,xxtamat,yytamat;
int xx1,yy1,xx2,yy2;
private mukadepan mdepan;
private DataPanel dpanel;
public carian23(mukadepan mk, DataPanel dp)

public void actionPerformed (ActionEvent e)
String r=mdepan.cuba();
String url="jdbc dbc b2";
Connection conn = DriverManager.getConnection(url);
String nama, satu2, dua2,tiga2, empat2 = null;
Statement statement =conn.createStatement();

String query = "SELECT * FROM personal p, koordinat k, koordinat2 k2 WHERE p.nopesakit = '" + r + "'";
ResultSet rs = statement.executeQuery( query );
while (rs.next())
satu2= rs.getString("xxone");
tiga2 = rs.getString("yone");
empat2 = rs.getString("ytwo");
int xx1 = Integer.parseInt(satu2);
int yy1 = Integer.parseInt(tiga2);
int xx2 = Integer.parseInt(dua2);
int yy2 = Integer.parseInt(empat2);
catch (Exception ec)
System.err.println("silap DataPanel carian11 ");
xxmula = xx1;
yymula = yy1;
xxtamat = xx2;
yytamat = yy2;
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int width = 10; g2d.setStroke(new BasicStroke (width));
g2d.drawLine(xxmula, yymula,xxtamat,yytamat);
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about the code as it won't compile as is. I think you need a repaint () call after each update of the line terminal point parameters, that is, at the end of you "actionPerformed" method to activate your paintComponent method.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!