Line2D between elements in an ArrayList<> - java

I want to draw lines from each Ellipse2D in an ArrayList<>() of them. I know I can hard code the locations of each line to make it look like it connects each Ellipse2D but I want to make it work efficiently with getCenterX() or getCenterY() or something else if there is a better way. I have posted a minimal, self contained example of what I am working with.
The line that is in there is obviously not in the right place. I tried adding coordinates by accessing the elements of the ArrayList and I could not figure out a way to make it work. Any help is appreciated!
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class SelfContainedExample extends JPanel {
private ArrayList<Shape> shapes = new ArrayList<>();
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public SelfContainedExample()
{
//Circle of Radios
shapes.add(new Ellipse2D.Double(110, 70, 15, 15));
shapes.add(new Ellipse2D.Double(90, 80, 15, 15));
shapes.add(new Ellipse2D.Double(70, 100, 15, 15));
shapes.add(new Ellipse2D.Double(70, 120, 15, 15));
shapes.add(new Ellipse2D.Double(90, 140, 15, 15));
shapes.add(new Ellipse2D.Double(110, 150, 15, 15));
shapes.add(new Ellipse2D.Double(130, 140, 15, 15));
shapes.add(new Ellipse2D.Double(150, 120, 15, 15));
shapes.add(new Ellipse2D.Double(150, 100, 15, 15));
shapes.add(new Ellipse2D.Double(130, 80, 15, 15));
//for this line I want to use getCenterX() of the Ellipses added to this ArrayList
shapes.add(new Line2D.Double(10,10,90,10));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.BLUE);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(Shape shape : shapes) {
g2d.fill(shape);
g2d.draw(shape);
}
g2d.dispose();
}
private static void createAndShowGUI()
{
//Make the big window be indented 50 pixels from each edge
//of the screen.
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("Example");
JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("Example",
false, //resizable
false, //closable
false, //maximizable
true); //iconifiable
internalFrame.setSize(260, 260);
internalFrame.add(new SelfContainedExample());
internalFrame.setVisible(true);
desktopPane.add(internalFrame);
desktopPane.setVisible(true);
desktopPane.setBounds(inset, inset,
screenSize.width - inset * 7,
screenSize.height - inset * 4);
frame.add(desktopPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(desktopPane.getSize());
frame.setLocationByPlatform( false );
frame.setLocationRelativeTo( null );
frame.setContentPane( desktopPane );
frame.setVisible( true );
}
}

You want to draw the lines before you draw the Shapes so the Shapes are painted on top of the lines.
The code would be something like:
for (int i = 0; i < shapes.size() - 1; i++)
{
Rectangle r1 = shapes.get(i).getBounds();
Rectangle r2 = shapes.get(i+1).getBounds();
int x1 = r1.x + r1.width / 2;
int y1 = r1.y + r1.height / 2;
int x2 = r2.x + r2.width / 2;
int y2 = r2.y + r2.height / 2;
g.drawLine(x1, y1, x2, y2);
}
for(Shape shape : shapes) {

Related

Associating object with cells on a JTable

I am trying to associate Ellipse2D objects with a cell on a JTable. Specifically if the user clicks on one Ellipse2D then it would highlight a cell on the JTable and accept input. I need to do this in a way that allows the user to associate a string with each Ellipse.
Is what I am trying to do possible and if so how can I do this?
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class SelfContainedExample extends JPanel {
private List<Shape> shapes = new ArrayList<>();
public static void main(String[] args)
{
EventQueue.invokeLater(() -> createAndShowGUI());
}
public SelfContainedExample()
{
//Circle of Radios
shapes.add(new Ellipse2D.Double(250, 100, 20, 20));
shapes.add(new Ellipse2D.Double(160, 100, 20, 20));
shapes.add(new Ellipse2D.Double(70, 100, 20, 20));
shapes.add(new Ellipse2D.Double(70, 160, 20, 20));
shapes.add(new Ellipse2D.Double(160, 160, 20, 20));
shapes.add(new Ellipse2D.Double(250, 160, 20, 20));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.BLACK);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
shapes.forEach(g2d::fill);
g2d.dispose();
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Example");
JPanel panel = new JPanel();
Object[][] data = {{"1_1", "1_2", "1_3"},
{"2_1", "2_2", "2_3"}};
Object[] columnNames = {"1", "2", "3"};
JTable jtable = new JTable(data, columnNames);
panel.setLayout(new BorderLayout());
panel.add(new SelfContainedExample(), BorderLayout.CENTER);
panel.add(jtable, BorderLayout.SOUTH);
panel.setOpaque(true);
panel.setVisible(true);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationByPlatform( false );
frame.setLocationRelativeTo( null );
frame.setContentPane( panel );
frame.setVisible( true );
}
}
Well maybe you start by creating a HashMap where the key is the ellipse and the value is a Point, where the Point would represent the row/column of the ellipse in the table.
//shapes.add(new Ellipse2D.Double(250, 100, 20, 20));
Ellipse2D.Double ellipse = new Ellipse2D.Double(...);
shapes.add(ellipse);
shapesMap.put(ellipse, new Point(0, 0);
the user clicks on one Ellipse2D then it would highlight a cell on the JTable and accept input.
So then you need to add a MouseListener to your panel and handle the mousePressed() event. The code would need to iterate through the List to find the ellipse that contains the point. Then you can start editing on the cell. Maybe something like:
public void mousePressed(MouseEvent e)
{
for (Ellipse2D.Double ellipse: shapes)
{
if (ellipse.contains(e.getPoint())
{
table.requestFocusInWindow();
Point p = shapesMap.get(ellipse);
table.editCellAt(p.x, p.y);
break;
}
}
}

Relative positioning of Graphics2D objects on JPanel

I am making a GUI with Swing and would like to configure the Graphics2D objects with relative positioning. I want to do this in a way that when I resize the window the objects are repainted in a new location relative to the initial anchor position (probably in the top left corner somewhere). I have tried using layout managers to do this but it doesn't impact the actual drawing because the points are pretty much hard coded.
What is the best way to do this? Can anyone provide an good example? Thanks.
Here is my self contained example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DrawPanelMain extends JPanel {
public static final double version = 0.0;
private JPanel btnPanel = new JPanel();
private JPanel switchPanel = new JPanel();
private JScrollPane switchPanelScrollPane = new JScrollPane(switchPanel);
//private JPanel[] panelArray = new JPanel[3];
DrawEllipses drawEllipses = new DrawEllipses(POINT_LIST);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initializePointList();
createAndShowGui();
}
});
}
public static java.util.List<Point> POINT_LIST = new ArrayList<>();
/*
* This loop will initialize POINT_LIST with the set of points for drawing the ellipses.
* The for each loop initializes points for the top row and the second for loop draws the
* right triangle.
*/
private static void initializePointList() {
int ellipsePointsYCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620};
int ellipsePointsXCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620, 680};
int xx = 80;
for (int aXt : ellipsePointsXCoordinate) {
POINT_LIST.add(new Point(aXt, xx));
}
for (int i = 0; i < ellipsePointsYCoordinate.length; i++) {
for (int j = i; j < ellipsePointsYCoordinate.length; j++) {
POINT_LIST.add(new Point(ellipsePointsXCoordinate[i], ellipsePointsYCoordinate[j]));
}
}
}
public DrawPanelMain() {
switchPanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
switchPanel.setBackground(Color.DARK_GRAY);
switchPanel.add(drawEllipses);
switchPanelScrollPane.setPreferredSize(new Dimension(600,600));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// first column
c.gridx = 0;
add(switchPanelScrollPane, c);
// second column
c.gridx = 1;
// first row
c.gridy = 0;
// second row
c.gridy = 1;
c.gridx = 0;
add(btnPanel, c);
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
}
public static void createAndShowGui() {
JFrame frame = new JFrame("RF Connection Panel " + version);
frame.setLayout(new BorderLayout());
frame.add(new DrawPanelMain());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(false);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
/*
* AddSwitchAction will add a new pane to the tabbedPane when the add switch button is clicked
*/
private class AddSwitchAction extends AbstractAction {
public AddSwitchAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
String title = "Switch ";
DrawEllipses tabComponent = new DrawEllipses(POINT_LIST);
switchPanel.add(title, tabComponent);
}
}
}
#SuppressWarnings("serial")
class DrawEllipses extends JPanel {
private final int PREF_W = 750; //Window width
private final int PREF_H = 750; //Window height
private final int OVAL_WIDTH = 30;
private static final Color INACTIVE_COLOR = Color.RED;
private static final Color ACTIVE_COLOR = Color.green;
private java.util.List<Point> points;
private java.util.List<Ellipse2D> ellipses = new ArrayList<>();
private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();
/*
* This method is used to populate "ellipses" with the initialized ellipse2D dimensions
*/
public DrawEllipses(java.util.List<Point> points) {
this.points = points;
for (Point p : points) {
int x = p.x - OVAL_WIDTH / 2;
int y = p.y - OVAL_WIDTH / 2;
int w = OVAL_WIDTH;
int h = OVAL_WIDTH;
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
ellipses.add(ellipse);
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
MyMouseAdapter mListener = new MyMouseAdapter();
addMouseListener(mListener);
addMouseMotionListener(mListener);
}
/*
* paintComponent is used to paint the ellipses
*/
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // paints the background
setBackground(Color.DARK_GRAY);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ellipse2D ellipse : ellipses) {
g2.setColor(ellipseColorMap.get(ellipse));
g2.fill(ellipse);
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.draw(ellipse);
}
/*
* Set the font characteristics, color, and draw the row labels.
*/
g.setFont(new Font("TimesRoman", Font.BOLD, 18));
g.setColor(Color.BLACK);
//Along the top row
g.drawString("External Port", 10, 50);
g.drawString("1", 135, 50);
g.drawString("2", 195, 50);
g.drawString("3", 255, 50);
g.drawString("4", 315, 50);
g.drawString("5", 375, 50);
g.drawString("6", 435, 50);
g.drawString("7", 495, 50);
g.drawString("8", 555, 50);
g.drawString("9", 615, 50);
g.drawString("10", 672, 50);
//Along the Y-axis
g.drawString("Radio 2", 40, 145);
g.drawString("3", 90, 205);
g.drawString("4", 90, 265);
g.drawString("5", 90, 325);
g.drawString("6", 90, 385);
g.drawString("7", 90, 445);
g.drawString("8", 90, 505);
g.drawString("9", 90, 565);
g.drawString("10", 90, 625);
//Along the X-Axis
g.drawString("1", 135, 670);
g.drawString("2", 195, 670);
g.drawString("3", 255, 670);
g.drawString("4", 315, 670);
g.drawString("5", 375, 670);
g.drawString("6", 435, 670);
g.drawString("7", 495, 670);
g.drawString("8", 555, 670);
g.drawString("9", 615, 670);
//Draws a 3DRect around the top row of ellipse2D objects
g2.setColor(Color.lightGray);
g2.draw3DRect(120, 60, 580, 40, true);
g2.draw3DRect(121, 61, 578, 38, true);
g2.draw3DRect(122, 62, 576, 36, true);
}
/*
* MouseAdapter is extended for mousePressed Event that detects if the x, y coordinates
* of a drawn ellipse are clicked. If the color is INACTIVE it is changed to ACTIVE and
* vice versa.
*/
private class MyMouseAdapter extends MouseAdapter {
#Override
/*
* When mousePressed event occurs, the color is toggled between ACTIVE and INACTIVE
*/
public void mousePressed(MouseEvent e) {
Color c;
for (Ellipse2D ellipse : ellipses) {
if (ellipse.contains(e.getPoint())) {
c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
ellipseColorMap.put(ellipse, c);
}
}
repaint();
}
}
/*
* This method will set the dimensions of the JFrame equal to the preferred H x W
*/
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
/*
* Used for button click action to change all ellipses to ACTIVE_COLOR
*/
public void activateAll(){
for (Ellipse2D ellipse : ellipses){
ellipseColorMap.put(ellipse, ACTIVE_COLOR);
}
repaint();
}
/*
* Used for button click action to change all ellipses to INACTIVE_COLOR
*/
public void deactivateAll(){
for (Ellipse2D ellipse : ellipses){
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
repaint();
}
}
As I suggested earlier if you want relative positioning then you need to calculate the relative location every time your paint (or when the component is resized).
Basically you know that widthwise you have 10 circle you want to paint at 30 pixels each and a label at a fixed width of say 100. So that means you have a minimum width of 400 pixels.
Of course that is not reasonable as you want a gap between each circle, so you need to decide what your preferred gap is and multiply that by 9 to determine your true minimum size. Looks like you have about a 30 pixel gap so that is another 270 pixels giving a preferred size of 670 pixels.
So the question is what do you do when the width is greater than 670? Do you increase the gap or leave it at 30?
The harder case is when the width is less than 670, as you will now need to adjust the gap smaller, down to some minimum value.
Once you determine the horizontal gap to use, you go through the same analysis for the vertical gap.
Now when you do your painting you have your starting location and every row you paint will be increased by your vertical gap. And every circle on the row will be increased by the horizontal gap.
I have never tried it but I believe you can use the setFrame(...) method of your Ellipse to dynamically move its location. So when you do your painting the Ellipse will no be in the correct spot and it should respond to mouse events correctly.
So basically you are writing your own custom layout manager for Graphics.
The second approach, instead of doing custom painting, is to use real components.
You can easily create a custom Icon to represent the circles. Then maybe you can use a JToggleButton. Then your logic of changing color can be handled by the toggle button and two different colored Icons.
Now the hard part. I have never tried it before, but I believe you can use the Spring Layout.
SpringLayout positions components relative to one another. It also contains "springs" between the components to allow the gap to grow/shrink.
Those are the two approaches as I see it.

Graphics2D on JPanel and adding JPanel to JFrame

I am trying to draw on a JPanel and add it to a JFrame in my createAndShowGui method. I have tried a few different things: creating the JPanel in the createAndShowGui method, adding the drawing to the JFrame, etc... The one thing that is common, I don't see any of my graphics!
Note: I am able to get the graphics to display in a JTabbedPane but not on a JPanel, which is what I actually want them to show up on to make the code more object oriented.
Edit:
Here is the working concept self contained example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DrawPanelMain extends JPanel {
/*
* Variables used to set the value of preferred height and width
*/
public static final double version = 0.0;
JPanel switchPanel = new JPanel();
JPanel testPanel = new JPanel();
JPanel btnPanel = new JPanel();
DrawEllipses drawEllipses = new DrawEllipses(POINT_LIST);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initializePointList();
createAndShowGui();
}
});
}
public static java.util.List<Point> POINT_LIST = new ArrayList<>();
/*
* This loop will initialize POINT_LIST with the set of points for drawing the ellipses.
* The for each loop initializes points for the top row and the second for loop draws the
* right triangle.
*/
public static void initializePointList() {
int ellipsePointsYCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620};
int ellipsePointsXCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620, 680};
int xx = 80;
for (int aXt : ellipsePointsXCoordinate) {
POINT_LIST.add(new Point(aXt, xx));
}
for (int i = 0; i < ellipsePointsYCoordinate.length; i++) {
for (int j = i; j < ellipsePointsYCoordinate.length; j++) {
POINT_LIST.add(new Point(ellipsePointsXCoordinate[i], ellipsePointsYCoordinate[j]));
}
}
}
public DrawPanelMain() {
testPanel.setBackground(Color.RED);
switchPanel.add(drawEllipses);
setLayout(new BorderLayout());
add(switchPanel, BorderLayout.CENTER);
add(testPanel, BorderLayout.EAST);
add(btnPanel, BorderLayout.SOUTH);
getPreferredSize();
btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
}
public static void createAndShowGui() {
JFrame frame = new JFrame("RF Connection Panel " + version);
frame.setLayout(new BorderLayout());
frame.add(new DrawPanelMain());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(false);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
/*
* AddSwitchAction will add a new pane to the tabbedPane when the add switch button is clicked
*/
private class AddSwitchAction extends AbstractAction {
public AddSwitchAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
int index = 0;
DrawEllipses tabComponent = new DrawEllipses(POINT_LIST);
switchPanel.add(tabComponent, index++);
}
}
}
#SuppressWarnings("serial")
class DrawEllipses extends JPanel {
private final int PREF_W = 750; //Window width
private final int PREF_H = 750; //Window height
private static final int OVAL_WIDTH = 30;
private static final Color INACTIVE_COLOR = Color.RED;
private static final Color ACTIVE_COLOR = Color.green;
private java.util.List<Point> points;
private java.util.List<Ellipse2D> ellipses = new ArrayList<>();
private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();
/*
* This method is used to populate "ellipses" with the initialized ellipse2D dimensions
*/
public DrawEllipses(java.util.List<Point> points) {
this.points = points;
for (Point p : points) {
int x = p.x - OVAL_WIDTH / 2;
int y = p.y - OVAL_WIDTH / 2;
int w = OVAL_WIDTH;
int h = OVAL_WIDTH;
Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
ellipses.add(ellipse);
ellipseColorMap.put(ellipse, INACTIVE_COLOR);
}
MyMouseAdapter mListener = new MyMouseAdapter();
addMouseListener(mListener);
addMouseMotionListener(mListener);
}
/*
* paintComponent is used to paint the ellipses
*/
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ellipse2D ellipse : ellipses) {
g2.setColor(ellipseColorMap.get(ellipse));
g2.fill(ellipse);
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.draw(ellipse);
}
/*
* Set the font characteristics, color, and draw the row labels.
*/
g.setFont(new Font("TimesRoman", Font.BOLD, 18));
g.setColor(Color.BLACK);
//Along the top row
g.drawString("External Port", 10, 50);
g.drawString("1", 135, 50);
g.drawString("2", 195, 50);
g.drawString("3", 255, 50);
g.drawString("4", 315, 50);
g.drawString("5", 375, 50);
g.drawString("6", 435, 50);
g.drawString("7", 495, 50);
g.drawString("8", 555, 50);
g.drawString("9", 615, 50);
g.drawString("10", 672, 50);
//Along the Y-axis
g.drawString("Radio 2", 40, 145);
g.drawString("3", 90, 205);
g.drawString("4", 90, 265);
g.drawString("5", 90, 325);
g.drawString("6", 90, 385);
g.drawString("7", 90, 445);
g.drawString("8", 90, 505);
g.drawString("9", 90, 565);
g.drawString("10", 90, 625);
//Along the X-Axis
g.drawString("1", 135, 670);
g.drawString("2", 195, 670);
g.drawString("3", 255, 670);
g.drawString("4", 315, 670);
g.drawString("5", 375, 670);
g.drawString("6", 435, 670);
g.drawString("7", 495, 670);
g.drawString("8", 555, 670);
g.drawString("9", 615, 670);
//Draws a 3DRect around the top row of ellipse2D objects
g2.setColor(Color.lightGray);
g2.draw3DRect(120, 60, 580, 40, true);
g2.draw3DRect(121, 61, 578, 38, true);
g2.draw3DRect(122, 62, 576, 36, true);
}
/*
* MouseAdapter is extended for mousePressed Event that detects if the x, y coordinates
* of a drawn ellipse are clicked. If the color is INACTIVE it is changed to ACTIVE and
* vice versa.
*/
private class MyMouseAdapter extends MouseAdapter {
#Override
/*
* When mousePressed event occurs, the color is toggled between ACTIVE and INACTIVE
*/
public void mousePressed(MouseEvent e) {
Color c;
for (Ellipse2D ellipse : ellipses) {
if (ellipse.contains(e.getPoint())) {
c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
ellipseColorMap.put(ellipse, c);
}
}
repaint();
}
}
/*
* This method will set the dimensions of the JFrame equal to the preferred H x W
*/
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
switchPanel.add(title, tabComponent);
By default a JPanel uses a FlowLayout which respects the preferred size of the component. The preferred size of your component is (0, 0) so there is nothing to paint. Also, you are using the "title" string which is incorrect (and obsolete as has already been mentioned). That string represents a constraint for the layout manager. You can't just make up a String value. In any case FlowLayout does not accept any contraints so you should just be using:
switchPanel.add(tabComponent);
I am trying to draw on a JPanel
When doing custom painting you need to override the getPreferredSize() of the panel so the layout manager can use the information. If you don't override this method then the size is (0, 0) so there is nothing to paint.
Edit:
First some general comments:
Don't hardcode sizes of the panel. Your hardcoded size of (1200 x 750) is too large for my monitor. If you want full screen then use frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Post code that you are actually testing. As has already been mentioned your posted code doesn't even add the "switchPanel" to the frame.
You haven't updated the code to show how you override the getPreferredSize() method.
Finally, I see in your code that you add the panel dynamically to the visible GUI. In this case the general code should be:
panel.add(....);
panel.revalidate(); // to invoke the layout manager otherwise size is still (0, 0)
panel.repaint();
You are adding DrawEllipses instances to switchPanel using an obsolete method add(String,Component); you should use something like add(component, index). Also, You don't add switchPanel to anything (commented out in DrawPanelMain ctor).

What is wrong with my listener?

I have created the interface gui and added the buttons. But now I am stuck with the "Steady" button. When I click it I want to the circle "light bulb" to change color from yellow to orange.
What am I doing wrong in my code and when I press the "Steady" button nothing is happening?
/**
* Created by Metallion on 30/03/2015.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BelishaBeacon {
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
}
}
public class changeColors extends JPanel {
private boolean choseColor = false;
private int x = 125;
private int y = 80;
public void changeColor(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.ORANGE);
if (!choseColor) {
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
}
public void changeColor() { choseColor = false; }
}
public BelishaBeacon() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
final changeColors colorinG = new changeColors();
JButton jbtFlash = new JButton("Flash");
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
colorinG.changeColor();
colorinG.repaint();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
}
}
You need to add handling logic to your Drawing Swing component, e.g.:
add method to your Drawing to change the color
modify the paintComponent(Graphics) according to the first step
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BelishaBeacon {
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
private boolean changeColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
if (changeColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
changeColors = false;
}
public void changeColors() {
changeColors = true;
repaint();
}
}
public BelishaBeacon() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
JButton jbtFlash = new JButton("Flash");
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapes.changeColors();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
}
}

2D Transformation (Translation, Rotation, Scaling) Program In Java

I have a problem with 2D transformation program
I have the source code
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class House extends JPanel {
MyCanvas canvas;
JSlider sliderTransX, sliderTransY, sliderRotateTheta, sliderRotateX,
sliderRotateY, sliderScaleX, sliderScaleY, sliderWidth;
double transX = 0.0;
double transY = 0.0;
double rotateTheta = 0.0;
double rotateX = 150.0;
double rotateY = 150.0;
double scaleX = 1.0;
double scaleY = 1.0;
float width = 1.0f;
public House() {
super(new BorderLayout());
JPanel controlPanel = new JPanel(new GridLayout(3, 3));
add(controlPanel, BorderLayout.NORTH);
controlPanel.add(new JLabel("Translate(dx,dy): "));
sliderTransX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
100, 50);
sliderTransY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
100, 50);
// To control rotation
controlPanel.add(new JLabel("Rotate(Theta,ox,oy): "));
sliderRotateTheta = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 360,
0, 90, 45);
JPanel subPanel = new JPanel();
subPanel.setLayout(new GridLayout(1, 2));
sliderRotateX = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
150, 50);
sliderRotateY = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
150, 50);
controlPanel.add(subPanel);
// To control scaling
controlPanel.add(new JLabel("Scale(sx,sy)x10E-2:"));
sliderScaleX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
100, 10);
sliderScaleY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
100, 10);
// To control width of line segments
JLabel label4 = new JLabel("Width Control:", JLabel.RIGHT);
sliderWidth = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
sliderWidth.setPaintTicks(true);
sliderWidth.setMajorTickSpacing(5);
sliderWidth.setMinorTickSpacing(1);
sliderWidth.setPaintLabels(true);
sliderWidth.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
width = sliderWidth.getValue();
canvas.repaint();
}
});
JPanel widthPanel = new JPanel();
widthPanel.setLayout(new GridLayout(1, 2));
widthPanel.add(label4);
widthPanel.add(sliderWidth);
add(widthPanel, BorderLayout.SOUTH);
canvas = new MyCanvas();
add(canvas, "Center");
}
public JSlider setSlider(JPanel panel, int orientation, int minimumValue,
int maximumValue, int initValue, int majorTickSpacing,
int minorTickSpacing) {
JSlider slider = new JSlider(orientation, minimumValue, maximumValue,
initValue);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(majorTickSpacing);
slider.setMinorTickSpacing(minorTickSpacing);
slider.setPaintLabels(true);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider tempSlider = (JSlider) e.getSource();
if (tempSlider.equals(sliderTransX)) {
transX = sliderTransX.getValue() - 150.0;
canvas.repaint();
} else if (tempSlider.equals(sliderTransY)) {
transY = sliderTransY.getValue() - 150.0;
canvas.repaint();
} else if (tempSlider.equals(sliderRotateTheta)) {
rotateTheta = sliderRotateTheta.getValue() * Math.PI / 180;
canvas.repaint();
} else if (tempSlider.equals(sliderRotateX)) {
rotateX = sliderRotateX.getValue();
canvas.repaint();
} else if (tempSlider.equals(sliderRotateY)) {
rotateY = sliderRotateY.getValue();
canvas.repaint();
} else if (tempSlider.equals(sliderScaleX)) {
if (sliderScaleX.getValue() != 0.0) {
scaleX = sliderScaleX.getValue() / 100.0;
canvas.repaint();
}
} else if (tempSlider.equals(sliderScaleY)) {
if (sliderScaleY.getValue() != 0.0) {
scaleY = sliderScaleY.getValue() / 100.0;
canvas.repaint();
}
}
}
});
panel.add(slider);
return slider;
}
class MyCanvas extends Canvas {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.translate(transX, transY);
g2D.rotate(rotateTheta, rotateX, rotateY);
g2D.scale(scaleX, scaleY);
BasicStroke stroke = new BasicStroke(width);
g2D.setStroke(stroke);
drawHome(g2D);
}
public void drawHome(Graphics2D g2D) {
Line2D line1 = new Line2D.Float(100f, 200f, 200f, 200f);
Line2D line2 = new Line2D.Float(100f, 200f, 100f, 100f);
Line2D line3 = new Line2D.Float(100f, 100f, 200f, 100f);
Line2D line5 = new Line2D.Float(200f, 100f, 200f, 200f);
g2D.draw(line1);
g2D.draw(line2);
g2D.draw(line3);
g2D.draw(line5);
}
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.getContentPane().add(new House());
f.setDefaultCloseOperation(1);
f.setSize(700, 550);
f.setVisible(true);
}
}
Please help me.
Don't mix AWT and Swing components needlessly: extend JPanel and override paintComponent(). A call to super.paintComponent(g) will clean up rendering, and RenderingHints will improve rotated drawing.
class MyCanvas extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2D.translate(transX, transY);
g2D.rotate(rotateTheta, rotateX, rotateY);
g2D.scale(scaleX, scaleY);
BasicStroke stroke = new BasicStroke(width);
g2D.setStroke(stroke);
drawHome(g2D);
}
...
}

Categories