Results 1 to 8 of 8
- 10-16-2008, 06:12 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
Newbie need help on JPanel graphics
hello gurus,
need your help. i'm so struggling on this program that i have to do. here's my scenario:
1) i have a frame that is divided by two JPanels, the left and right.
2) the right are subdivided by multiple jpanels and contains buttons and text.
when a button is pressed, it creates a text on the right panel.
3) the right works ok.
4) the left is "not" subdivided. it contains a couple of simple straight lines. and a clored background.
5) what i want to do is: to add additional lines to my left panel when a particular button is pressed from the right panel.
6) thats where im stuck. i'm played around with it but it either make my left drawings disappear or it makes a new panel. h
here's part of my code:
//this is in a different file.
public class LeftSide extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.GREEN);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawLine(275, 50, 275, 500);
}
}
//this is also in a separate file.
private void createBodyGUIs()
{
setLayout(new BorderLayout());
biggerPanel.setLayout(new GridLayout(1,2));
add(biggerPanel, BorderLayout.CENTER);
LeftSide drawings = new LeftSide();
biggerPanel.add(drawings);
createRightColumnGUI(); //creates my goodies on the right side
}
thanks in advance!
- 10-16-2008, 06:33 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Basically you need two panels first. In one of the panel you need two panels again. Do you design in that way?
- 10-16-2008, 06:41 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
See in this example code, how I handle the layouts.
Java Code:public PanelTest() { this.setLayout(new GridLayout(1, 2)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 300); JPanel one = new JPanel(); one.add(new JButton("One")); JPanel two = new JPanel(new GridLayout(2, 1)); two.add(new JButton("Two")); two.add(new JButton("Three")); this.add(one); this.add(two); } public static void main(String[] args) { new PanelTest().setVisible(true); }
- 10-16-2008, 10:46 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
hello eranga,
thank you for replying. basically, what i have is a frame with panel. that panel is divided in two, the left and the right. i have buttons on the right panel and they work perfectly. my problem is my left panel. i have a straight line drawn on it as you can see from my code. i would like to add another line to it everytime a particular button (from the right panel) is pressed.
i don't know how to do that. i don't know how to add another straight line (grapihcs) on the existing straight line on my left panel. could you help me please?
thank you!
-
Your doing some basic no-no's in your graphics code such as subclassing paint and paintComponent where only paintComponent is needed, calling setBackground from within a paint/paintComponent method,...
I think that you would benefit greatly by reading the Sun graphics tutorials. Good luck.Last edited by Fubarable; 10-17-2008 at 02:55 AM.
- 10-17-2008, 03:42 AM #6
The basic idea is to keep the lines in a collection which is a member variable (in class scope) and draw all of the lines on each trip through the paintComponent method.
Another approach is to use a BufferedImage that is the same size as the component. You draw each new line to the image in/via your event code and then simply draw/render the image in the paintComponent method.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.*; import java.util.List; import javax.swing.*; public class AddingLines extends JPanel { List<Line2D.Double> lines = new ArrayList<Line2D.Double>(); Random seed = new Random(); public AddingLines() { setBackground(Color.white); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.blue); for(int i = 0; i < lines.size(); i++) { g2.draw(lines.get(i)); } } private void addLine() { int w = getWidth(); int h = getHeight(); int x1 = seed.nextInt(w); int y1 = seed.nextInt(h); int x2 = seed.nextInt(w); int y2 = seed.nextInt(h); lines.add(new Line2D.Double(x1, y1, x2, y2)); repaint(); } private JPanel getLast() { JButton button = new JButton("add line"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addLine(); } }); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { AddingLines test = new AddingLines(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.add(test.getLast(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
-
It's funny. Our apps were a bit similar:
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.*; public class ShowLines { private static final int WIDTH = 400; private static final Dimension GP_SIZE = new Dimension(WIDTH, WIDTH); private static final String ADD_LINE = "Add Random Line"; private static final String REMOVE_LINE = "Remove Random Line"; private static final float STROKE = 6f; private JPanel mainPanel = new JPanel(); private List<Line2D> lineList = new ArrayList<Line2D>(); private JPanel graphicsPanel = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); myPaint(g); } }; public ShowLines() { graphicsPanel.setPreferredSize(GP_SIZE); graphicsPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 3)); JPanel buttonPanel = createButtonPanel(); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainPanel.setLayout(new BorderLayout(5, 5)); mainPanel.add(graphicsPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.EAST); } private void myPaint(Graphics g) { Graphics2D g2 = (Graphics2D)g; RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHints(rh); g2.setColor(Color.red); g2.setStroke(new BasicStroke(STROKE)); for (Line2D line : lineList) { g2.draw(line); } } private JPanel createButtonPanel() { JButton addLineBtn = new JButton(ADD_LINE); JButton removeLineBtn = new JButton(REMOVE_LINE); LineListener lineListener = new LineListener(); addLineBtn.addActionListener(lineListener); removeLineBtn.addActionListener(lineListener); JPanel innerPanel = new JPanel(new GridLayout(0, 1, 0, 10)); innerPanel.add(addLineBtn); innerPanel.add(removeLineBtn); JPanel bp = new JPanel(); bp.add(innerPanel); return bp; } public JComponent getComponent() { return mainPanel; } private class LineListener implements ActionListener { private Random random = new Random(); public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals(ADD_LINE)) { Line2D line = new Line2D.Double( random.nextInt(WIDTH), random.nextInt(WIDTH), random.nextInt(WIDTH), random.nextInt(WIDTH) ); lineList.add(line); } else if (actionCommand.equals(REMOVE_LINE)) { if (lineList.size() > 0) { lineList.remove(0); } } graphicsPanel.repaint(); } } private static void createAndShowUI() { JFrame frame = new JFrame("ShowLines"); frame.getContentPane().add(new ShowLines().getComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 10-21-2008, 07:44 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
SWT Graphics Example
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:28 PM -
Graphics
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-25-2008, 06:24 PM -
graphics
By Joe2003 in forum Advanced JavaReplies: 4Last Post: 01-18-2008, 07:44 PM -
Graphics
By feniger in forum New To JavaReplies: 1Last Post: 12-29-2007, 04:22 PM -
Updating Graphics
By Greedful in forum Java 2DReplies: 2Last Post: 07-20-2007, 07:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks