Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Custom JComponent
Justin Fox
Ranch Hand
Posts: 802
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
ok i have a Battle ship game im doing,
and i want each ship be a separate Component...
so i can add mouse listeners to them
ive extended JComponent and Component...
and when i do
ship BS = new Ship() // ship extends Component..
this should create an instance of ship with Component attributes right??
please help, what when i do the following:
if(e.getSource == BS)
{
// do something
}
nothing happens..
Justin
my code...
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class shipimple extends JPanel { ship BS = new ship("Battle Ship"); int EX = 150, WHY = 100; public shipimple() { BS.addMouseListener(new ClickListener()); BS.addMouseMotionListener(new ShipListener()); setBackground(Color.cyan); setPreferredSize(new Dimension(300,200)); add(BS); } public void paintComponent(Graphics g) { super.paintComponent(g); BS.drawShip(EX,WHY,g); } public class ShipListener implements MouseMotionListener { public void mouseDragged( MouseEvent e) { if(e.getSource() == BS) { EX = e.getX(); WHY = e.getY(); repaint(); } } public void mouseMoved( MouseEvent e){} } public class ClickListener implements MouseListener { public void mousePressed( MouseEvent e){} public void mouseReleased( MouseEvent e){} public void mouseEntered( MouseEvent e){} public void mouseExited( MouseEvent e){} public void mouseClicked( MouseEvent e) { if(e.getSource() == BS) { BS.Rotate(); EX += 10; WHY += 10; repaint(); } } } public class ship extends JComponent { String name; int W = 20; int L = 100; public ship(String Name) { name = Name; } public void drawShip(int x, int y, Graphics g) { g.setColor(Color.blue); g.drawRect(x,y,W,L); g.setColor(Color.orange); g.fillRect(x+1,y+1,W-1,L-1); } public void Rotate() { int temp = W; W = L; L = temp; } } public static void main (String[] args) { JFrame frame = new JFrame("Dragging bitch"); frame.getContentPane().add(new shipimple()); frame.pack(); frame.setVisible(true); } }
You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand
Posts: 4632
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
here's a starter, needs a bit more work to get the coords correct.
(have something else to do at present)
import java.awt.*; import java.awt.event.*; import javax.swing.*; class shipimple extends JPanel { ship BS = new ship("Battle Ship"); int EX = 150, WHY = 100; public shipimple() { setLayout(null);//<-------------- BS.addMouseListener(new ClickListener()); BS.addMouseMotionListener(new ShipListener()); setBackground(Color.cyan); setPreferredSize(new Dimension(300,200)); BS.setBounds(EX,WHY,BS.W,BS.L); add(BS); } //public void paintComponent(Graphics g) //{ //super.paintComponent(g); //BS.drawShip(EX,WHY,g); //} //public class ShipListener implements MouseMotionListener public class ShipListener extends MouseMotionAdapter { public void mouseDragged( MouseEvent e) { if(e.getSource() == BS) { EX = e.getX()+BS.getX();//<----- WHY = e.getY()+BS.getY();//<----- BS.setLocation(EX,WHY);//<----- repaint(); } } //public void mouseMoved( MouseEvent e){} } //public class ClickListener implements MouseListener public class ClickListener extends MouseAdapter { //public void mousePressed( MouseEvent e){} //public void mouseReleased( MouseEvent e){} //public void mouseEntered( MouseEvent e){} //public void mouseExited( MouseEvent e){} public void mouseClicked( MouseEvent e) { if(e.getSource() == BS) { BS.Rotate(); EX += 10; WHY += 10; BS.setBounds(EX,WHY,BS.W,BS.L);//<----- revalidate();//<--------- repaint(); } } } //public class ship extends JComponent public class ship extends JPanel { String name; int W = 20; int L = 100; public ship(String Name) { name = Name; setPreferredSize(new Dimension(W,L));//<---- setBorder(BorderFactory.createLineBorder(Color.blue));//<----- setBackground(Color.orange);//<---------- } //public void drawShip(int x, int y, Graphics g) //{ //g.setColor(Color.blue); //g.drawRect(x,y,W,L); //g.setColor(Color.orange); //g.fillRect(x+1,y+1,W-1,L-1); //} public void Rotate() { int temp = W; W = L; L = temp; } } public static void main (String[] args) { JFrame frame = new JFrame("Dragging bitch"); frame.getContentPane().add(new shipimple()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//<----- frame.pack(); frame.setVisible(true); } }
Justin Fox
Ranch Hand
Posts: 802
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
sweet man, i knew i was on the right track..
just about 3/4 of a mile off : )
thanks,
Justin
You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand
Posts: 4632
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
some changes to get the coords right
class shipimple extends JPanel { ship BS = new ship("Battle Ship"); int EX = 150, WHY = 100; Point startPt;//<-----------add this public shipimple() =================================== public class ClickListener extends MouseAdapter { public void mousePressed( MouseEvent e){startPt = e.getPoint();}//<--add this =================================== public class ShipListener extends MouseMotionAdapter { public void mouseDragged( MouseEvent e) { if(e.getSource() == BS) { EX = BS.getX() + e.getX() - startPt.x;//<-----modify line WHY = BS.getY() + e.getY() - startPt.y;//<-----modify line BS.setLocation(EX,WHY); repaint(); } } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
TriState CheckBox
how to paint some shape over another JComponent
: A gray color box appeare d when loading an image on JPanel
Help 3
GUI
More...