# 2
This is my code. There's still error in it. It can not show the rectangle and also the characters. Could you fix my code? I am still a beginner in this field
I am sorry if my code is not formatted as well, because I am a new comer in this forum. So please help me to solve my problem, I expecting a lot from you.
/** Here is my code **/
package brute_force;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
/*
* BorderLayoutDemo.java
*
*/
public class BruteForceAnimation4 extends JPanel{
/** Variable for drawing character inside rectangle **/
private String[] mSourceString = {"G","C","A","T","C","G","C","A","G","A","G","A","G","T"};
private String[] mPatternString = {"G","C","A","G","A","G","A","G"};
/** This is variables to draw the rectangle **/
private int xPosSource = 40;
private int yPosSource = 100;
private int xPosPattern = 80;
private int yPosPattern = 80;
/** constants for predefined colors */
private static final Color lightBlue = new Color(153, 204, 255);
public BruteForceAnimation4()
{
super();
}
public static void addComponentsToPane(Container pane) {
JLabel lblTitle = new JLabel("Brute Force String Searching
Algorithm", SwingConstants.CENTER);
String bruteForceCode[] = {
"int count = 0",//0
"int m= mPattern.length();", //1
"int n= mSource .length();",//2
"outer:",//3
"for (int i = 0; i <= n - m; ++i) {", //4
"for (int k = 0; k < m; ++k) {",//5
"if (mPattern.charAt(k) != mSource.charAt(i + k)) {",//6
"continue outer;", //7
" }", //8
" }",//9
"++count;",//10
"}", //11
"return count;",//12
"}" //13
};
JList list = new JList(bruteForceCode); // a container for pseud code
JButton cmdRun = new JButton("Run");
JButton cmdStep = new JButton("Step");
//Set the title of the applet
lblTitle.setFont(new Font("Serif", Font.BOLD, 18));
JPanel buttons = new JPanel();
buttons.add(cmdRun);
buttons.add(cmdStep);
buttons.setBackground(lightBlue);
//Set the size and border of list (JList component)
Border etch = BorderFactory.createEtchedBorder();
list.setBorder(BorderFactory.createTitledBorder(etch, "Brute Force
Code"));
JPanel listPanel = new JPanel();
listPanel.add(list);
listPanel.setBackground(lightBlue);
list.setBackground(lightBlue);
BruteForceAnimation4 border = new BruteForceAnimation4();
pane.add(lblTitle, BorderLayout.NORTH);
pane.add(border, BorderLayout.CENTER);
pane.add(listPanel, BorderLayout.EAST);
pane.add(buttons, BorderLayout.SOUTH);
pane.setBackground(lightBlue);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
setBackground(lightBlue);
drawSourceString(g2, mSourceString);
}
/** this is the method to draw character inside rectangles **/
/** but it still wrong **/
public void drawSourceString(Graphics2D g2,String[] mSource)
{
if (mSource == null)
{
return;
}
for (int i=0; i < mSource.length; i++)
{
g2.drawRect(xPosSource, yPosSource, 60, 40);
g2.drawString(mSource,40,40);
xPosSource += 30;
}
//This is to count the length of the the Source
System.out.println("Your length" +mSource.length);
}
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("Brute Force Algorithm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Use the content pane's default BorderLayout. No need for
//setLayout(new BorderLayout());
//Display the window.
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}
# 6
import java.awt.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.border.Border;
public class BFA4 extends JPanel {
private String[] mSourceString = {
"G","C","A","T","C","G","C","A","G","A","G","A","G","T"
};
private String[] mPatternString = {"G","C","A","G","A","G","A","G"};
private static final Color lightBlue = new Color(153, 204, 255);
public BFA4() {
super();
setBackground(lightBlue);
}
public static void addComponentsToPane(Container pane) {
JLabel lblTitle = new JLabel("Brute Force String Searching " +
"Algorithm", SwingConstants.CENTER);
String[] bruteForceCode = {
"int count = 0",//0
"int m = mPattern.length();",//1
"int n = mSource .length();",//2
"outer:",//3
" for (int i = 0; i <= n - m; ++i) {",//4
" for (int k = 0; k < m; ++k) {",//5
" if (mPattern.charAt(k) != mSource.charAt(i + k)) {",//6
" continue outer;", //7
" }",//8
" }",//9
" ++count;", //10
" }",//11
" return count;",//12
"}"//13
};
JList list = new JList(bruteForceCode); // a container for pseud code
JButton cmdRun = new JButton("Run");
JButton cmdStep = new JButton("Step");
lblTitle.setFont(new Font("Serif", Font.BOLD, 18));
JPanel buttons = new JPanel();
buttons.add(cmdRun);
buttons.add(cmdStep);
buttons.setBackground(lightBlue);
//Set the size and border of list (JList component)
Border etch = BorderFactory.createEtchedBorder();
list.setBorder(BorderFactory.createTitledBorder(etch, "Brute Force Code"));
JPanel listPanel = new JPanel();
listPanel.add(list);
listPanel.setBackground(lightBlue);
list.setBackground(lightBlue);
BFA4 border = new BFA4();
pane.add(lblTitle, BorderLayout.NORTH);
pane.add(border, BorderLayout.CENTER);
pane.add(listPanel, BorderLayout.EAST);
pane.add(buttons, BorderLayout.SOUTH);
pane.setBackground(lightBlue);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// set this in the class constructor, not here
//setBackground(lightBlue);
drawSourceString(g2, mSourceString);
}
public void drawSourceString(Graphics2D g2,String[] mSource) {
if (mSource == null) {
return;
}
Font font = g2.getFont().deriveFont(14f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics(mSource[0], frc);
float height = lm.getAscent() + lm.getDescent();
int x0 = 20;
int xPad = 5;
int yPad = 50;
int y = yPad;
int w = 15;
int h = 20;
float sy = y + (h + height)/2 - lm.getDescent();
for (int j=0; j < mSource.length; j++) {
int x = x0 + j*(w + xPad);
g2.drawRect(x, y, w, h);
float width = (float)font.getStringBounds(mSource[j], frc).getWidth();
float sx = x + (w - width)/2;
g2.drawString(mSource[j], sx, sy);
}
//This is to count the length of the the Source
System.out.println("Your length" +mSource.length);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Brute Force Algorithm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}
# 7
Here's a short elegant solution for you.
If you like my answer, please give me your Duke Dollars. ;-)
#
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class StringRectPaintPanel extends JPanel {
private String s;
public StringRectPaintPanel(String s) {
this.s = s;
}
public void paint(Graphics g) {
FontMetrics fm = getFontMetrics(getFont());
int iX = 50;
int iY = 50;
for (int i=0; i<this.s.length(); i++) {
char c = s.charAt(i);
int h = fm.getHeight();
int w = fm.charWidth(c);
g.drawRect(iX, iY, w, h);
g.drawString(String.valueOf(c), iX, iY+h);
iX = iX + w;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new StringRectPaintPanel("GCATCGCAGAGAGT"));
frame.setSize(500,300);
frame.setVisible(true);
}
}
>